Пример #1
0
        public bool Delete(int id)
        {
            var current     = root;
            var parent      = root;
            var isLeftChild = true;

            while (current.iData != id)
            {
                parent = current;

                if (current.iData < id)
                {
                    isLeftChild = true;
                    current     = current.leftChild;
                }
                else
                {
                    isLeftChild = false;
                    current     = current.rightChild;
                }

                if (current == null) // not in the tree
                {
                    return(false);
                }
            }

            // CASE 1 : LEAF NODE
            if (current.leftChild == null && current.rightChild == null)
            {
                if (current == root)
                {
                    root = null;
                }
                else if (isLeftChild)
                {
                    parent.leftChild = null;
                }
                else
                {
                    parent.rightChild = null;
                }
            }
            else if (current.leftChild == null) // this if will execute if the node to be deleted has a right child but no left
            {
                // basically if the node we want to delete has a right subtree then set parent node to now equal the
                // right subtree rather than the current node (which is being deleted)

                if (current == root)
                {
                    root = current.rightChild;
                }
                else if (isLeftChild)
                {
                    parent.leftChild = current.rightChild;
                }
                else
                {
                    parent.rightChild = current.rightChild;
                }
            }
            else if (current.rightChild == null)
            {
                if (current == root)
                {
                    root = current.leftChild;
                }
                else if (isLeftChild)
                {
                    parent.leftChild = current.leftChild;
                }
                else
                {
                    parent.rightChild = current.leftChild;
                }
            }
            else  // two children, so replace with inorder successor
            {
                var successor = GetSuccessor(current);

                if (current == root)
                {
                    root = successor;
                }
                else if (isLeftChild)
                {
                    parent.leftChild = successor;
                }
                else
                {
                    parent.rightChild = successor;
                }

                // connect successor to current's left child
                successor.leftChild = current.leftChild;
            }

            return(true);
        }