Binary Search Tree
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
- 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
- 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.