Chapter 12: Threaded Binary Trees
Multiple Choice · Why Threads?
Question 1: Null Pointers in a Ternary Tree
In a tree of n nodes (n ≥ 1) in which every node has three child pointers, how many pointers are NULL?
A: There are 3n pointers in total. Every node except the root is pointed to by exactly one pointer of its parent: n − 1 are used. The rest is 3n − (n − 1) = 2n + 1. For a binary tree the same computation gives 2n − (n − 1) = n + 1.
Classic Question · Why Threads?
Question 2: Call Stack or Threads?
A left-skewed binary tree with 1,000,000 nodes (every node has only a left child) is traversed in inorder. In the recursive traversal every call frame takes 48 bytes. How many frames does the stack reach at most, and about how much memory? How much extra memory does a traversal of the threaded tree need?
Show answer
- From the root to the leftmost leaf, 1,000,000 calls are nested, plus one more call with the leaf's NULL left child: 1,000,001 frames, about 1,000,001 × 48 ≈ 48 MB.
- The default stack is typically 1–8 MB: the program crashes with a stack overflow.
- An iterative traversal that keeps its own stack also needs O(h) = 1,000,000 pointers (≈ 8 MB, on the heap).
- Threaded traversal uses just the pointer
p: extra memory O(1); the price is two flags per node.
Classic Question · Threading Rules
Question 3: Determining the Threads
The tree with the inorder sequence N L Q O K M P is shown (the dashed links do not exist yet). Once it is threaded, which pointer points to which node? How many threads go to the head node?
Show answer
| node | left thread → predecessor | right thread → successor |
|---|---|---|
| N | head | L |
| Q | L | O |
| O | (real child Q) | K |
| M | K | (real child P) |
| P | M | head |
7 nodes → 8 = n + 1 threads; 2 of them (N's left, P's right) go to the head node.
Multiple Choice · Threading Rules
Question 4: A Left Thread Pointing to the Head
In a threaded tree, a node's left_thread field is TRUE and its left_child field points to the head node. Which statement is certainly true for this node?
B: In inorder, a left thread points to the predecessor; if it points to the head node instead of a predecessor, the node has no predecessor, so it is the first one visited. This node can have a right subtree, so it does not have to be a leaf; a root with no left subtree can also be this node, but it is not always the root.
Multiple Choice · Threading Rules
Question 5: Right Threads Only
In a tree only the right NULL pointers have been turned into inorder successors, and the left NULLs are left as NULL (the right flags are kept). Which operation can be done with O(1) extra memory in this tree?
C: insucc uses only the right pointer, the right flag, and the left child links (when descending to the leftmost node, the left NULL stops the loop). Predecessor and reverse traversal need left threads; postorder needs parent information.
Spot the Bug · Threading Rules
Question 6: Leftmost Without the Flags
The following routine was written to find the first node of an inorder traversal in a threaded tree. What happens on the tree of the third question? Fix the routine.
thread_ptr leftmost(thread_ptr head) {
thread_ptr p = head->left_child; /* root */
while (p->left_child != NULL)
p = p->left_child;
return p;
}
Show answer
In a threaded tree no left_child is NULL: N's left points to the head node, and the head's left points to the root again. p becomes K, L, N, head, K, L, N … in turn: infinite loop.
Fixed body:
thread_ptr p; if (head->left_thread) return NULL; /* empty tree */ p = head->left_child; while (!p->left_thread) /* real */ p = p->left_child; return p;
In an empty tree the head's left_thread is TRUE; this case is handled separately. The call insucc(head) does the same job (in an empty tree it returns the head node).
Multiple Choice · Inorder Traversal
Question 7: The Predecessor of the Root
In the threaded tree of the third question, which node does inpred(K) return?
thread_ptr inpred(thread_ptr L) {
thread_ptr p = L->left_child;
if (!L->left_thread)
while (!p->right_thread) p = p->right_child;
return p;
}D: K's left pointer is a real child (L); we go down to the rightmost node of the left subtree: L's right is real (O), O's right is a thread (right_thread TRUE) → O. In inorder, O comes right before K as well: N L Q O K. Q comes before O because it is in O's left subtree.
Step-by-Step Trace · Inorder Traversal
Question 8: Stackless Inorder Traversal
In the tree of the third question, tinorder(head) runs. In each round, where does p go, and how many pointers are followed (each assignment to p counts as one)?
p starts at the head node. No stack or recursion; a single pointer is enough.
The head's right is itself and its flag is FALSE → go down left: K, L, N; N's left is a thread → N (4 pointers).
N's right is a thread: the successor is directly L.
L's right is real (O); O's left is real → Q; Q's left is a thread → Q.
Q's right thread → O.
O's right thread → K.
K's right is real (M); M's left is a thread, the loop never runs → M.
M's right is real (P); P's left is a thread → P.
P's right thread points to the head → the loop ends. 12 pointers followed: n + 1 = 8 calls, 4 real left links. O(n), extra memory O(1).
thread_ptr insucc(thread_ptr L) {
thread_ptr p = L->right_child;
if (!L->right_thread)
while (!p->left_thread)
p = p->left_child;
return p;
}
void tinorder(thread_ptr L) {
thread_ptr p = L;
for (;;) {
p = insucc(p);
if (p == L) break;
printf("%c ", p->data);
}
}
Output Question · Inorder Traversal
Question 9: Threaded Preorder Traversal
In the tree of the third question, starting at the head node, presucc is called until it returns to the head, and every node is printed. What is the output? Which node does presucc(Q) return, and how many right threads are followed in the process?
thread_ptr presucc(thread_ptr p) {
if (!p->left_thread) /* left child, if any */
return p->left_child;
while (p->right_thread) /* climb threads */
p = p->right_child;
return p->right_child; /* real right child */
}
Show answer
Output: K L N O Q M P (preorder).
- Q has no left child (left thread TRUE); we climb along right threads: Q → O (1), O's right is a thread too → K (2).
- K's right pointer is a real child: the loop stops and
K->right_child= M is returned. 2 threads were followed. - Q is the last node of O's left subtree; since O's right is empty, the sequence moves on to K's right subtree.
Multiple Choice · Inorder Traversal
Question 10: The Cost of Morris Traversal
Which statement about Morris traversal is true?
A: A thread is written temporarily into the predecessor's empty right pointer and removed when the node is reached a second time. So no flag is needed, and the tree returns to its original state when the traversal ends. Since the tree is modified during the traversal, another reader at the same time could see a wrong link.
Step-by-Step Trace · Inorder Traversal
Question 11: Morris Traversal Step by Step
The tree in the figure is stored without threads and without flags. morris_inorder(root) sets up and removes which temporary threads, and what does it print?
Start: an ordinary tree, no flags.
K's predecessor is O (go right repeatedly from L). O's right is empty → temporary thread O → K, go left.
L's predecessor is N; the thread N → L is set up, go left.
N has no left child: print it and go right; since the right pointer is a temporary thread, it returns to L.
The predecessor N's right is already L: the thread is removed, L is printed, go right (O).
O's predecessor is Q; the thread Q → O is set up, go left.
Q is printed; the temporary thread leads back to O.
Q → O is removed, O is printed; O's right (a temporary thread) leads to K.
K's predecessor is O, and O's right is K: the thread is removed, K is printed, go right (M).
M has no left child: print it, go right (P).
P is printed, L = NULL: done. 10 rounds, 3 temporary threads (one for every node with a left child); the tree is in its original state.
void morris_inorder(tree_ptr L) { while (L) { if (!L->left) { printf("%c", L->data); L = L->right; } else { tree_ptr p = L->left; while (p->right && p->right != L) p = p->right; if (!p->right) { p->right = L; L = L->left; } else { p->right = NULL; printf("%c", L->data); L = L->right; } } } }
Coding Question · Inserting a Node
Question 12: Searching a Threaded Search Tree
In a threaded binary search tree (with a head node, char data), write the routine thread_ptr threaded_search(thread_ptr head, char x) that searches for the value x. It returns NULL if x is not found; an empty tree must also be handled correctly. The search is also the first step of the insertion routines of a threaded tree.
Show answer
thread_ptr threaded_search(thread_ptr head, char x) { thread_ptr p = head->left_child; short thr = head->left_thread; /* empty: TRUE */ while (!thr) { /* a real node */ if (x == p->data) return p; if (x < p->data) { thr = p->left_thread; p = p->left_child; } else { thr = p->right_thread; p = p->right_child; } } return NULL; /* hit a thread: absent */ }
In an ordinary BST we would stop on reaching NULL; here, if the flag of the pointer being followed is TRUE (a thread), x is not in the tree. For insertion, at exactly this point we call insert_left if x is smaller and insert_right if it is larger. O(h).
Step-by-Step Trace · Inserting a Node
Question 13: insert_right(L, X): The Second Case
To the tree of the third question, insert_right(L, X) is applied. Since L has a right subtree (O), this is the second case. Which fields change, and what is the new inorder sequence?
First: L's right is a real child, O. X is the new node; its fields are still empty.
X's right gets L's old right subtree (O); the flag is copied too: FALSE, a real link.
X's inorder predecessor is L: the left pointer goes to L, the left flag is TRUE (a thread).
The real link L → X is set. L's flag was already FALSE; this line is needed for the first case.
X's right flag is FALSE → the successor of X is searched for: from O to the leftmost node, Q. Q's left thread still points to L.
Q's predecessor is now X. New sequence: N L X Q O K M P. 7 assignments; O(h) because of the successor search.
void insert_right(thread_ptr parent, thread_ptr child) { thread_ptr p; child->right_child = parent->right_child; child->right_thread = parent->right_thread; child->left_child = parent; child->left_thread = TRUE; parent->right_child = child; parent->right_thread = FALSE; if (!child->right_thread) { p = insucc(child); p->left_child = child; } }
Multiple Choice · Inserting a Node
Question 14: A Constant TRUE Flag
insert_right: the line child->right_thread = parent->right_thread; is changed to child->right_thread = TRUE; — after the same call insert_right(L, X), what does the inorder traversal print?
C: X's right pointer points to O but is taken for a thread because of the TRUE flag; succ(X) becomes O directly, and Q in O's left subtree is skipped. Since the flag is TRUE, the if block does not run either; Q's left thread stays at L. There is no loop; the traversal reaches the head node and ends.
Multiple Choice · Inserting a Node
Question 15: What Does insert_right Change?
insert_right(parent, child) — besides child, the fields of which nodes can change?
D: In the parent, only right_child and right_thread are written. In the second case, the new successor of child (the parent's old successor) used to point to the parent as its predecessor; its left_child is set to child. No other thread changes, because no other thread points just before or after child.
Classic Question · Inserting a Node
Question 16: insert_left: The First Case
Apply insert_left(M, Y) from the slides to the tree of the third question. M has no left subtree. What are the four fields of Y and the left fields of M? Does the if block run? What is the new inorder sequence?
void insert_left(thread_ptr parent, thread_ptr child) { child->left_child = parent->left_child; child->left_thread = parent->left_thread; child->right_child = parent; child->right_thread = TRUE; parent->left_child = child; parent->left_thread = FALSE; if (!child->left_thread) { thread_ptr p = inpred(child); p->right_child = child; } }
Show answer
Y->left_child = M->left_child= K,Y->left_thread= TRUE (M's old predecessor thread passes to Y).Y->right_child = M,Y->right_thread = TRUE: Y's successor is M.M->left_child = Y,M->left_thread = FALSE: a real link.- Since Y's left flag is TRUE, the if block does not run: K, which comes just before Y, has a real right child (M), so there is no thread to fix.
New sequence: N L Q O K Y M P.