Chapter 11: Search Trees
Introduction
Learning Outcomes
Check the four properties of a binary search tree on a given tree.
Write bst_insert and bst_search, both iteratively and recursively.
Write depth recursively, with no extra parameter and no global variable.
Compare four ways to delete a node with no child, one child or two children.
Measure the cost of imbalance; apply the LL, RR, LR and RL rotations of an AVL tree.
Tell BST, AVL, red-black and B-tree apart by where each is used.
Binary Search Tree
Binary search tree
- All keys are distinct; every value is unique.
- Keys in a nonempty left subtree are smaller than the key at the root.
- Keys in a nonempty right subtree are larger than the key at the root.
- The left and right subtrees are binary search trees too.
Binary Search Tree
Which ones are binary search trees?
22 is right of 15, so larger than 15 (fine), but it sits in the left subtree of 18 and is larger than 18 → violates the definition.
Every key left of 95 (80, 50, 2) is smaller than 95; 80's left is 50, 50's left is 2: the order holds.
Every key right of 35 is larger; 40's left holds 38 and 39 (below 40, above 35): valid.
Insertion
Insertion: the tree decides the place
QUESTION: Using the set 3, 5, 7, 9, 11, draw binary search trees of depth 3, 4 and 5. Can you draw one of depth 2?
Solution
Lemma: a tree of depth k has at most 2k−1 nodes; depth 2 holds at most 3, so 5 nodes do not fit. Trees of depth 3, 4, 5 are shown.
Insertion
Does the insertion order matter?
Insert 47 (left) and 26 (right): start at the root, go left if smaller, right if larger, place at the first NULL.
Insert 55 (left) and 17 (right): start at the root, go left if smaller, right if larger, place at the first NULL.
Insert 49 (left) and 47 (right): start at the root, go left if smaller, right if larger, place at the first NULL.
Insert 26 (left) and 93 (right): start at the root, go left if smaller, right if larger, place at the first NULL.
Insert 34 (left) and 84 (right): start at the root, go left if smaller, right if larger, place at the first NULL.
Insert 17 (left) and 52 (right): start at the root, go left if smaller, right if larger, place at the first NULL.
Insert 52 (left) and 34 (right): start at the root, go left if smaller, right if larger, place at the first NULL.
Insert 84 (left) and 55 (right): start at the root, go left if smaller, right if larger, place at the first NULL.
Result: depth 4 and 6. The inorder traversal is the same for both: 17 26 34 47 49 52 55 84 93.
Insertion
Skew, balanced trees and frequently used nodes
- If the keys arrive sorted, the tree leans right or left and its depth becomes n: it is a list.
- If the depth is ≈ lg n, the BST is balanced: insert, delete and search take O(lg n).
- Self-balancing trees: algorithms that rebalance the tree as keys arrive — red-black, AVL.
- Frequently used nodes: algorithms that move often-searched nodes toward the root (“zig-zag”; known in the literature as the splay tree, Sleator–Tarjan).
Insertion
??? — two questions to think about
??? Can you recover the insertion order from the shape of the tree? Is the tree built from a given input unique?
Answer
The same order always gives the same tree (insertion is deterministic). The converse fails: 2,1,3 and 2,3,1 build the same tree. Any order that puts each ancestor before its descendants works; the tree's preorder is always a valid insertion order.
??? Can you recover the tree from its preorder, inorder or postorder sequence?
Answer
For a general binary tree one traversal is not enough; inorder + (preorder or postorder) determines the tree. In a BST the inorder is just the sorted keys, so preorder alone (or postorder) suffices.
Now we can build sample trees with insertion and really run the binary tree routines (traversal, copy, equality).
Insertion
How does insertion work?
- You cannot insert under a node that has both children; the new node goes under a node missing at least one child.
- Search for the new key from the root: go left at a node with a larger key, right at a node with a smaller key.
- If the key is already there, nothing is inserted: duplicates break the definition.
- The new node replaces the first NULL the search pointer reaches, so we need a pointer that trails one step behind (q).
typedef enum { FALSE, TRUE } boolean; typedef struct node *tree_ptr; struct node { tree_ptr left; int data; tree_ptr right; }; /* get_node(): malloc + NULL check, sets both children to NULL */
Insertion · Run the code step by step
bst_insert(50, &L) step by step
p points to the root (47); q (the trailing pointer) is NULL.
q = p = 47: the trailing pointer stays one step behind; we will attach to q.
50 > 47 → it belongs in the right subtree: p = 47->right = 55.
q = p = 55: the trailing pointer stays one step behind; we will attach to q.
50 < 55 → the new node belongs in the left subtree: p = 55->left = 49.
q = p = 49: the trailing pointer stays one step behind; we will attach to q.
50 > 49 → it belongs in the right subtree: p = 49->right = 52.
q = p = 52: the trailing pointer stays one step behind; we will attach to q.
50 < 52 → the new node belongs in the left subtree: p = 52->left = NULL.
p is NULL: the loop ends. The new node takes the empty child slot of q = 52.
A new node is allocated (children NULL) and its data set to 50.
The tree is not empty; 50 < 52 → q->left = r. Linked.
Returns TRUE. Only one pointer changed; the rest of the tree is untouched.
boolean bst_insert(int x, tree_ptr *L) { tree_ptr q = NULL, p = *L, r; // q trails p while (p) { // down to NULL q = p; // q follows p if (x < p->data) p = p->left; else if (x > p->data) p = p->right; else { printf("Duplicate key.\n"); return FALSE; } } r = get_node(); // children NULL r->data = x; if (*L == NULL) *L = r; // empty: root else if (x < q->data) q->left = r; else q->right = r; return TRUE; }
Insertion
Why tree_ptr *L? What does it cost?
- If the tree is empty (
*L == NULL), the new node becomes the root: the caller's root pointer must change. - To change the pointer itself we pass its address (call by reference):
bst_insert(x, &root). - Cost: one comparison per level → Ω(lg n) – O(n).
- C99 fix: initialize
q = NULL.
/* Recursive version: returns the new root */ tree_ptr insert(tree_ptr L, int x) { if (!L) { L = get_node(); L->data = x; } else if (x < L->data) L->left = insert(L->left, x); else if (x > L->data) L->right = insert(L->right, x); return L; /* root = insert(root, x); */ }
Search · Run the code step by step
bst_search(52, L) — iterative
52 > 47 → go right. The left subtree is ruled out.
52 < 55 → go left. The right subtree (faded) is never examined: that is where search gets its power.
52 > 49 → go right. The left subtree is ruled out.
52 == 52: found; return the node's address. 4 comparisons in all.
tree_ptr bst_search(int x, tree_ptr L) { while (L) { // pick a subtree if (x == L->data) return L; if (x < L->data) L = L->left; else L = L->right; } return NULL; // not found }
“When a manager gives a clerk a task, it must be defined precisely. The clerk should never have to come back and knock on the door.”
Search · Run the code step by step
Recursive search: search(34, L)
34 < 47: return search(x, L->left) — whatever it returns is passed up unchanged.
34 > 26: return search(x, L->right).
34 == 34: found; this frame returns L.
The call returned &node(34); search(34, 26) returns it too.
The call returned &node(34); search(34, 47) returns it too.
tree_ptr search(int x, tree_ptr L) { if (!L) return NULL; // absent if (x == L->data) return L; // found if (x < L->data) return search(x, L->left); return search(x, L->right); }
Search
Example: “Does this work?”
if (L) { if (x == L->data) return L; if (x < L->data) return search(x, L->left); return search(x, L->right); } return NULL; /* L == NULL: empty (sub)tree */
Once the if on line 3 is taken, the right-subtree search on line 4 never runs: the call returns whatever the subcall returns.
- In a binary search tree that is exactly what we want: if x is smaller it cannot be on the right; the code is correct and O(h).
- In a general, unordered binary tree x may be in the right subtree; this code misses it.
- For a general tree the fix searches both sides → O(n).
Search
Sample solution: searching any binary tree
tree_ptr search_rec(int data, tree_ptr L) { if (!L) return NULL; /* empty tree? */ if (L->data == data) return L; /* key found */ tree_ptr T = search_rec(data, L->left); return T ? T : search_rec(data, L->right); }
- Search the left first; if not found there (T == NULL), search the right.
- The ordering is not used → O(n) worst case; needlessly slow on a BST.
- The solution to the find-weight exam question uses this routine.
Depth
QUESTION: write depth iteratively
/* iterative, no globals: walk level by level, count levels */ int depth_iter(tree_ptr L) { tree_ptr k[MAX_SIZE]; int front = 0, rear = 0, d = 0; if (L) k[rear++] = L; while (front < rear) { /* one level */ int last = rear; d++; for (; front < last; front++) { if (k[front]->left) k[rear++] = k[front]->left; if (k[front]->right) k[rear++] = k[front]->right; } } return d; /* needs MAX_SIZE >= n */ }
Depth
Why no global variables?
Input through parameters, output through the return value; no hidden state.
A routine may declare another routine's prototype locally and call it; the definition stays outside (standard C has no nested functions).
Each call has its own local left/right variables; no global counter is needed.
Depth · Run the code step by step
Recursive depth: {left, right} frames
L = A: first the depth of the left subtree: call depth(B).
L = B: first the depth of the left subtree: call depth(D).
L = D: left = 0, right = 0 (NULL → 0) → MAX + 1 = 1 returned; locals left/right are freed.
L = B: left = 1, right = 0 (NULL → 0) → MAX + 1 = 2 returned; locals left/right are freed.
left = 2; now depth(C).
L = C: first the depth of the left subtree: call depth(E).
L = E: first the depth of the left subtree: call depth(F).
L = F: left = 0, right = 0 (NULL → 0) → MAX + 1 = 1 returned; locals left/right are freed.
L = E: left = 1, right = 0 (NULL → 0) → MAX + 1 = 2 returned; locals left/right are freed.
L = C: left = 2, right = 0 (NULL → 0) → MAX + 1 = 3 returned; locals left/right are freed.
L = A: left = 2, right = 3 → MAX + 1 = 4 returned; locals left/right are freed.
int depth(tree_ptr L) { int left, right; if (L) { // pointer NULL? left = depth(L->left); right = depth(L->right); return MAX(left, right) + 1; } else return 0; // reached past a leaf }
Depth
Depth: is the problem small enough yet?
- The depth of a tree is one more than the deeper of its left and right subtrees:
MAX(left, right) + 1. - Each node has its own 2 locals: the left and right depth. Calls made with NULL free their locals without using them.
- Time O(n), space O(h).
#define MAX(a,b) ((a) > (b) ? (a) : (b))
int depth(tree_ptr L) { /* one line */ return !L ? 0 : MAX(depth(L->left), depth(L->right)) + 1; }
The MAX macro evaluates its arguments twice; in this one-liner the recursive calls run twice → exponential time! So use a max() function instead of the macro (as in the exam solution).
Depth
On adding extra parameters
— Alan J. Perlis
Question: “Sir, can we add an extra parameter, int depth(tree_ptr L, int a), and call it as depth(L, 0)?”
Deletion
Deleting from a binary search tree
- Deletion must preserve the binary search tree property.
- No children (leaf): set the parent's pointer to NULL.
- One child: the parent's pointer now points to that only child.
- Two children: there are several methods; let's delete 60 from the sample tree.
Sample tree: built by inserting 40, 20, 60, 10, 30, 50, 70, 42, 55, 65, 73, 52, 57, 66, 56 in that order (depth 6).
Deletion
Method 1: the largest key in the left subtree
The largest key in the left subtree: follow the right pointers inside it; the node with no right child (57) is the largest.
That node may have a left subtree (57 has 56). This left child must move to the right link of the moved node's parent (55 → 56).
Second way: 60 is not freed; 57 is written into its data field and only 55's right link is set to 56 → 1 link. The first way (moving the node) needs 4 links: 40→57, 55→56, 57.left→50, 57.right→70.
Deletion · Run the code step by step
bst_delete(60, &L): a node with two children
p is the root; link holds the address of the root pointer, so we can change the link leading to the node we delete.
60 > 40 → link = &40->right, p = 60.
Found 60, and it has two children (50, 70) → find the largest key in the left subtree.
b = &60->left → *b = 50.
*b has a right child → b = &50->right, *b = 55.
*b has a right child → b = &55->right, *b = 57.
57 has no right child: the largest key in the left subtree is 57.
57 is written into 60's data field (the node does not move: method 1, second way).
Now the old 57 node is the one to delete; link = &55->right.
*link = 57's only child (56): 55's right link now points to 56.
The old node is freed. Still a binary search tree; depth 6 → 5.
void bst_delete(int x, tree_ptr *L) { tree_ptr p = *L, *link = L; // link to p while (p && p->data != x) { // find x link = (x < p->data) ? &p->left : &p->right; p = *link; } if (!p) return; // x absent if (p->left && p->right) { // 2 children tree_ptr *b = &p->left; // left subtree's while ((*b)->right) // largest b = &(*b)->right; // node p->data = (*b)->data; // move the data link = b; p = *b; // delete it } *link = p->left ? p->left : p->right; free(p); }
Deletion
Four methods and their effect on depth
Method 1: 57 moves up, 56 goes right of 55. Depth drops to 5.
Method 2: the smallest key of the right subtree, 65, moves up; its right child 66 hangs left of 70. Depth 6.
Method 3: the right child (70) takes the deleted node's place; subtree 50 hangs left of the smallest node of subtree 70 (65). Depth 7.
Method 4: the left child (50) takes its place; subtree 70 hangs right of the largest node of subtree 50 (57). Depth 7 (the path 40-50-55-57-70-65-66 has 7 nodes).
Balanced Trees
The cost of imbalance
| n | balanced: ⌈lg(n+1)⌉ | skewed: n |
|---|---|---|
| 1.000 | 10 | 1.000 |
| 1,000,000 | 20 | 1,000,000 |
| 2,000,000 | 21 | 2,000,000 |
| 109 | 30 | 109 |
- A BST built from n keys in random order has average depth Θ(lg n); an average search takes ≈ 1.39 lg n comparisons (Sedgewick; CLRS 12.4).
- But the worst case (sorted or nearly sorted input, which is common in practice) is Θ(n).
- The fix: trees that keep depth O(lg n) with local rotations after each insert or delete: AVL, red-black, B-tree.
Balanced Trees
AVL tree (Adelson-Velsky and Landis, 1962)
- At every node the balance factor = height(left) − height(right) ∈ {−1, 0, +1}.
- Height is at most ≈ 1.44 lg(n+2): search, insert and delete in O(lg n).
- Insert as in an ordinary BST, then update the balance factors on the path back to the root.
- Find the first ancestor with factor ±2; which grandchild subtree got the new node gives 4 cases: LL, RR, LR, RL.
- After an insertion, a single (LL/RR) or double (LR/RL) rotation restores balance.
typedef struct avl *avl_ptr; struct avl { int data; int ht; /* subtree height */ avl_ptr left, right; }; int ht(avl_ptr t) { return t ? t->ht : 0; } int balance(avl_ptr t) { return ht(t->left) - ht(t->right); }
Balanced Trees
Single rotation: LL and RR
LL: the new node went into the left subtree of z's left child (y); z's factor is +2. Rotate z right: y moves up and y's right subtree T3 becomes z's left. Order is kept: T1 x T2 y T3 z T4.
RR is the mirror case: rotate z left; y's left subtree (T2) becomes z's right. In both cases the subtree's height returns to its value before the insertion.
Balanced Trees
Double rotation: LR and RL
LR: the new node is in the right subtree of z's left child (y). A single right rotation does not help; first rotate y left → LL shape.
Then rotate z right: grandchild x rises above both ancestors. RL is the mirror: first the right child to the right, then z to the left.
Balanced Trees · Run the code step by step
rot_right(): two pointers in the LL case
Inserted 5: 30's factor is ht(20)=3 − ht(40)=1 = +2, left-left heavy → LL: rot_right(30).
k1 = k2->left = 20: the node that will be the new root.
k2->left = k1->right: 25 moves to 30's left (20 < 25 < 30, order kept). For now two pointers point to 25.
k1->right = k2: 30 becomes 20's right child. Links done; let's redraw →
Heights bottom-up: first k2 (30): 1 + MAX(1, 1) = 2.
Then k1 (20): 1 + MAX(ht(10)=2, 2) = 3. The subtree's height is 3, as before the insertion.
The new root k1 is returned; the caller links the parent's pointer to it. Only 2 pointers changed: O(1).
avl_ptr rot_right(avl_ptr k2) { // LL
avl_ptr k1 = k2->left;
k2->left = k1->right;
k1->right = k2;
k2->ht = 1 + MAX(ht(k2->left), ht(k2->right));
k1->ht = 1 + MAX(ht(k1->left), k2->ht);
return k1; // new root
}Balanced Trees
Inserting 3, 2, 1, 4, 5, 6, 7, 16, 15 into an AVL tree
3 inserted. All factors in −1..+1: no rotation needed.
2 inserted. All factors in −1..+1: no rotation needed.
1 inserted. Imbalance → LL (rotate right at 3).
4 inserted. All factors in −1..+1: no rotation needed.
5 inserted. Imbalance → RR (rotate left at 3).
6 inserted. Imbalance → RR (rotate left at 2).
7 inserted. Imbalance → RR (rotate left at 5).
16 inserted. All factors in −1..+1: no rotation needed.
15 inserted. Imbalance → RL (right at 16, left at 7).
Inserted into a plain BST, the same sequence would reach depth 7 because of the chain 3–4–5–6–7–16–15; the AVL tree has depth 4. (Example: Weiss, Data Structures and Algorithm Analysis in C, 4.4)
Balanced Trees
AVL insertion: BST insertion + rebalancing on the way back
avl_ptr avl_insert(avl_ptr t, int x) { if (!t) return new_avl(x); /* malloc, ht = 1 */ if (x < t->data) t->left = avl_insert(t->left, x); else if (x > t->data) t->right = avl_insert(t->right, x); else return t; /* no duplicate keys */ t->ht = 1 + MAX(ht(t->left), ht(t->right)); int d = balance(t); if (d > 1 && x < t->left->data) return rot_right(t); /* LL */ if (d < -1 && x > t->right->data) return rot_left(t); /* RR */ if (d > 1) { t->left = rot_left(t->left); return rot_right(t); } /* LR */ if (d < -1) { t->right = rot_right(t->right); return rot_left(t); } /* RL */ return t; }
- Every ancestor is updated as the recursion unwinds; one (single or double) rotation is enough for an insertion.
- Usage:
root = avl_insert(root, x);— the new root is returned, so*Lis not needed. Deletion is similar but may need O(lg n) rotations.
Balanced Trees
A quick look: red-black trees and B-trees
- Every node is red or black; the root and the NULL leaves are black.
- The children of a red node are black.
- Every path from a node down to the NULLs has the same number of black nodes.
- Result: height ≤ 2 lg(n+1); at most 2–3 rotations plus recoloring per insert/delete.
- C++
std::map, JavaTreeMap, the Linux kernel.
- A balanced m-way search tree: ⌈m/2⌉−1 … m−1 keys per node.
- All leaves are on the same level.
- A full node splits, pushing its middle key up to the parent; the tree grows at the root.
- One node = one disk block → very few disk accesses: database indexes, file systems (B+ tree).
Balanced Trees
Which search tree?
| structure | search / insert / delete (worst) | balance condition | used where? |
|---|---|---|---|
| binary search tree | O(n) (average O(lg n)) | none | small, random data; teaching |
| AVL | O(lg n) | |height(left) − height(right)| ≤ 1 | search-heavy workloads |
| red-black | O(lg n) | equal black height | library maps |
| B-tree / B+ | O(logm n) disk accesses | leaves on one level | databases, file systems |
| splay (“zig-zag”) | O(lg n) amortized | none; accessed node moves to root | frequently accessed items |
Exam Questions
Exam question: the weight of a node
In a binary tree with no duplicate keys, the weight of the node holding x is “the longest of the paths down to the leaves”. If x is absent or L = NULL, it is 0. Write int FindWeight(tree_ptr L, int x). Do not use global variables.
Sample solution
int FindWeight(tree_ptr L, int x) { /* tree_search: node holding x, or NULL */ tree_ptr tree_search(tree_ptr L, int x); int depth(tree_ptr L); return depth(tree_search(L, x)); } tree_ptr tree_search(tree_ptr L, int x) { if (!L) return NULL; if (L->data == x) return L; tree_ptr T = tree_search(L->left, x); return T ? T : tree_search(L->right, x); } int depth(tree_ptr L) { int max(int a, int b); /* not smaller than a, b */ return L ? max(depth(L->left), depth(L->right)) + 1 : 0; }
Design: a composition of two well-defined routines; since depth(NULL) = 0, “not found” needs no special case.
Exam Questions
Exam question: how many times does FindWeight run?
int FindWeight(item_ptr L, int x) { if (!L) return 0; else if (L->data == x) return weight(L); if (FindWeight(L->left, x) > FindWeight(L->right, x)) FindWeight(L->left, x); if (FindWeight(L->right, x) > FindWeight(L->left, x)) FindWeight(L->right, x); return 0; }
Tree: root 2, left 10, right 20. i) How many calls for x = 20 and x = 30? ii) Write and solve the recurrence for the worst case.
Solution
i) x = 20: 14, x = 30: 21. Each call on a leaf makes 1 + 4 NULL calls = 5 calls; for x = 30, the root gives 1 + 4·5 = 21.
ii) If x is not in the tree, each call calls each subtree twice. On a skewed tree T(n) = 2T(n−1) + c → Θ(2n); on a balanced tree T(n) = 4T(n/2) + c → Θ(n2). The right design (the find-weight solution) is O(n).
Exam Questions
Exam question: SWAP between the two recursive calls
Run proc on the BST built from {44, 30, 15, 18, 86, 97, 65, 113, 74, 129}; redraw the tree each time the SWAP line changes it; interpret the final tree.
void proc(tree_ptr L) { tree_ptr p; if (L) { proc(L->left); SWAP(L->left, L->right, p); proc(L->right); } }
Solution (step by step)
Start: the binary search tree built from 44, 30, 15, 18, 86, 97, 65, 113, 74, 129.
SWAP at 15: left and right swapped. Then the “right” subtree (the former left) is processed.
SWAP at 30: left and right swapped. Then the “right” subtree (the former left) is processed.
SWAP at 15: left and right swapped. Then the “right” subtree (the former left) is processed.
SWAP at 44: left and right swapped. Then the “right” subtree (the former left) is processed.
SWAP at 30: left and right swapped. Then the “right” subtree (the former left) is processed.
Final tree: the root's two subtrees swapped, but the subtrees moved to the right (the old left ones) were processed twice, so it is not a full mirror.
Interpretation: with SWAP before or after both calls we would get the exact mirror; in between, the left child moves right with SWAP and is processed once more for the same node.
Lab · C code step by step
Lab: Binary Search Tree
Summary
Complexity summary
| operation | best / balanced | worst (skewed) | extra space |
|---|---|---|---|
| bst_search (iterative) | O(lg n) | O(n) | O(1) |
| search (recursive) | O(lg n) | O(n) | O(h) stack |
| bst_insert | Ω(lg n) | O(n) | O(1) |
| bst_delete | O(lg n) | O(n) | O(1) |
| depth | O(n) | O(n) | O(h) |
| search in a general binary tree | O(n) | O(n) | O(h) |
| AVL search / insert / delete | O(lg n) | O(lg n) | O(lg n) recursion |
| single rotation | O(1) | O(1) | — |
Summary
Common mistakes
*L, not tree_ptr L).returning the subcall's result in a recursive search.MAX macro and ending up with exponential time.Summary
Exercises
- Insert 50, 30, 70, 20, 40, 60, 80, 35 in this order into an empty BST; write its preorder.
- Delete 30 from the same tree with methods 1 and 2; compare the depths.
- Write iterative routines that return the smallest and the largest key.
- Write an O(n) routine that checks whether a binary tree is a BST.
- Insert 10, 20, 30, 25, 28 into an empty AVL tree; which rotations happen?
- Write a routine that counts the keys smaller than x (an inorder traversal helps).
Hints / answers
- Preorder: 50 30 20 40 35 70 60 80.
- Method 1: 20 moves up (depth stays 4); method 2: 35 moves up, 40's left is NULL (depth 3).
- Smallest: keep following
left; largest: keep followingright. - Pass each node its allowed (low, high) range as parameters; comparing with the children alone is not enough.
- RR at 30 (20 becomes root); LR at 28: 30's left is 25, first left at 25, then right at 30 → 20(10, 28(25, 30)).
- Count up to x during an inorder traversal, or store subtree sizes in the nodes.
Summary
Chapter summary
Left subtree smaller, right larger, keys unique; inorder = sorted.
Insert at the first NULL; trailing pointer q, and *L for the empty tree.
Each comparison rules out one subtree: O(h).
MAX(left, right) + 1; no globals, no needless parameters.
0/1 child is easy; with 2 children swap in the predecessor/successor's data (1 link).
Sorted input gives O(n); AVL/red-black guarantee O(lg n).
Summary
References and next steps
- Horowitz–Sahni–Anderson-Freed, Fundamentals of Data Structures in C, 5.7 (BST), 10.2 (AVL).
- Cormen et al., Introduction to Algorithms, Chapters 12 (BST), 13 (red-black), 18 (B-tree).
- M. A. Weiss, Data Structures and Algorithm Analysis in C, 4.4 (AVL); R. Sedgewick, Algorithms in C, Chapters 12–13.