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 k children, each node can hold the data and k pointers:
This is simple, but wasteful: a tree with n nodes has nk link fields, and only n−1 of them are used (one per edge), so n(k−1)+1 links are NULL. For k=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 n nodes once, so it takes O(n) time.
Key takeaways
k-pointer nodes waste n(k−1)+1 of nk 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).
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
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 k child pointers, where k is the maximum degree. It is simple and gives direct access to the j-th child, but of the nk pointers only n−1 are used, so n(k−1)+1 are NULL (very wasteful for large k), and k 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 j-th child takes j steps. Representation (2) also shows that every general tree can be stored as a binary tree.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-208/trees/general-trees-representation-and-traversal/ to read this page.