Example #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;
 }
Example #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++;
 }
Example #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);
            }
        }
Example #4
0
 public Treap(Treap copy){
     x = copy.x;
     y = copy.y;
     Left = copy.Left;
     Right = copy.Right;
     Count++;
 }
Example #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; 
        }
Example #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++;
 }
Example #7
0
 public Treap(Treap copy)
 {
     x     = copy.x;
     y     = copy.y;
     Left  = copy.Left;
     Right = copy.Right;
     Count++;
 }
Example #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);
        }
Example #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;
 }
Example #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);
        }
Example #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);
        }
Example #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);
        }
Example #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);
 }
Example #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));
            }
        }
Example #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);
     }
 }