Exemple #1
0
        protected int RecHeight(Node <T> nCurrent)
        {
            int iHeightLeft  = 0;
            int iHeightRight = 0;

            // if the current node is not a leaf
            if (!nCurrent.IsLeaf())
            {
                // the current node has a left node, check it
                if (nCurrent.Left != null)
                {
                    iHeightLeft = RecHeight(nCurrent.Left) + 1;
                }

                // the current node has a right node, check it
                if (nCurrent.Right != null)
                {
                    iHeightRight = RecHeight(nCurrent.Right) + 1;
                }
            }

            // When we get them to the leaf, compare the two heights.
            // If the left is greater, return it, else return the right.
            // If they are the same, then returning the right is the same as returning the left.
            return(iHeightLeft > iHeightRight ? iHeightLeft : iHeightRight);
        }
 public bool IsLeaf()
 {
     if (!IsEmpty())
     {
         return(root.IsLeaf(ref root));
     }
     return(true);
 }
Exemple #3
0
        private Node <T> RecRemove(Node <T> nCurrent, T data, ref bool wasRemoved)
        {
            T substitute = default(T);

            if (nCurrent != null)
            {
                // Find item to remove
                int compare = data.CompareTo(nCurrent.Data);

                // if data item to remove is smaller than the current data
                if (compare < 0)
                {
                    nCurrent.Left = RecRemove(nCurrent.Left, data, ref wasRemoved);
                }
                else if (compare > 0)
                {
                    nCurrent.Right = RecRemove(nCurrent.Right, data, ref wasRemoved);
                }
                else // else we are on the item to remove --- YAY!
                {
                    // Indicate that we found it
                    wasRemoved = true;

                    // Check the if current node is a leaf (base case)
                    if (nCurrent.IsLeaf())
                    {
                        // reduce the count of the tree
                        nCurrent = null;

                        // set its reference to null
                        iCount = iCount - 1;
                    }
                    // Else it is not a leaf
                    else
                    {
                        if (nCurrent.Left != null)
                        {
                            substitute    = RecFindLargest(nCurrent.Left);
                            nCurrent.Data = substitute;
                            nCurrent.Left = RecRemove(nCurrent.Left, substitute, ref wasRemoved);
                        }
                        else
                        {
                            return(nCurrent = nCurrent.Right);


                            //substitute = RecFindLargest(nCurrent.Right);
                            //nCurrent.Data = substitute;
                            //nCurrent.Right = RecRemove(nCurrent.Right, substitute, ref wasRemoved);
                        }
                    }
                }
            }

            return(nCurrent);
        }
Exemple #4
0
        /// <summary>
        ///     Substitute = default
        ///     if current is not null
        ///         compare = Compare data with current nodes data
        ///         if Data is less than currentData
        ///             Recursively remove value from current's left subtree
        ///         else
        ///             if Data is greater than currentData
        ///                 Recursively remove value from current's right subtree
        ///             else
        ///                 Remove the value
        ///                 wasRemoved = true
        ///                 if current is a leaf
        ///                     decrement treeCount
        ///                     set current to null
        ///                 else if its not a leaf
        ///                     if current's left subtree exists
        ///                         substitute = largest value in current's left subtree
        ///                         currentData = substitute
        ///                         currentLeft = Recursively Remove substitute from current's left subtree
        ///                     else current's right subtree must exist
        ///                         substitute = smallest value in current's right subtree
        ///                         currentData = substitute
        ///                         currentRight = Recursively remove substitute from current's right subtree
        ///                return current
        /// </summary>
        /// <param name="nCurrent"></param>
        /// <param name="data"></param>
        /// <param name="bRemoved"></param>
        /// <returns></returns>
        private Node <T> RecRemove(Node <T> nCurrent, T data, ref bool bRemoved)
        {
            T subData = default(T);

            if (nCurrent != null)
            {
                int iCompare = data.CompareTo(nCurrent.Data); //Compares data we're looking for against current data

                if (iCompare < 0)                             //If data we're looking for is less than current, recurse left
                {
                    nCurrent.Left = RecRemove(nCurrent.Left, data, ref bRemoved);
                }
                else
                {
                    if (iCompare > 0)   //If data we're looking for is greater than current, recurse right
                    {
                        nCurrent.Right = RecRemove(nCurrent.Right, data, ref bRemoved);
                    }
                    else //if iCompare == 0, or data equals current data
                    {
                        nCurrent.Data = default(T);
                        bRemoved      = true;

                        if (nCurrent.IsLeaf())  //If nCurrent is a leaf node
                        {
                            --this.iCount;      //Reduces tree's count
                            nCurrent = null;    //Sets nCurrent to null, removes the node
                        }
                        else
                        {
                            //Current node has two children, requires a substitute value
                            if (nCurrent.Left != null && nCurrent.Right != null)
                            {
                                subData       = RecFindLargest(nCurrent.Left); //Locates a substitute value from left tree
                                nCurrent.Data = subData;
                                nCurrent.Left = RecRemove(nCurrent.Left, subData, ref bRemoved);
                            }
                            //Else if the left child exists
                            else
                            {
                                if (nCurrent.Left != null)
                                {
                                    nCurrent = nCurrent.Left;
                                    this.iCount--;
                                }
                                //Only right child exists
                                else
                                {
                                    nCurrent = nCurrent.Right;
                                    this.iCount--;
                                }
                            }

                            //if (nCurrent.Left != null)
                            //{
                            //    subData = RecFindLargest(nCurrent.Left);
                            //    nCurrent.Data = subData;
                            //    nCurrent.Left = RecRemove(nCurrent.Left, subData, ref bRemoved);
                            //    nCurrent.Left = Balance(nCurrent.Left);


                            //}
                            //else
                            //{
                            //    subData = RecFindSmallest(nCurrent.Right);
                            //    nCurrent.Data = subData;
                            //    nCurrent.Right = RecRemove(nCurrent.Right, subData, ref bRemoved);
                            //    nCurrent.Right = Balance(nCurrent.Right);
                            //}
                        }
                    }
                }
            }
            return(nCurrent);
        }
Exemple #5
0
        /// <summary>
        /// Removes the data.
        /// </summary>
        /// <returns><c>true</c>, if data was removed, <c>false</c> otherwise.</returns>
        /// <param name="node">Node.</param>
        /// <param name="data">Data.</param>
        public bool RemoveData(ref Node node, int data)
        {
            if (node == null)
                throw new ArgumentNullException("Node");

            if (node.Value == data)
            {
                if (node.IsLeaf(node))
                {
                    node = null;
                    return true;
                }
                if (node._left == null || node._right == null)
                {
                    node = node._left ?? node._right;
                    return true;
                }

                var temp = node._left;
                while (temp._right != null)
                    temp = temp._right;
                temp._right = node._right;
                node = node._left;
                return true;
            }
            else if (node.Value < data)
            {
                return RemoveData(ref node._right, data);
            }
            else if (node.Value > data)
            {
                return RemoveData(ref node._left, data);
            }
            return false;
        }