Chapter 10: Trees
Multiple Choice · Terminology
Question 1: Degree, Depth, Ancestors
In the tree shown (root at level 1), what are the degree and the depth of the tree, and the ancestors of K?
A: The degree is the largest number of children of any node: A and D have three each → 3. The depth is the largest level: J, K, L are at level 4. The ancestors are the nodes on the path from the root to K: A, D, H. Option C gives the height in CLRS terms (number of edges: 3).
Multiple Choice · Binary Tree
Question 2: Numbers from the Lemma
In a binary tree of depth 6 (root at level 1), what is the maximum number of nodes, the maximum number of nodes at level 6, and the minimum total number of nodes?
C: A tree of depth k has at most 2ᵏ − 1 = 63 nodes; level i has at most 2ⁱ⁻¹ = 32. For the depth to be 6, every level needs at least one node: at least 6 (a skewed tree). D gives the values for depth 7.
Multiple Choice · Binary Tree
Question 3: Leaves and NULL Links
A binary tree has 12 leaves and 5 nodes with one child. What is the total number of nodes and the number of NULL pointers in the linked representation?
D: n₀ = n₂ + 1 → n₂ = 11. n = 12 + 5 + 11 = 28. Of the 2n = 56 pointers, n − 1 = 27 are used (every node except the root has one link entering it); the NULL ones number 56 − 27 = n + 1 = 29.
Classic Question · Representation
Question 4: From an Array to a Tree
In the 1-based array representation, tree[1..9] = A B C D – E F – G (– is an empty cell). Draw the tree. Who is G's parent, and is G a left or a right child? What is the depth, and how many cells must be allocated for a general binary tree of this depth?
Show answer
- The children are at 2i and 2i + 1: A(1) → B(2), C(3); B(2) → D(4), cell 5 empty; C(3) → E(6), F(7); D(4) → cell 8 empty, G(9).
- G: parent(9) = ⌊9/2⌋ = 4 → D; since 9 is odd, it is the right child.
- Depth 4 → 2⁴ − 1 = 15 cells. For 7 nodes, 15 cells: space is wasted because the tree is not complete.
Multiple Choice · Representation
Question 5: Formulas in a 0-Based Array
The root is placed in tree[0], but the formulas are not changed and left_child(i) = 2i, right_child(i) = 2i + 1 are used. What happens?
C: left_child(0) = 0: the root becomes its own child and a traversal never ends; right_child(0) = 1 also lands where the left child should be. With 0-based indexing, left is 2i + 1, right is 2i + 2, and the parent is ⌊(i − 1)/2⌋.
Classic Question · Representation
Question 6: The Order of Freeing a Tree
Why is the following tree_free wrong? Give two different fixes.
void tree_free(tree_pointer L) { if (!L) return; free(L); tree_free(L->left_child); tree_free(L->right_child); }
Show answer
The root is released with free first, and only afterwards are L->left_child and L->right_child read: freed memory is read (dangling pointer, undefined behavior). That it sometimes seems to work in tests hides the bug.
/* 1) root last */ void tree_free(tree_pointer L) { if (!L) return; tree_free(L->left_child); tree_free(L->right_child); free(L); } /* 2) save the links first */ void tree_free2(tree_pointer L) { if (!L) return; tree_pointer s = L->left_child, g = L->right_child; free(L); tree_free2(s); tree_free2(g); }
The first version is a postorder traversal; the second saves the children first. In both, n nodes cause n calls to free (verified with a counter).
Multiple Choice · Representation
Question 7: Left Child – Right Sibling
The general tree A(B(E, F), C, D(G)) is converted into a binary tree with the left child – right sibling method. What is the inorder sequence of the resulting binary tree?
D: A.left = B; B.left = E, B.right = C; E.right = F; C.right = D; D.left = G. Inorder: E F B C G D A (verified by a program). This is the postorder sequence of the general tree. Option A is the preorder sequence; it is the same for both trees.
Classic Question · Traversals
Question 8: Expression Tree
(a - b) * c + d / (e + f): draw the tree of this expression and write its preorder, inorder, and postorder sequences. For a = 9, b = 3, c = 2, d = 8, e = 1, f = 3, evaluate the postorder sequence with a stack.
Show answer
- Preorder:
+ * - a b c / d + e f - Inorder (with parentheses):
((a - b) * c) + (d / (e + f)) - Postorder:
a b - c * d e f + / +
Computation: 9 − 3 = 6, 6 · 2 = 12, 1 + 3 = 4, 8 / 4 = 2, 12 + 2 = 14 (verified with eval). The root is processed last: the values of both subexpressions are needed first.
Step-by-Step Trace · Traversals
Question 9: Computing the Depth
depth is called on the tree A(B(–, D(E, –)), C). What does each node return, and what is the result?
Call for B, the left child of A; A's frame waits.
B's left is NULL → returned 0 (s = 0). Now the right child, D.
Call for E, the left child of D: the stack is at its deepest, 5 frames.
E is a leaf: both children give 0 → returns 1 + 0 = 1.
D: s = 1, g = 0 (NULL) → 2.
B: s = 0, g = 2 → 3.
Back in A, s = 3. The right child C is a leaf → 1.
A: 1 + max(3, 1) = 4. Postorder pattern: both subtrees first, then the node. O(n) time, O(h) stack.
int depth(tree_pointer L) { if (!L) return 0; int s = depth(L->left_child); int g = depth(L->right_child); return 1 + (s > g ? s : g); }
Multiple Choice · Traversals
Question 10: NULL Calls
A binary tree with 12 nodes is traversed with the recursive preorder (there is no NULL check before the call). How many calls are made in total, and how many of them are made with NULL?
B: One call per node (12) and one call per NULL link; the number of NULL links is n + 1 = 13. Total 2n + 1 = 25; the call tree is the tree itself with NULL calls as its leaves (31 calls and 16 NULL measured on a 15-node tree). Checking before the call removes the 13 calls.
Step-by-Step Trace · Iterative Traversal
Question 11: Preorder with a Stack
The stack version of preorder traversal is run on the tree A(B(D, E), C(–, F)). How does the stack change, and what is the output?
The root is pushed on the stack.
A is popped and printed; the right child (C) is pushed first, then the left (B): the left stays on top.
B is popped and printed; E and D are pushed.
D is a leaf: it is printed, nothing is pushed.
E is printed. B's subtree is done; C is waiting next.
C is printed; it has no left child, only F is pushed.
F is printed; the stack is empty. The next pop returns NULL and the loop ends.
void iter_preorder(tree_pointer L) { tree_pointer stack[MAX_SIZE]; int top = -1; if (L) push(&top, L, stack); while ((L = pop(&top, stack))) { printf("%c", L->data); if (L->right_child) push(&top, L->right_child, stack); if (L->left_child) push(&top, L->left_child, stack); } }
Classic Question · Other Procedures
Question 12: Nodes at Level k
Write the recursive procedure int count_level(tree_pointer L, int k) that returns the number of nodes at level k (the root is at level 1). What does it return on the tree A(B(D, E), C(–, F)) for k = 1, 2, 3, 4?
Show answer
int count_level(tree_pointer L, int k) { if (!L || k < 1) return 0; if (k == 1) return 1; return count_level(L->left_child, k - 1) + count_level(L->right_child, k - 1); }
Results: 1, 2, 3, 0 (verified by a program). Each call decreases k by one; every non-empty node reached with k = 1 adds 1. Worst case O(n) time, O(min(k, h)) stack; no queue is needed.
Step-by-Step Trace · Other Procedures
Question 13: Level Order with a Small Queue
level_order with MAX_SIZE = 4 (a circular queue) is run on the tree A(B(D, E), C(–, F)). Is the queue large enough, and what is the output?
The root enters the queue: rear = 1. A circular queue with four cells holds at most 3 addresses.
A is dequeued and printed; the queue is empty.
A's children B and C are added at the rear.
B is dequeued; while D and E are added, rear wraps around: D goes to cell 0, E to cell 1.
C is dequeued and F is added (cell 2). The queue holds D, E, F: its capacity is completely full.
D is dequeued (front wrapped around); it has no children.
E and F are dequeued too; front == rear: the queue is empty and the loop ends.
void level_order(tree_pointer L) { tree_pointer q[MAX_SIZE]; /* queue */ int front = 0, rear = 0; if (!L) return; enqueue(front, &rear, L, q); while ((L = dequeue(&front, rear, q))) { printf("%c", L->data); if (L->left_child) enqueue(front, &rear, L->left_child, q); if (L->right_child) enqueue(front, &rear, L->right_child, q); } }
Multiple Choice · Other Procedures
Question 14: SWAP in Between
swap_between puts SWAP between the two recursive calls. Applied to the tree A(B(D, E), C), what is the inorder sequence of the new tree?
#define SWAP(x, y, t) ((t) = (x), (x) = (y), (y) = (t)) void swap_between(tree_pointer L) { tree_pointer p; if (L) { swap_between(L->left_child); SWAP(L->left_child, L->right_child, p); swap_between(L->right_child); } }
B: At B, D is processed first, the swap gives B(E, D), then the “right” child, which is again D, is processed. At A, the swap gives A(C, B) and the B on the right is processed a second time, which turns it back into B(D, E). The result is A(C, B(D, E)): C A D B E. For a true mirror (A), SWAP must come before or after both calls (verified by running).
Classic Question · Exam Question
Question 15: A Tree from Two Traversals
The binary tree whose inorder sequence is D B G E A C H F and whose postorder sequence is D G E B H F C A: draw it and write its preorder sequence.
Show answer
- The last item of the postorder sequence, A, is the root. In the inorder sequence, A's left part is D B G E and its right part is C H F.
- Left side: postorder D G E B → root B; left child D, right part G E. In G E the last one is E → G is E's left child.
- Right side: H F C → root C; no letter before C in inorder → empty left side; right part H F → F, with left child H.
- Preorder: A B D E G C F H (built and verified by a program).
Multiple Choice · Summary
Question 16: Extra Memory Comparison
In a perfect binary tree with n = 2ᵏ − 1 nodes, which statement is true about the extra memory of stack-based inorder traversal and queue-based level-order traversal?
A: The stack holds at most one left chain at a time, i.e. as many addresses as the depth: k = lg(n + 1) (4 measured for 15 nodes). The queue accumulates a whole level; the last level has 2ᵏ⁻¹ ≈ n/2 nodes. In a left-skewed tree the situation is reversed: stack O(n), queue O(1).