Chapter 9: Trees
Multiple Choice · The Tree Concept
Question 1: Edge Count and Cycles
A connected structure has 10 nodes and 10 edges. Which statement must be true?
B. A tree with n nodes always has n − 1 = 9 edges. A 10th edge in a connected structure joins two nodes that already have a path between them, which creates a cycle. A is wrong: being connected is not enough; the edge count must also be n − 1.
Multiple Choice · The Tree Concept
Question 2: Tree Relationships
Which statement is true for the tree shown?
D. The ancestors of a node are the nodes on its path to the root: Y → W → Q → R. A is wrong: U's parent is P and V's is Q; having the same depth does not make nodes siblings. B leaves out X: the descendants are the whole subtree (T, U, X). S has no children, so it is a leaf.
Short Answer · The Tree Concept
Question 3: Depth and Height
For the tree shown, find the depth and height of nodes P, W, and X, counting edges. What is the height of the tree?
Show answer
| Node | Depth | Height |
|---|---|---|
| P | 1 | 2 (P→T→X) |
| W | 2 | 1 (W→Y) |
| X | 3 | 0 (leaf) |
Depth is measured down from the root (R has depth 0); height is measured from the node down to its farthest leaf.
Height of the tree = height of the root = the largest depth = 3 (R→P→T→X or R→Q→W→Y).
Sources that count nodes get every value one larger (tree height 4).
Multiple Choice · The Tree Concept
Question 4: height(Q) with the ADT
The method below is called as height(Q) on the tree shown. What does it return, and how many times is the method called in total?
int height(Position<E> p) { int h = 0; for (Position<E> c : children(p)) h = Math.max(h, 1 + height(c)); return h; }
A. V is a leaf (0). W's children Y and Z are leaves → W = 1. Q = max(1 + 0, 1 + 1) = 2. Only Q's subtree is visited: Q, V, W, Y, Z → 5 calls. B is wrong: height looks downward, so the rest of the tree (R, P, S…) is never visited.
Short Answer · Binary Trees
Question 5: Full, Complete, Perfect
Which of the properties full, complete, and perfect does each of the binary trees T1 and T2 have?
Show answer
| Full | Complete | Perfect | |
|---|---|---|---|
| T1 | yes | no | no |
| T2 | no | yes | no |
T1: every node has 0 or 2 children → full. But the bottom level hangs under 4 while the space under 3 is empty → the level above the last is not full, so it is not complete.
T2: the last level (4, 5, 6) fills from left to right with no gaps → complete. Node 3 has only one child → not full.
Neither is perfect: the leaves are not all at the same depth (T1), or an internal node has only one child (T2).
Multiple Choice · Binary Trees
Question 6: Always True
Which statement holds for every binary tree?
B. In a perfect tree every internal node has 2 children (full) and every level is full (complete). A and C are wrong: full and complete do not imply each other (there are full trees that are not complete and complete trees that are not full). D: the leaves of a complete tree may lie on the last two levels.
Short Answer · Binary Trees
Question 7: Height Bounds
What are the smallest and largest possible heights of a binary tree with 1,000 nodes? Justify your answer.
Show answer
At most 999: in a skewed (chain) tree, where every node has one child, h = n − 1.
At least 9 = ⌊log2 1000⌋. A binary tree of height h holds at most 2h+1 − 1 nodes: h = 8 → 511 < 1000 is not enough; h = 9 → 1023 ≥ 1000 is.
In general, ⌊log2 n⌋ ≤ h ≤ n − 1. Since tree operations depend on height, a balanced tree needs ≈ 10 steps and a skewed one ≈ 1,000.
Short Answer · Representations
Question 8: Drawing a Tree from an Array
A binary tree is stored in a 1-based array (index 0 unused, – marks an empty cell). Draw the tree.index: 1 2 3 4 5 6 7 8 9 10a[i]: A B C D E – F – – G
Show answer
1-based: left = 2i, right = 2i + 1, parent = ⌊i/2⌋.
- B (2), C (3): the left and right children of A. D (4), E (5): the children of B.
- 6 is empty → C has no left child; F (7) → C's right child.
- G (10): parent ⌊10/2⌋ = 5 → E; 10 is even → E's left child.
Multiple Choice · Representations
Question 9: The 0-Based Index Formula
A binary tree is stored in a 0-based array (the root at index 0). What are the indices of the parent and the left child of the node at index 6?
C. 0-based: parent = ⌊(i − 1)/2⌋ = ⌊5/2⌋ = 2, left child = 2i + 1 = 13 (right child 14). A is a common mistake that comes from using the 1-based formulas (⌊i/2⌋ and 2i).
Multiple Choice · Representations
Question 10: Counting null References
In the linked representation with left/right references, how many null child references does a binary tree with 50 nodes have in total?
D. Each node has 2 references → 2n = 100. Of these, n − 1 = 49 are edges (every node except the root is pointed to by its parent). The remaining 100 − 49 = 51 = n + 1 references are null. A is the number of non-null references (the edges).
Short Answer · Traversals
Question 11: Preorder and Postorder
Write the preorder and postorder traversal outputs of the binary tree shown.
Show answer
Preorder (root → left → right): F B A D C E G I H
Postorder (left → right → root): A C E D B H I G F
The root (F) is the first item of the preorder output and the last item of the postorder output. G has no left child, so in preorder its right subtree (I, H) comes right after G.
For comparison, the inorder output A B C D E F G H I is sorted → this tree is a BST.
Code Trace · Traversals
Question 12: Inorder with a Stack
What does inorderIterative(root) print for the tree rooted at 8 (8's children are 3 and 10; 3's are 1 and 6; 10 has only a right child, 14)? (stack: bottom → top)
temp starts at the root (8); the stack is empty.
Push 8 (to be printed later); temp = 8.left = 3. Keep going left.
Push 3; temp = 3.left = 1.
Push 1; temp = 1.left = null. Nothing more to the left.
temp is null → pop and print 1. temp = 1.right = null.
temp is null → pop and print 3. temp = 3.right = 6.
Push 6; temp = 6.left = null.
Pop and print 6; temp = 6.right = null.
Pop and print 8 (its left subtree is done); temp = 8.right = 10.
Push 10; temp = 10.left = null.
Pop and print 10; temp = 10.right = 14.
Push 14; temp = 14.left = null.
Pop and print 14; temp = 14.right = null.
The stack is empty and temp is null → the loop ends. The output is sorted (left → root → right); each node is pushed and popped once: O(n) time, O(h) stack.
void inorderIterative(TreeNode root) { Deque<TreeNode> stack = new ArrayDeque<>(); TreeNode temp = root; while (!stack.isEmpty() || temp != null) { if (temp != null) { stack.push(temp); temp = temp.left; } else { temp = stack.pop(); System.out.print(temp.data + " "); temp = temp.right; } } }
Multiple Choice · Traversals
Question 13: A Bug in Stack-Based Preorder
What does the stack-based preorder code below print for the tree where 1's children are 2 and 3, 2's are 4 and 5, and 3's are 6 and 7?
void preorderIterative(TreeNode root) { Deque<TreeNode> stack = new ArrayDeque<>(); stack.push(root); while (!stack.isEmpty()) { TreeNode t = stack.pop(); System.out.print(t.data + " "); if (t.left != null) stack.push(t.left); if (t.right != null) stack.push(t.right); } }
B. Left is pushed before right, so the right child ends up on top (LIFO) and is processed first → root, right, left. A correct preorder pushes right first, then left; the output would then be A (1 2 4 5 3 6 7).
Code Trace · Traversals
Question 14: Level Order with a Queue
What does levelOrder(root) print for the tree shown? Track the contents of the queue at each step (queue: front → rear).
Add the root to the queue. A queue is FIFO: first in, first out.
poll() → print F. F's children B and G join the rear.
poll() → print B. B's children A and D join behind G: level 1 finishes first.
poll() → print G. G has no left child; only its right child, I, is added.
poll() → print A. A is a leaf: no children to add.
poll() → print D. D's children C and E are added.
poll() → print I. Only I's left child, H, is added.
poll() → print C. C is a leaf.
poll() → print E. E is a leaf.
poll() → print H. H is a leaf; the queue is now empty.
The queue is empty → the loop ends. The output goes level by level: F B G A D I C E H. The queue never held more than 3 nodes: extra space O(w), where w is the widest level.
void levelOrder(TreeNode root) { if (root == null) return; Queue<TreeNode> queue = new LinkedList<>(); queue.add(root); while (!queue.isEmpty()) { TreeNode t = queue.poll(); System.out.print(t.data + " "); if (t.left != null) queue.add(t.left); if (t.right != null) queue.add(t.right); } }
Short Answer · Traversals
Question 15: Expression Trees
Write the postfix form of the expression tree shown, and find the value that evaluate() (leaf → number, internal node → operator) returns.
Show answer
Postfix = postorder traversal: 6 2 + 3 × 8 4 / −
Value: 6 + 2 = 8 → 8 × 3 = 24; 8 / 4 = 2; 24 − 2 = 22.
evaluate() follows postorder logic: an operator is applied only after the values of both its subtrees are known.
For comparison: prefix (preorder) is − × + 6 2 3 / 8 4; infix (inorder) without parentheses, 6 + 2 × 3 − 8 / 4, reads wrongly, so each subtree must be parenthesized: ((6 + 2) × 3) − (8 / 4).
Short Answer · Traversals
Question 16: Building a Tree from Traversals
The traversals of a binary tree are given. Draw the tree and write its postorder output.Preorder: 1 2 4 7 3 5 6 8Inorder: 4 7 2 1 5 3 8 6
Show answer
The first item of the preorder is the root: 1. In the inorder, {4, 7, 2} lies to the left of 1 and {5, 3, 8, 6} to the right. Left side: the next item, 2, is its root; {4, 7} is on its left and nothing on its right. 4 is the root there, and 7 is its right child. Right side: 3 is the root; {5} on its left, {8, 6} on its right → 6 is the root, with 8 as its left child.
Postorder: 7 4 2 5 8 6 3 1
Multiple Choice · Traversals
Question 17: Extra Space of Traversals
For a perfect binary tree with n nodes, what is the extra space of level order with a queue and of recursive inorder, respectively?
A. The queue grows as large as the widest level (O(w)); a perfect tree has ≈ n/2 nodes on its last level → O(n). Recursion uses O(h) frames, and a perfect tree has h = log2(n + 1) − 1 → O(log n). B confuses the space of level order with the height. Both take O(n) time.
Multiple Choice · Traversals
Question 18: height on a Linked Tree
What does the method from the lecture return for height(A) and height(F), respectively, on the tree shown?
int height(TreeNode node) { if (node == null) return -1; return 1 + Math.max(height(node.left), height(node.right)); }
C: for the leaf F, 1 + max(−1, −1) = 0; the longest path, A→B→E→G, has 3 edges. A counts nodes, as if the base case were return 0 (every value one larger). B gives −1 to a leaf, but −1 is the height of the empty tree (null) only. D mixes the two measures.
Short Answer · Traversals
Question 19: The Euler Tour
An Euler tour walks around the tree shown, passing each node on its left, below it, and on its right. (a) What three orders do you get by writing the nodes down when passing on the left, below, and on the right? (b) How many passes does the tour make in total? (c) When does the tour pass below D, which has no left child?
Show answer
(a) Left (preorder): H D F K J M
Below (inorder): D F H J K M
Right (postorder): F D J M K H
(b) 3 passes per node → 3n = 18. Superscripts 1, 2, 3 = left, below, right:
H¹ D¹ D² F¹ F² F³ D³ H² K¹ J¹ J² J³ K² M¹ M² M³ K³ H³
(c) Since D's left subtree is empty, its left and below passes come back to back (D¹ D²); that is why D comes before F in inorder. For a leaf, all three passes are consecutive. The tour takes O(n).
Multiple Choice · Traversals
Question 20: Preorder + Postorder
How many different binary trees have preorder 1 2 4 3 5 and postorder 4 2 5 3 1?
D: 1 is the root; in preorder 2 comes right after 1, and in postorder 3 comes right before it → {2, 4} is the left subtree and {3, 5} the right. But the two traversals cannot tell whether 4 is the left or right child of 2 (both give 2 4 and 4 2), and the same holds for 3 and 5 → 2 · 2 = 4 trees (inorder 4 2 1 5 3, 2 4 1 3 5, …). A wrongly carries over the uniqueness of preorder + inorder. If every node has 0 or 2 children (a full tree), this pair does determine the tree.
Short Answer · Traversals
Question 21: size and countLeaves
On the tree shown, what do the lecture's size(root) and countLeaves(root) return? How many times is each method called in total, counting the first call and the calls on null?
int size(TreeNode node) { if (node == null) return 0; return 1 + size(node.left) + size(node.right); } int countLeaves(TreeNode node) { if (node == null) return 0; if (node.left == null && node.right == null) return 1; return countLeaves(node.left) + countLeaves(node.right); }
Show answer
size= 9,countLeaves= 4 (1, 4, 7, 13).sizeis called on both children of every node: 9 nodes + n + 1 = 10 nulls → 19 calls (2n + 1).countLeavesstops at a leaf and never descends to its null children. Only the 5 internal nodes (8, 3, 6, 10, 14) make two calls each: 1 + 2 · 5 = 11 calls, 2 of them on null (10's left, 14's right).
Both follow postorder logic: O(n) time, O(h) stack space (here h = 3).
Multiple Choice · Traversals
Question 22: The Forgotten Leaf Check
If the marked line is deleted, what does countLeaves(root) return for a nonempty tree with n nodes?
int countLeaves(TreeNode node) { if (node == null) return 0; if (node.left == null && node.right == null) return 1; // deleted return countLeaves(node.left) + countLeaves(node.right); }
A: only the base case, which returns 0 for null, is left; no call ever produces a 1, and a sum of 0s is 0. B needs the line that returns 1 at a leaf. C would come from the 1 + term of size. D is what you would get if the base case node == null returned 1: a binary tree with n nodes has n + 1 null references.