Exemplo n.º 1
0
 public void Reset()
 {
     current = null;
 }
Exemplo n.º 2
0
        /// <summary>
        /// Returns the depth of a subtree rooted at the parameter value
        /// </summary>
        public virtual int GetDepth(T value)
        {
            BinaryTreeNode <T> node = this.Find(value);

            return(this.GetDepth(node));
        }
Exemplo n.º 3
0
 public void Dispose()
 {
     current = null;
     tree    = null;
 }
Exemplo n.º 4
0
        /// <summary>
        /// Removes a node from the tree and returns whether the removal was successful.
        /// </summary>>
        public virtual bool Remove(BinaryTreeNode <T> removeNode)
        {
            if (removeNode == null || removeNode.Tree != this)
            {
                return(false); //value doesn't exist or not of this tree
            }
            //Note whether the node to be removed is the root of the tree
            bool wasHead = (removeNode == head);

            if (this.Count == 1)
            {
                this.head       = null; //only element was the root
                removeNode.Tree = null;

                size--;                 //decrease total element count
            }
            else if (removeNode.IsLeaf) //Case 1: No Children
            {
                //Remove node from its parent
                if (removeNode.IsLeftChild)
                {
                    removeNode.Parent.LeftChild = null;
                }
                else
                {
                    removeNode.Parent.RightChild = null;
                }

                removeNode.Tree   = null;
                removeNode.Parent = null;

                size--;                          //decrease total element count
            }
            else if (removeNode.ChildCount == 1) //Case 2: One Child
            {
                if (removeNode.HasLeftChild)
                {
                    //Put left child node in place of the node to be removed
                    removeNode.LeftChild.Parent = removeNode.Parent; //update parent

                    if (wasHead)
                    {
                        this.Root = removeNode.LeftChild; //update root reference if needed
                    }
                    if (removeNode.IsLeftChild)           //update the parent's child reference
                    {
                        removeNode.Parent.LeftChild = removeNode.LeftChild;
                    }
                    else
                    {
                        removeNode.Parent.RightChild = removeNode.LeftChild;
                    }
                }
                else //Has right child
                {
                    //Put left node in place of the node to be removed
                    removeNode.RightChild.Parent = removeNode.Parent; //update parent

                    if (wasHead)
                    {
                        this.Root = removeNode.RightChild; //update root reference if needed
                    }
                    if (removeNode.IsLeftChild)            //update the parent's child reference
                    {
                        removeNode.Parent.LeftChild = removeNode.RightChild;
                    }
                    else
                    {
                        removeNode.Parent.RightChild = removeNode.RightChild;
                    }
                }

                removeNode.Tree       = null;
                removeNode.Parent     = null;
                removeNode.LeftChild  = null;
                removeNode.RightChild = null;

                size--; //decrease total element count
            }
            else //Case 3: Two Children
            {
                //Find inorder predecessor (right-most node in left subtree)
                BinaryTreeNode <T> successorNode = removeNode.LeftChild;
                while (successorNode.RightChild != null)
                {
                    successorNode = successorNode.RightChild;
                }

                removeNode.Value = successorNode.Value; //replace value

                this.Remove(successorNode);             //recursively remove the inorder predecessor
            }


            return(true);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Removes a value from the tree and returns whether the removal was successful.
        /// </summary>
        public virtual bool Remove(T value)
        {
            BinaryTreeNode <T> removeNode = Find(value);

            return(this.Remove(removeNode));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Adds a new element to the tree
        /// </summary>
        public virtual void Add(T value)
        {
            BinaryTreeNode <T> node = new BinaryTreeNode <T>(value);

            this.Add(node);
        }
Exemplo n.º 7
0
 /// <summary>
 /// Creates a new instance of a Binary Tree
 /// </summary>
 public BinaryTree()
 {
     head = null;
     size = 0;
 }
Exemplo n.º 8
0
 public BinaryTreeNode(T value, BinaryTreeNode <T> leftChild, BinaryTreeNode <T> rightChild)
 {
     this.value      = value;
     this.leftChild  = leftChild;
     this.rightChild = rightChild;
 }