Ejemplo n.º 1
0
 public static Treap Treapify(int[] x, int[] y) {
     Treap Root = new Treap();
     for (int i = 0; i < 11; i++) {
         Root = Root.Grow(x[i], y[i]);
     }
     return Root;
 }
Ejemplo n.º 2
0
 public Treap(int key, int priority, Treap left = null, Treap right = null) {
     this.x = key;
     this.y = priority;
     this.Left = left;
     this.Right = right;
     Count++;
 }
Ejemplo n.º 3
0
        private void Split(int key, out Treap left, out Treap right)
        {
            Treap NewTree = null;

            if (this.x <= key)
            {
                if (this.Right == null)
                {
                    right = null;
                }
                else
                {
                    this.Right.Split(key, out NewTree, out right);
                }
                left = new Treap(this.x, this.y, this.Left, NewTree);
            }
            else
            {
                if (this.Left == null)
                {
                    left = null;
                }
                else
                {
                    this.Left.Split(key, out left, out NewTree);
                }
                right = new Treap(this.x, this.y, NewTree, this.Right);
            }
        }
Ejemplo n.º 4
0
 public Treap(Treap copy){
     x = copy.x;
     y = copy.y;
     Left = copy.Left;
     Right = copy.Right;
     Count++;
 }
Ejemplo n.º 5
0
        public static Treap Treapify(int[] numbers) {
            Random dice = new Random();
            Treap Root = new Treap();

            foreach (int number in numbers)
                Root = Root.Grow(number, dice.Next());
            return Root; 
        }
Ejemplo n.º 6
0
 public Treap(int key, int priority, Treap left = null, Treap right = null)
 {
     this.x     = key;
     this.y     = priority;
     this.Left  = left;
     this.Right = right;
     Count++;
 }
Ejemplo n.º 7
0
 public Treap(Treap copy)
 {
     x     = copy.x;
     y     = copy.y;
     Left  = copy.Left;
     Right = copy.Right;
     Count++;
 }
Ejemplo n.º 8
0
        private static Treap Merge(Treap left, Treap right){
            if (left == null) return right;
            if (right == null) return left;

            if (left.y > right.y) 
                return new Treap(left.x, left.y, left.Left, Merge(left.Right, right));
            else
                return new Treap(right.x, right.y, Merge(left, right.Left), right.Right);
        }
Ejemplo n.º 9
0
 private bool Search(int key, Treap gazing){
     if (gazing == null) return false;
     if (key < gazing.x)
         return Search(key, gazing.Left);
     if (key > gazing.x)
         return Search(key, gazing.Right);
     if (gazing.x == key)
         return true;
     return true;
 }
Ejemplo n.º 10
0
        public static Treap Treapify(int[] x, int[] y)
        {
            Treap Root = new Treap();

            for (int i = 0; i < 11; i++)
            {
                Root = Root.Grow(x[i], y[i]);
            }
            return(Root);
        }
Ejemplo n.º 11
0
        public Treap Grow(int x, int y)
        {
            Treap left, right;

            this.Split(x, out left, out right);
            Treap buf    = Merge(left, new Treap(x, y));
            Treap result = Merge(buf, right);

            return(result);
        }
Ejemplo n.º 12
0
        public static Treap Treapify(int[] numbers)
        {
            Random dice = new Random();
            Treap  Root = new Treap();

            foreach (int number in numbers)
            {
                Root = Root.Grow(number, dice.Next());
            }
            return(Root);
        }
Ejemplo n.º 13
0
 private bool Search(int key, Treap gazing)
 {
     if (gazing == null)
     {
         return(false);
     }
     if (key < gazing.x)
     {
         return(Search(key, gazing.Left));
     }
     if (key > gazing.x)
     {
         return(Search(key, gazing.Right));
     }
     if (gazing.x == key)
     {
         return(true);
     }
     return(true);
 }
Ejemplo n.º 14
0
        private static Treap Merge(Treap left, Treap right)
        {
            if (left == null)
            {
                return(right);
            }
            if (right == null)
            {
                return(left);
            }

            if (left.y > right.y)
            {
                return(new Treap(left.x, left.y, left.Left, Merge(left.Right, right)));
            }
            else
            {
                return(new Treap(right.x, right.y, Merge(left, right.Left), right.Right));
            }
        }
Ejemplo n.º 15
0
 private void Split(int key, out Treap left, out Treap right){
     Treap NewTree = null;
     
     if (this.x <= key) {
         if (this.Right == null) 
             right = null; 
          else 
             this.Right.Split(key, out NewTree, out right);
         left = new Treap(this.x, this.y, this.Left, NewTree);
     } else {
         if (this.Left == null) 
             left = null; 
          else 
             this.Left.Split(key, out left, out NewTree);
         right = new Treap(this.x, this.y, NewTree, this.Right);
     }
 }