예제 #1
0
        private bool DeleteInternal(T data, BinarySearchTreeNode <T> currentNode)
        {
            if (currentNode.Data.Equals(data))
            {
                // if there is no parent or children, then there's only 1 node in the BST, and we can simply delete it
                if (currentNode.LeftChild == null && currentNode.RightChild == null && currentNode.Parent == null)
                {
                    this._root = null;
                }
                if (currentNode.Parent == null)
                {
                    if (currentNode.LeftChild != null)
                    {
                        this._root = currentNode.LeftChild;
                        currentNode.LeftChild.Parent = null;

                        if (currentNode.RightChild != null)
                        {
                            this.InsertInternal(currentNode.RightChild, currentNode.LeftChild);
                        }
                    }
                    else if (currentNode.RightChild != null)
                    {
                        this._root = currentNode.RightChild;
                        currentNode.RightChild.Parent = null;
                    }
                    return(true);
                }
                bool currentNodeIsLeftChild = currentNode.Parent.LeftChild == currentNode;
                if (currentNode.LeftChild != null)
                {
                    if (currentNodeIsLeftChild)
                    {
                        currentNode.Parent.LeftChild = currentNode.LeftChild;
                    }
                    else
                    {
                        currentNode.Parent.RightChild = currentNode.LeftChild;
                    }
                    currentNode.LeftChild.Parent = currentNode.Parent;

                    if (currentNode.RightChild != null)
                    {
                        this.InsertInternal(currentNode.RightChild, this._root);
                    }
                }
                else if (currentNode.RightChild != null)
                {
                    if (currentNodeIsLeftChild)
                    {
                        currentNode.Parent.LeftChild = currentNode.RightChild;
                    }
                    else
                    {
                        currentNode.Parent.RightChild = currentNode.RightChild;
                    }
                    currentNode.RightChild.Parent = currentNode.Parent;
                }
                else
                {
                    // this is a leaf node
                    if (currentNodeIsLeftChild)
                    {
                        currentNode.Parent.LeftChild = null;
                    }
                    else
                    {
                        currentNode.Parent.RightChild = null;
                    }
                }
                return(true);
            }
            else if (data.CompareTo(currentNode.Data) < 1)
            {
                if (currentNode.LeftChild != null)
                {
                    return(this.DeleteInternal(data, currentNode.LeftChild));
                }
                return(false);
            }
            else
            {
                if (currentNode.RightChild != null)
                {
                    return(this.DeleteInternal(data, currentNode.RightChild));
                }
                return(false);
            }
        }
예제 #2
0
 public BinarySearchTree()
 {
     this._root = null;
 }