Ejemplo n.º 1
0
 /// <summary>
 /// Clears all the objects in this instance.
 /// </summary>
 /// <remarks>
 /// <b>Notes to Inheritors: </b>
 ///  Derived classes can override this method to change the behavior of the <see cref="Clear"/> method.
 /// </remarks>
 protected virtual void ClearItems()
 {
     tree  = null;
     Count = 0;
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Adds an item to the <see cref="ICollection{T}"/>.
        /// </summary>
        /// <param name="subtree">The subtree.</param>
        /// <exception cref="NotSupportedException">The <see cref="ICollection{T}"/> is read-only.</exception>
        /// <exception cref="InvalidOperationException">The <see cref="BinaryTree{T}"/> is full.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="subtree"/> is null (Nothing in Visual Basic).</exception>
        public void Add(BinaryTree <T> subtree)
        {
            Guard.ArgumentNotNull(subtree, "subtree");

            AddItem(subtree);
        }
Ejemplo n.º 3
0
        private void Splay(KeyValuePair <TKey, TValue> item)
        {
            var header   = new BinaryTree <KeyValuePair <TKey, TValue> >(nullPair, null, null, false);
            var left     = header;
            var right    = header;
            var tempRoot = Tree;

            while (true)
            {
                BinaryTree <KeyValuePair <TKey, TValue> > rotateTree;
                if (Comparer.Compare(item, tempRoot.Data) < 0)
                {
                    if (tempRoot.Left == null)
                    {
                        break;
                    }

                    if (Comparer.Compare(item, tempRoot.Left.Data) < 0)
                    {
                        rotateTree       = tempRoot.Left; /* rotate right */
                        tempRoot.Left    = rotateTree.Right;
                        rotateTree.Right = tempRoot;
                        tempRoot         = rotateTree;
                        if (tempRoot.Left == null)
                        {
                            break;
                        }
                    }
                    right.Left = tempRoot; /* link right */
                    right      = tempRoot;
                    tempRoot   = tempRoot.Left;
                }
                else if (Comparer.Compare(item, tempRoot.Data) > 0)
                {
                    if (tempRoot.Right == null)
                    {
                        break;
                    }
                    if (Comparer.Compare(item, tempRoot.Right.Data) > 0)
                    {
                        rotateTree      = tempRoot.Right; /* rotate left */
                        tempRoot.Right  = rotateTree.Left;
                        rotateTree.Left = tempRoot;
                        tempRoot        = rotateTree;
                        if (tempRoot.Right == null)
                        {
                            break;
                        }
                    }
                    left.Right = tempRoot; /* link left */
                    left       = tempRoot;
                    tempRoot   = tempRoot.Right;
                }
                else
                {
                    break;
                }
            }
            left.Right     = tempRoot.Left; /* assemble */
            right.Left     = tempRoot.Right;
            tempRoot.Left  = header.Right;
            tempRoot.Right = header.Left;
            Tree           = tempRoot;
        }
Ejemplo n.º 4
0
 /// <param name="data">The data contained in this node.</param>
 /// <param name="left">The left subtree.</param>
 /// <param name="right">The right subtree.</param>
 public BinaryTree(T data, BinaryTree <T> left, BinaryTree <T> right)
     : this(data, left, right, true)
 {
 }