Overview
A binary search tree (BST) is a binary tree satisfying the binary-search-tree property:
Let be a node in a binary search tree. If is a node in the left subtree of , then . If is a node in the right subtree of , then .
Traversals
Consider an arbitrary node of some BST. Then:
- An inorder traversal visits ‘s left child, then , then ‘s right child.
- A preorder traversal visits , then ‘s left child, then ‘s right child.
- A postorder traversal visits ‘s left child, then ‘s right child, then .
Successors
The successor of a node in a binary search tree is the node whose value would appear immediately after in an in-order traversal.
Predecessors
The predecessor of a node in a binary search tree is the node whose value would appear immediately before in an in-order traversal.
Deletions
Consider deleting node from a BST. There are three conceptual cases to consider corresponding to the number of children has:
- If has no children we can just replace with
NIL
. - If has one child, we can replace with its child.
- If has two children, we swap with either its predecessor or successor, updating pointers as necessary to maintain the binary-search-tree property.
Bibliography
- Donald Ervin Knuth, Art of Computer Programming, 3: Sorting and Searching, 2. ed., 34. (Reading, Mass: Addison-Wesley, 1995).
- Thomas H. Cormen et al., Introduction to Algorithms, Fourth edition (Cambridge, Massachusett: The MIT Press, 2022).