MCS-208 · Unit 6

Trees

Section 7 of 8

Block 2 · Unit 6Trees17 min · Section 7 of 8

6.7Non-Recursive Traversals and Threaded Binary Trees

Covers IGNOU MCS-208 §6.7.2

Recursion hides a stack: the call stack (Unit 4). We can make that stack explicit and write traversals as loops. Or we can avoid a stack completely by storing extra links, called threads, in the tree. This page does both.

Preorder with an explicit stack

  1. Push the root (if the tree is not empty).
  2. While the stack is not empty: pop a node vv, visit it, push vv‘s right child if it exists, then push its left child if it exists.

The right child is pushed first so that the left child is on top and is processed first.

Inorder with an explicit stack

  1. Set cur = root.
  2. While cur is not NULL or the stack is not empty:
    • while cur is not NULL: push cur, set cur = cur->left;
    • pop a node, visit it, and set cur to its right child.

Threaded binary trees

A binary tree with nn nodes has n+1n + 1 NULL links. A threaded binary tree puts these unused links to work: a NULL right link is replaced by a thread to the node’s inorder successor (and, in a fully threaded tree, a NULL left link by a thread to its inorder predecessor). A one-bit flag in each node says whether the link is a real child or a thread.

Node of a right-threaded binary tree
struct tnode {
    struct tnode *left;
    int value;
    struct tnode *right;   /* right child, or thread to the inorder successor */
    bool rthread;          /* true if right is a thread                       */
};

Inorder traversal then needs no stack: start at the leftmost node; after visiting a node, if its right link is a thread, follow it; otherwise go to the leftmost node of its right subtree.

Key takeaways

  • Iterative preorder: pop, visit, push right then left (non-NULL only).
  • Iterative inorder: push the left chain, pop and visit, then go right.
  • Threads reuse the n+1n + 1 NULL links to point to inorder successors (and predecessors), with flag bits.
  • Threaded inorder traversal needs neither recursion nor a stack.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

In iterative preorder, why is the right child pushed before the left child?

Show answer

Answer: B. So that the left child is on top of the stack and is processed first

A stack returns the last item pushed; pushing left last makes left come out first, giving node-left-right.

Question 2 · multiple choice

Iterative inorder is run on the tree with root 1, left child 2 and right child 3. What does the stack hold just before the first pop?

Show answer

Answer: C. 1 and 2 (2 on top)

The algorithm first pushes the left chain 1, 2; the first pop returns 2, which is visited first. 3 is pushed only after 1 is visited.

Question 3 · multiple choice

In a right-threaded binary tree, where does the thread of a node with no right child point?

Show answer

Answer: D. To its inorder successor

The thread leads to the node that comes next in inorder. That is sometimes the parent (G → D), but not always (J → A, its grandparent’s parent).

Question 4 · multiple choice

What does this program print?

C · q6-stack.c
#include <stdio.h>
#include <stddef.h>

typedef struct n {
    struct n *l;
    int v;
    struct n *r;
} N;

int main(void)
{
    N d = {NULL, 4, NULL}, e = {NULL, 5, NULL}, c = {NULL, 3, NULL};
    N b = {&d, 2, &e}, a = {&b, 1, &c};
    N *st[10];
    int top = -1;
    st[++top] = &a;
    while (top >= 0) {
        N *v = st[top--];
        printf("%d ", v->v);
        if (v->r) st[++top] = v->r;
        if (v->l) st[++top] = v->l;
    }
    printf("\n");
    return 0;
}
Show answer

Answer: C. 1 2 4 5 3

This is the iterative preorder: 1, then its left subtree (2 4 5), then 3. “1 3 2 5 4” would result from pushing left before right.

Question 5 · multiple choice

How many NULL links can a binary tree with 10 nodes turn into threads?

Show answer

Answer: C. 11

2n−(n−1)=n+1=112n - (n - 1) = n + 1 = 11 links are NULL and available for threads.

Question 6 · short answer

Write a non-recursive algorithm for inorder traversal using a stack and trace it on the tree with root A, left child B (with left child D) and right child C.

Show worked answer

Algorithm: cur = root; while cur ≠ NULL or stack not empty: { while cur ≠ NULL: push cur, cur = cur→left; cur = pop(); visit cur; cur = cur→right }. Trace: push A, push B, push D (stack A B D); D’s left is NULL; pop D, visit D, cur = D→right = NULL; pop B, visit B, cur = NULL; pop A, visit A, cur = C; push C; C’s left is NULL; pop C, visit C, cur = NULL; stack empty: stop. Output: D B A C.

↑ ↓ move↵ openTab switch type