Loading lessons...
Binary Search Trees (BST)
Binary Search Trees (BST)
A Binary Search Tree (BST) is a binary tree that satisfies the BST Ordering Property at every node N:
- Every key in N's left subtree is strictly less than N.key.
- Every key in N's right subtree is greater than N.key.
- Both left and right subtrees are themselves BSTs.
1. Search, Min/Max & Insertion
- Search (
find): Start at root; ifkey == root->datareturn node; go left ifkey < root->data, right otherwise. - Find Minimum / Maximum:
- Minimum: Follow
leftpointers untilnode->left == NULL. - Maximum: Follow
rightpointers untilnode->right == NULL.
- Insertion: Search for
keyuntil reachingNULLand attach the new leaf node at that exact position.
struct BSTNode* insertBST(struct BSTNode* root, int key) {
if (!root) return createBSTNode(key);
if (key < root->data) root->left = insertBST(root->left, key);
else if (key > root->data) root->right = insertBST(root->right, key);
return root;
}Build Trace: Inserting [45, 15, 79, 90, 10, 55, 12, 20, 50]:
45is root →15goes left,79goes right →90goes right of79→10left of15,55left of79→12right of10,20right of15,50left of55.- Inorder Traversal:
10, 12, 15, 20, 45, 50, 55, 79, 90(always sorted!).
2. BST Deletion (3 Cases)
- Case 1 (Leaf Node, 0 children): Remove the node and set parent's link to
NULL. (E.g., delete12above). - Case 2 (1 Child): Bypass the node by connecting its parent directly to its single child, then
free()the node. (E.g., delete10→15->leftbecomes12). - Case 3 (2 Children): Replace the node's key with either:
- Inorder Successor: Smallest key in its right subtree (leftmost node of right child), OR
- Inorder Predecessor: Largest key in its left subtree (rightmost node of left child).
Then recursively delete that successor/predecessor (which is guaranteed to have at most 1 child!). (E.g., delete 45 → replace with Inorder Successor 50, then delete leaf 50.)
struct BSTNode* deleteNode(struct BSTNode* root, int key) {
if (!root) return NULL;
if (key < root->data) root->left = deleteNode(root->left, key);
else if (key > root->data) root->right = deleteNode(root->right, key);
else {
if (!root->left) { struct BSTNode* t = root->right; free(root); return t; }
if (!root->right) { struct BSTNode* t = root->left; free(root); return t; }
struct BSTNode* succ = root->right;
while (succ->left) succ = succ->left;
root->data = succ->data;
root->right = deleteNode(root->right, succ->data);
}
return root;
}3. Complexity Analysis
- Average Case (Balanced): O(log n) for Search, Insert, Delete.
- Worst Case (Skewed Tree): O(n) - occurs when keys are inserted in sorted (or reverse-sorted) order, degenerating the BST into a linked list!
Worked Build from the Notes
Insert 39, 27, 45, 18, 29, 40, 54, 28, 43, 41, 54, 60 (duplicates go to the right, since the right subtree holds values ≥ the root):
39
/ \
27 45
/ \ / \
18 29 40 54
/ \ \
28 43 54
/ \
41 60Inorder: 18 27 28 29 39 40 41 43 45 54 54 60-sorted, which confirms the BST property.
BST Algorithms in Pseudocode (Gilberg & Forouzan style)
Algorithm findSmallestBST(root)
if root.left is null: return root
return findSmallestBST(root.left)
Algorithm findLargestBST(root)
if root.right is null: return root
return findLargestBST(root.right)
Algorithm searchBST(root, target)
if root is null: return null // not found
if target < root.key: return searchBST(root.left, target)
if target > root.key: return searchBST(root.right, target)
return root // found
Algorithm addBST(root, newNode)
if root is null: root = newNode; return root
if newNode.key < root.key: root.left = addBST(root.left, newNode)
else: root.right = addBST(root.right, newNode)
return root
Algorithm deleteBST(root, dltKey)
if root is null: return false
if dltKey < root.key: return deleteBST(root.left, dltKey)
if dltKey > root.key: return deleteBST(root.right, dltKey)
// node found
if root.left is null: root = root.right // 0 or 1 child
else if root.right is null: root = root.left
else: // 2 children
dltPtr = findLargestBST(root.left) // inorder predecessor
root.key = dltPtr.key
deleteBST(root.left, dltPtr.key)
return trueThe notes replace a two-child node by the largest node of its left subtree (inorder predecessor); using the smallest of the right subtree (inorder successor) is equally correct.
Deleting 45 from the tree above: 45 has two children; the largest key in its left subtree is 43. Copy 43 into 45's node, then delete the old 43 (which has one child, 41, so 41 moves up to take its place).
📝 IPU End-Term Exam Corner (5-10 Marks)
- Q1. Define Binary Search Tree (BST). Write C functions for Insertion and Deletion (all 3 cases) in a BST. (10 Marks)
- Write: BST ordering invariant,
insertBST(), and all 3 deletion cases (0 children, 1 child, 2 children using Inorder Successor/Predecessor) withdeleteNode()C code. - Q2. Construct a BST for
[45, 15, 79, 90, 10, 55, 12, 20, 50]and write its Inorder traversal. (5 Marks) - Write: Step-by-step tree diagram and sorted Inorder output (
10, 12, 15, 20, 45, 50, 55, 79, 90).