Пример #1
0
        /// <summary>
        /// Split treap on two treaps.All items right > item, item > all items left.
        /// </summary>
        /// <param name="item"></param>
        /// <param name="left"></param>
        /// <param name="right"></param>
        internal void Split(int item, out Treap left, out Treap right)
        {
            left  = null;
            right = null;
            Treap newTree = null;

            if (this.Item <= item)
            {
                if (this.Right == null)
                {
                    right = null;
                }
                else
                {
                    this.Right.Split(item, out newTree, out right);
                }
                left = new Treap(this.Item, this.Priority, this.Left, newTree);
            }
            else
            {
                if (this.Left == null)
                {
                    left = null;
                }
                else
                {
                    this.Left.Split(item, out left, out newTree);
                }
                right = new Treap(this.Item, this.Priority, newTree, this.Right);
            }
        }
Пример #2
0
 private Treap(int item, int priority, Treap left, Treap right)
 {
     this.Item     = item;
     this.Priority = priority;
     this.Left     = left;
     this.Right    = right;
 }
Пример #3
0
 private Treap(int item, int priority, Treap left, Treap right)
 {
     this.Item = item;
     this.Priority = priority;
     this.Left = left;
     this.Right = right;
 }
Пример #4
0
 private void InitializeList(Treap treap)
 {
     if (treap == null || (treap.Item == 0 && treap.Priority == 0))
     {
         return;
     }
     InitializeList(treap.Left);
     this.list.Add(treap.Item);
     InitializeList(treap.Right);
 }
Пример #5
0
 private void InitializeList(Treap treap)
 {
     if (treap == null || (treap.Item == 0 && treap.Priority == 0))
     {
         return;
     }
     InitializeList(treap.Left);
     this.list.Add(treap.Item);
     InitializeList(treap.Right);
 }
Пример #6
0
        internal void Remove(int item)
        {
            Treap left       = new Treap();
            Treap right      = new Treap();
            Treap removeItem = new Treap();

            this.Split(item - 1, out left, out right);
            this.Split(item, out removeItem, out right);
            Treap result = Merge(left, right);

            this.Item     = result.Item;
            this.Priority = result.Priority;
            this.Left     = result.Left;
            this.Right    = result.Right;
        }
Пример #7
0
        internal void Add(int item)
        {
            if (this.Find(item))
            {
                return;
            }
            Treap left;
            Treap right;

            this.Split(item, out left, out right);
            Treap result = new Treap(item, random.Next());

            result        = Merge(left, result);
            result        = Merge(result, right);
            this.Item     = result.Item;
            this.Priority = result.Priority;
            this.Left     = result.Left;
            this.Right    = result.Right;
        }
Пример #8
0
 /// <summary>
 /// Merge two treaps. All items right > all items left!!!
 /// </summary>
 /// <param name="left"></param>
 /// <param name="right"></param>
 /// <returns></returns>
 internal static Treap Merge(Treap left, Treap right)
 {
     if (left == null)
     {
         return(right);
     }
     if (right == null)
     {
         return(left);
     }
     if (left.Priority > right.Priority)
     {
         Treap newRight = Merge(left.Right, right);
         return(new Treap(left.Item, left.Priority, left.Left, newRight));
     }
     else
     {
         Treap newLeft = Merge(left, right.Left);
         return(new Treap(right.Item, right.Priority, newLeft, right.Right));
     }
 }
Пример #9
0
 /// <summary>
 /// Merge two treaps. All items right > all items left!!!
 /// </summary>
 /// <param name="left"></param>
 /// <param name="right"></param>
 /// <returns></returns>
 internal static Treap Merge(Treap left, Treap right)
 {
     if (left == null)
     {
         return right;
     }
     if (right == null)
     {
         return left;
     }
     if (left.Priority > right.Priority)
     {
         Treap newRight = Merge(left.Right, right);
         return new Treap(left.Item, left.Priority, left.Left, newRight);
     }
     else
     {
         Treap newLeft = Merge(left, right.Left);
         return new Treap(right.Item, right.Priority, newLeft, right.Right);
     }
 }
Пример #10
0
 internal TreapIterator(Treap treap)
 {
     InitializeList(treap);
 }
Пример #11
0
 internal TreapIterator(Treap treap)
 {
     InitializeList(treap);
 }
Пример #12
0
 /// <summary>
 /// Split treap on two treaps.All items right > item, item > all items left.
 /// </summary>
 /// <param name="item"></param>
 /// <param name="left"></param>
 /// <param name="right"></param>
 internal void Split(int item, out Treap left, out Treap right)
 {
     left = null;
     right = null;
     Treap newTree = null;
     if (this.Item <= item)
     {
         if (this.Right == null)
         {
             right = null;
         }
         else
         {
             this.Right.Split(item, out newTree, out right);
         }
         left = new Treap(this.Item, this.Priority, this.Left, newTree);
     }
     else
     {
         if (this.Left == null)
         {
             left = null;
         }
         else
         {
             this.Left.Split(item, out left, out newTree);
         }
         right = new Treap(this.Item, this.Priority, newTree, this.Right);
     }
 }
Пример #13
0
 internal void Remove(int item)
 {
     Treap left = new Treap();
     Treap right = new Treap();
     Treap removeItem = new Treap();
     this.Split(item - 1, out left, out right);
     this.Split(item, out removeItem, out right);
     Treap result = Merge(left, right);
     this.Item = result.Item;
     this.Priority = result.Priority;
     this.Left = result.Left;
     this.Right = result.Right;
 }
Пример #14
0
 internal void Add(int item)
 {
     if (this.Find(item))
     {
         return;
     }
     Treap left;
     Treap right;
     this.Split(item, out left, out right);
     Treap result = new Treap(item, random.Next());
     result = Merge(left, result);
     result = Merge(result, right);
     this.Item = result.Item;
     this.Priority = result.Priority;
     this.Left = result.Left;
     this.Right = result.Right;
 }
Пример #15
0
 public void Initialize()
 {
     treap = new Treap();
 }