// Delete to delete the node with the key from the tree
Delete to delete the node with the key from the tree
// Delete to delete the node with the key from the tree
func (b *BinarySearchTree) Delete(key int) error {
	node := b.searchNode(key)
	if node == nil {
		return fmt.Errorf("key %v does not exist", key)
	}
	if node.leftChild == nil { 
		b.transplant(node, node.rightChild) 
	} else if node.rightChild == nil { 
		b.transplant(node, node.leftChild)
	} else { 
		y := minimumNode(node.rightChild)
		if y != node.rightChild { 
			b.transplant(y, y.rightChild)
			y.rightChild = node.rightChild
			y.rightChild.parent = y
		}
		b.transplant(node, y)
		y.leftChild = node.leftChild
		y.leftChild.parent = y
	}
	b.count--
	return nil
}
func ( b * BinarySearchTree ) Delete ( key int ) error { node := b . searchNode ( key ) if node == nil { return fmt . Errorf ( " " , key ) } if node . leftChild == nil { b . transplant ( node , node . rightChild ) } else if node . rightChild == nil { b . transplant ( node , node . leftChild ) } else { y := minimumNode ( node . rightChild ) if y != node . rightChild { b . transplant ( y , y . rightChild ) y . rightChild = node . rightChild y . rightChild . parent = y } b . transplant ( node , y ) y . leftChild = node . leftChild y . leftChild . parent = y } b . count -- return nil }
Delete to delete the node with the key from the tree
Delete to delete the node with the key from the tree
// only have a rightChild or no child //whatever node.rightChild is nil or not nil // only have a leftChild // both have leftChild and rightChild // Here's the most complex process of binary search tree. // Refer to "Introduction to Algorithms - Third Edition" ch12.3 for more details if necessary. // find successor of current node // The successor of node MUST be in right-sub-tree of current node, // and it will not have a leftChild. // y in node's subTree but not the rightChild of node