Introduction
A Binary Search Tree (BST) is a type of binary tree where each node satisfies the BST property:
- Left Subtree: All values in the left subtree are less than the node’s value.
- Right Subtree: All values in the right subtree are greater than the node’s value.
- This rule applies recursively to every node in the tree.
BSTs are particularly useful when we need to store data in a way that supports efficient ordering and retrieval.
Structure
Example BST:
Here:
- Root:
50 - Left subtree:
30 → {20, 40} - Right subtree:
70 → {60, 80}
Number of Nodes
If a BST has h height (root at height 0):
- Minimum number of nodes: (completely skewed)
- Maximum number of nodes: (perfectly balanced)
For a BST that is full and balanced (every level completely filled):