MCS-208 · Unit 6

Trees

Section 3 of 8

Block 2 · Unit 6Trees10 min · Section 3 of 8

6.3General Trees: Representation and Traversal

Covers IGNOU MCS-208 §6.3, 6.4

A general tree allows any number of children per node. This page shows two ways to store such a tree and the ways to visit all its nodes, using a file system as the running example.

Representation 1: k child pointers

If no node has more than kk children, each node can hold the data and kk pointers:

Node of a k-ary tree
+------+-------+-------+-----+-------+
| data | link1 | link2 | ... | linkk |
+------+-------+-------+-----+-------+

This is simple, but wasteful: a tree with nn nodes has nknk link fields, and only n−1n - 1 of them are used (one per edge), so n(k−1)+1n(k - 1) + 1 links are NULL. For k=10k = 10 that is about 90% wasted links.

Representation 2: first child, next sibling

Each node stores two links: its first child and its next sibling. Any general tree can be stored this way with exactly two links per node. (This is also how a general tree is turned into a binary tree: “first child” becomes “left”, “next sibling” becomes “right”.)

Traversals of a general tree

A traversal visits every node exactly once. For a general tree:

  • Preorder: visit the node, then traverse each child’s subtree from left to right. A book’s table of contents is a preorder listing: a chapter appears before its sections.
  • Postorder: traverse each child’s subtree, then visit the node. Computing the space used by a folder needs postorder: a folder’s total is known only after all its files and sub-folders are totalled.
  • Level order: visit the nodes level by level, left to right, using a queue (Unit 5).

Each traversal visits each of the nn nodes once, so it takes O(n)O(n) time.

Key takeaways

  • kk-pointer nodes waste n(k−1)+1n(k - 1) + 1 of nknk links.
  • First-child/next-sibling uses two links per node for any general tree.
  • Preorder: node before children; postorder: children before node; level order: by levels with a queue.
  • Each traversal is O(n)O(n).

Practice questions

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

Question 1 · multiple choice

A 3-ary tree with 10 nodes is stored with 3 child pointers per node. How many pointers are NULL?

Show answer

Answer: A. 21

There are 3×10=303 \times 10 = 30 pointers and 10−1=910 - 1 = 9 edges, so 30−9=2130 - 9 = 21 are NULL (=n(k−1)+1= n(k - 1) + 1).

Question 2 · multiple choice

Which traversal computes the total size of each folder in a file system?

Show answer

Answer: B. Postorder

A folder’s total depends on the totals of everything inside it, so its children must be processed first.

Question 3 · multiple choice

In the first-child/next-sibling representation, how many link fields does each node need?

Show answer

Answer: D. Two

One link to the first child and one to the next sibling, whatever the number of children.

Question 4 · multiple choice

What does this program print?

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

typedef struct g {
    char name;
    struct g *child, *sibling;
} G;

void pre(const G *n)
{
    for (; n != NULL; n = n->sibling) {
        printf("%c", n->name);
        pre(n->child);
    }
}

int main(void)
{
    G e = {'E', NULL, NULL}, d = {'D', NULL, NULL};
    G c = {'C', NULL, NULL}, b = {'B', &d, &c};
    d.sibling = &e;
    G a = {'A', &b, NULL};
    pre(&a);
    printf("\n");
    return 0;
}
Show answer

Answer: B. ABDEC

A’s first child is B, whose children are D and E (D’s sibling is E); C is B’s sibling. Preorder visits A, B, then B’s subtree D, E, then C. “DEBCA” is postorder.

Question 5 · multiple choice

Which structure is needed for a level-order traversal?

Show answer

Answer: B. A queue

Nodes must be visited in the order they were discovered, level by level: FIFO. A stack would give a depth-first order.

Question 6 · short answer

Explain the two ways of representing a general tree in memory and compare them.

Show worked answer

(1) Each node holds the data and kk child pointers, where kk is the maximum degree. It is simple and gives direct access to the jj-th child, but of the nknk pointers only n−1n - 1 are used, so n(k−1)+1n(k - 1) + 1 are NULL (very wasteful for large kk), and kk must be known in advance. (2) First-child/next-sibling: each node holds the data, a pointer to its first child and a pointer to its next sibling. It uses only two pointers per node for any number of children, but reaching the jj-th child takes jj steps. Representation (2) also shows that every general tree can be stored as a binary tree.

↑ ↓ move↵ openTab switch type