Chapter 11: Search Trees
Multiple Choice · Binary Search Tree
Question 1: An Impossible Preorder Sequence
Which of the following sequences, read as the preorder traversal of a binary search tree, cannot occur?
C: Since 50 > 40, 50 starts the right subtree; every value after it must be greater than 40, yet 30 comes. In a preorder sequence the root is written first, then all the smaller values (left subtree), then all the larger ones. D is valid: 30 is to the right of 20, and 25 is to the left of 30.
Classic Question · Insertion
Question 2: A Tree from an Insertion Order
Into an empty binary search tree, using bst_insert, the values 41, 20, 65, 11, 29, 50, 91, 32, 72, 99 are inserted in this order. Draw the tree. What is the depth of the tree? What is its postorder sequence?
Show answer
Each value starts at the root and goes left if it is smaller and right if it is larger, and is placed at the first NULL: 32 follows the path 41 → 20 → 29 and goes to the right of 29; 72 follows 41 → 65 → 91 and goes to the left of 91.
Depth 4 (paths 41–20–29–32 and 41–65–91–72).
Postorder: 11 32 29 20 50 72 99 91 65 41. The inorder sequence is always sorted: 11 20 29 32 41 50 65 72 91 99.
Multiple Choice · Insertion
Question 3: The Sequence That Builds a Different Tree
The following sequences are inserted into an empty binary search tree. Which one builds a different tree from the others?
A: 1 becomes the left child of 4; the following 2 and 3 build a chain to the right (1 → 2 → 3). In B, C, and D, 2 is the left child of 4 and the parent of 1 and 3. Many sequences give the same tree, as long as every ancestor is inserted before its descendants.
Spot the Bug · Insertion
Question 4: The Return Value That Is Not Assigned
The recursive insertion routine is written as follows. What does the program print, and for how many nodes is memory allocated? What is the bug, and how is it fixed? (inorder prints left, root, right.)
tree_ptr insert(tree_ptr L, int x) { if (!L) { L = get_node(); /* children NULL */ L->data = x; } else if (x < L->data) insert(L->left, x); else if (x > L->data) insert(L->right, x); return L; } /* in main */ tree_ptr root = NULL; root = insert(root, 50); insert(root, 30); insert(root, 70); insert(root, 30); inorder(root);
Show answer
Output: 50. get_node is called 4 times; the 3 nodes other than 50 are linked nowhere and leak.
- The sub-call stores the new node in its own local
Land returns its address; the caller assigns this value nowhere and ignores it, soL->leftstays NULL. - Since 30 never entered the tree, the second
insert(root, 30)also allocates a new node.
else if (x < L->data) L->left = insert(L->left, x); else if (x > L->data) L->right = insert(L->right, x);
The caller must also always write root = insert(root, x); — in an empty tree this is the only way to obtain the new root.
Step-by-Step Trace · Search
Question 5: Searching for an Absent Value
bst_search(70, root) is run on the tree above (from the second question). Which nodes are examined, and what does the routine return?
x = 70; L points to the root (41).
70 ≠ 41 and 70 > 41 → go right: L = 65. The whole left subtree is eliminated.
70 > 65 → go right: L = 91. 50 is never examined.
70 < 91 → go left: L = 72.
70 < 72 → go left: L = 72->left = NULL.
The loop condition is false → NULL. 4 nodes were examined (as many as the depth). If 70 were inserted, it would become the left child of 72.
tree_ptr bst_search(int x, tree_ptr L) { while (L) { if (x == L->data) return L; if (x < L->data) L = L->left; else L = L->right; } return NULL; }
Multiple Choice · Search
Question 6: The Forgotten return
A recursive search is written as below. In a tree with distinct values and at least two nodes, which statement is true for search(x, root)?
tree_ptr search(int x, tree_ptr L) { if (!L) return NULL; if (x == L->data) return L; if (x < L->data) search(x, L->left); else search(x, L->right); return NULL; }
D: Even if a sub-call finds the node, the address it returns is discarded unused; control reaches the last return NULL. The address is returned only if x == L->data holds in the first call, that is, at the root. The code is valid C and compiles; the recursion ends when it reaches NULL.
Step-by-Step Trace · Depth
Question 7: The depth() Call Stack
depth(root) is called. In which order are the calls (stack: bottom to top) opened and closed, and what is the result? (The number next to a node is the value it returns.)
At the root, left and right are not known yet; the depth of the left subtree is requested first.
Each call has its own left/right locals; those of 50 wait on the stack.
20 is a leaf: both NULL calls return 0 → MAX(0, 0) + 1 = 1.
Back in 30: left = 1. Now the right subtree: depth(40).
The stack is at its deepest: 4 frames (+ NULL calls). 35 → 1.
40: right is NULL (0) → MAX(1, 0) + 1 = 2.
30: MAX(1, 2) + 1 = 3; the frame is removed.
50 has saved left = 3; 60 is a leaf → 1.
MAX(3, 1) + 1 = 4. In total 13 calls: 6 nodes + 7 NULL. Time O(n), stack O(h).
int depth(tree_ptr L) { int left, right; if (L) { left = depth(L->left); right = depth(L->right); return MAX(left, right) + 1; } else return 0; }
Multiple Choice · Depth
Question 8: Call Count with the MAX Macro
Depth is written in one line using a macro. In a tree made of just the root and its two leaf children, how many times in total does depth(root) run (counting the first call)?
#define MAX(a,b) ((a) > (b) ? (a) : (b)) int depth(tree_ptr L) { return !L ? 0 : MAX(depth(L->left), depth(L->right)) + 1; }
B: At a leaf, MAX(a, b) first makes both calls; since 0 > 0 is false, the (b) expression is evaluated once more. 1 + 3 = 4 calls per leaf. At the root the larger one (b when equal) is recomputed too: 1 + 4 + 4 + 4 = 13. The version with local variables finishes with 7 calls (3 nodes + 4 NULL).
Coding Question · Search
Question 9: Finding the Floor Value
In a binary search tree, the floor of x is the node holding the largest value that is not greater than x. Write tree_ptr floor_node(int x, tree_ptr L) as an iterative O(h) routine that returns it, or NULL if there is no such value. In the tree of the second question, floor_node(30) should return 29, floor_node(64) 50, and floor_node(10) NULL.
Show answer
tree_ptr floor_node(int x, tree_ptr L) { tree_ptr candidate = NULL; /* best so far */ while (L) { if (x == L->data) return L; /* exact */ if (x < L->data) L = L->left; else { /* L->data < x */ candidate = L; L = L->right; } } return candidate; }
Every node we turn right at is smaller than x and becomes the candidate; a larger candidate can only be found in its right subtree. A node we turn left at is larger than x and cannot be a candidate.
A single root-to-leaf path is followed: O(h), extra memory O(1).
Classic Question · Deletion
Question 10: Deleting a Node with Two Children
Apply the bst_delete routine from the slides (the data of the largest node in the left subtree is moved up) to the tree of the second question: first bst_delete(41, &root), then bst_delete(20, &root). What does the tree look like after each deletion? Which nodes are released with free?
Show answer
bst_delete(41): two children. Go right in the left subtree: 20 → 29 → 32; 32 has no right child, so it is the largest. 32 goes into the root's data; the node of 32 is freed, and 29's right link takes its left child (NULL).
bst_delete(20): the largest of the left subtree is 11 (the loop never runs). 11 goes into 20's node and the old node of 11 is freed; since b = &p->left, this node's left link becomes NULL.
Only one link changed in each; the depth stayed 4.
Multiple Choice · Deletion
Question 11: If the Last Line Changes
bst_delete: if the line *link = p->left ? p->left : p->right; is changed to *link = NULL; — which statement is then true?
A: At a leaf both children are NULL, so nothing changes. At a node with one child, the child and its whole subtree are cut off from the tree and leak. In a two-children deletion too, if the moved predecessor has a left child (56 under 57 in the slide example), that subtree is lost.
Step-by-Step Trace · Balanced Trees
Question 12: AVL Insertion and Rotation
Into an AVL tree that already holds 40, 20, 60, 10, 30, the values 25, 27, 5, and 4 are inserted in this order. Which rotation happens at which insertion, and what is the final tree? (The red numbers are balance factors.)
Start: all factors are in the range −1..+1.
25 goes to the left of 30. On the way back, the factor of 40 is +2; x = 25 > 20 (left child) → not LL but LR.
First the left child 20 is rotated left: 30 moves up, and 25 becomes the right child of 20 (LL shape).
Then 40 is rotated right: the grandchild 30 rises above its two ancestors. The height is 3, as before the insertion.
27: to the right of 25. Factors −1, −1, +1: no rotation.
5: to the left of 10. Both subtrees of 20 have height 2 (factor 0); no rotation.
4: to the left of 5. The factor of 10 is +2 and x = 4 < 5 (left child) → LL.
10 is rotated right, and 5 becomes the root of the subtree. Final height 4; the same sequence in an ordinary BST would build a tree of 5 levels.
avl_ptr avl_insert(avl_ptr t, int x) { if (!t) return new_avl(x); 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; t->ht = 1 + MAX(ht(t->left), ht(t->right)); int d = balance(t); if (d > 1 && x < t->left->data) /* LL */ return rot_right(t); if (d < -1 && x > t->right->data) /* RR */ return rot_left(t); if (d > 1) { /* LR */ t->left = rot_left(t->left); return rot_right(t); } if (d < -1) { /* RL */ t->right = rot_right(t->right); return rot_left(t); } return t; }
Multiple Choice · Balanced Trees
Question 13: About Balanced Trees
Which of the statements about balanced search trees is false?
D is false: deletion decreases the height of a subtree, and this decrease can propagate up to the root; O(lg n) rotations may be needed along the path. In insertion, the rotation restores the subtree height to its value before the insertion, so the ancestors above are not affected.
Multiple Choice · Balanced Trees
Question 14: Assignment Order in a Rotation
rot_right routine: what happens if the order of the first two link assignments is changed as below?
avl_ptr rot_right(avl_ptr k2) {
avl_ptr k1 = k2->left;
k1->right = k2; /* order changed */
k2->left = k1->right;
k2->ht = 1 + MAX(ht(k2->left), ht(k2->right));
k1->ht = 1 + MAX(ht(k1->left), k2->ht);
return k1;
}B: When k1->right = k2 is executed first, the address of the old right subtree of k1 (T2) is erased. Next, k2->left = k1->right, that is, k2->left = k2: a link pointing back to itself. A traversal loops forever at this node, and T2 leaks. In the correct order, T2 is first moved to the left of k2, and then k2 is attached to the right of k1.
Multiple Choice · Exam Questions
Question 15: SWAP in Between: The Fix
In the exam question's routine, SWAP sits between the two recursive calls, and the tree does not become a full mirror image. Which replacement of the second call (the marked line) makes the routine produce the full mirror image of the tree?
#define SWAP(x,y,t) ((t)=(x),(x)=(y),(y)=(t)) void proc(tree_ptr L) { tree_ptr p; if (L) { proc(L->left); SWAP(L->left, L->right, p); proc(L->right); /* second call */ } }
C: After SWAP, the old right subtree, which has not been processed yet, is on the left; proc(L->left) processes it. For the tree {44, 30, 15, 18, 86, 97, 65, 113, 74, 129} the inorder output becomes 129 113 97 86 74 65 44 30 18 15. A processes the old left subtree a second time and flips it back; D never flips the old right subtree; B is infinite recursion.
Classic Question · Exam Questions
Question 16: FindWeight Call Count
The faulty FindWeight routine from the exam question is called on a tree with root 5 and only a left child, 3. For i) x = 3 and ii) x = 9, how many times does the routine run (counting the first call)?
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; }
Show answer
A call made with NULL counts as 1. If x is not found, every node calls its subtrees four times, once for each of the four comparison terms; since > is never true, the inner calls are not made.
- x = 9: for leaf 3: 1 + 4 = 5 calls. Root: 1 + A(3) + A(NULL) + A(NULL) + A(3) = 1 + 5 + 1 + 1 + 5 = 13.
- x = 3: A(3) returns 1 immediately through
weight(1 call). Root: in the first if, A(3) = 1 > A(NULL) = 0 is true, so A(3) is called once more; in the second if, A(NULL) and A(3) are evaluated: 1 + 1 + 1 + 1 + 1 + 1 = 6.
If each intermediate call result were computed once and kept in a local variable, these repetitions would not occur.