private void InsertIntoPool(BinaryNode <T> nodeToInsert)
 {
     nodeToInsert.LeftNode  = null;
     nodeToInsert.RightNode = null;
     Pool = InsertIntoPoolHelper(poolPosition: Pool, nodeToInsert: nodeToInsert, currPosition: 0);
 }
        private BinaryNode <T> Remove(BinaryNode <T> root, T elementToRemove)
        {
            if (root == null)
            {
                return(root);
            }
            int comparisonResult = root.CompareTo(elementToRemove);

            if (comparisonResult == 0)
            {
                // found the element to remove.
                if (root.LeftNode == null && root.RightNode == null)
                {
                    // both children are null, so removal is simple.
                    if (PoolSize > 0)
                    {
                        InsertIntoPool(nodeToInsert: root);
                    }
                    root = null;
                }
                else if (root.LeftNode != null && root.RightNode != null)
                {
                    // There are two children here, so removal is a bit
                    // more complicated.
                    // checking to see if the right child's left node is null.
                    bool simplePullup = (root.RightNode.LeftNode == null);
                    if (!simplePullup)
                    {
                        BinaryNode <T> newRoot   = DeleteRightMinimum(root: root.RightNode);
                        BinaryNode <T> tempLeft  = root.LeftNode;
                        BinaryNode <T> tempRight = root.RightNode;
                        newRoot.LeftNode  = tempLeft;
                        newRoot.RightNode = tempRight;
                        if (PoolSize > 0)
                        {
                            InsertIntoPool(nodeToInsert: root);
                        }
                        root = newRoot;
                    }
                    else
                    {
                        // Since the right child's left node is null, we can simply
                        // pull up the right child.
                        if (PoolSize > 0)
                        {
                            InsertIntoPool(nodeToInsert: root);
                        }
                        root = root.RightNode;
                    }
                }
                else if (root.LeftNode != null)
                {
                    // Only the Left Child Node exists.
                    if (PoolSize > 0)
                    {
                        InsertIntoPool(nodeToInsert: root);
                    }
                    root = root.LeftNode;
                }
                else if (root.RightNode != null)
                {
                    // Only the Right Child Node exists.
                    if (PoolSize > 0)
                    {
                        InsertIntoPool(nodeToInsert: root);
                    }
                    root = root.RightNode;
                }
            }
            else if (comparisonResult > 0)
            {
                // Look left.
                root.LeftNode = Remove(root: root.LeftNode, elementToRemove: elementToRemove);
            }
            else if (comparisonResult < 0)
            {
                // Look right.
                root.RightNode = Remove(root: root.RightNode, elementToRemove: elementToRemove);
            }
            return(root);
        }
예제 #3
0
 public BinaryNode(T element, BinaryNode <T> leftNode, BinaryNode <T> rightNode)
 {
     Element   = element;
     LeftNode  = leftNode;
     RightNode = rightNode;
 }