Binary Search Tree (BST)
About
A Binary Tree in which:
The left child contains values smaller than the parent.
The right child contains values greater than the parent.
Searching an element
Runs on O(logN) up to O(N) in the worst case, depending on how the Tree structure is.
Inserting an element
Just look for the place that satisfy BST conditions and add.
Will eventually unbalance the tree.
Deleting an element
Deleting is bit more complicated and have 3 separate cases to look for:
Case 1: The deleted node X has no children, so AncestorX just points to
undefined.Case 2: The deleted node X has only 1 child, so AncestorX points to X child.
Case 3: The deleted node X has both children, so AncestorX will point to either:
The largest decendent node of Xleft child. (largest on small side)
The smallest decendent node of Xright child. (smallest on large side)
Having a height property on each node, helps choosing in Case 3 which child has the highest height.
And choosing the child with the highest helps pruning the tree and reducing its height.
Last updated