6.1Tree Terminology and the Tree ADT
Covers IGNOU MCS-208 §6.0, 6.2
A tree organises data in levels, like a family tree or the folders on a disk. Before working with trees we need precise words for their parts. This page defines a tree, its vocabulary and the tree as an abstract data type.
What a tree is
In graph theory a tree is a connected graph with no cycles, so there is exactly one path between any two nodes. In data structures we almost always use a rooted tree, in which one node is chosen as the root and all edges are read as going from a parent down to a child.
The definition is recursive: a tree is a root plus smaller trees. When the tree is a single node.
Vocabulary
Level 1 A <- root
/ | \
Level 2 B C D <- B and D are internal nodes; C is a leaf
/ \ \
Level 3 E F G <- E, F, G are leaves| Term | Meaning | In the tree above |
|---|---|---|
| Root | The only node with no parent | A |
| Parent, child | A node directly above / below another, joined by an edge | B is the parent of E and F |
| Siblings | Children of the same parent | B, C, D |
| Leaf | A node with no children (also called external or terminal node) | C, E, F, G |
| Internal node | A node with at least one child | A, B, D |
| Degree of a node | Number of its children | deg(A) = 3, deg(D) = 1 |
| Degree of a tree | Largest degree of any node | 3 |
| Ancestor / descendant | Nodes on the path up to the root / below a node | A is an ancestor of G |
| Level | Root at level 1, its children at level 2, and so on | G is at level 3 |
| Depth of a node | Number of edges from the root to the node | depth(G) = 2 |
| Height of a tree | Number of levels (some books count edges instead) | 3 |
Trees are non-linear
In a list every element has one successor. In a tree a node can have many children, so there is no single “next” element: a tree is a non-linear, hierarchical structure. Trees are also dynamic: nodes are created and freed as the program runs, usually with pointers.
The tree ADT
The study material specifies binary trees as an ADT in which a tree is either empty (nil) or fork(e, T, T'): an element e with a left subtree T and a right subtree T'.
| Operation | Meaning | Error case |
|---|---|---|
null(T) | True if T is empty | — |
leaf(T) | True if T is a single node with two empty subtrees | leaf(nil) |
fork(e, T, T') | Make a tree with root e and subtrees T, T' | — |
left(T), right(T) | The left / right subtree | left(nil), right(nil) |
contents(T) | The element at the root | contents(nil) |
Height and size (weight) are defined recursively: height(nil) = 0, height(fork(e, T, T')) = 1 + max(height(T), height(T')); weight(nil) = 0, weight(fork(e, T, T')) = 1 + weight(T) + weight(T'). So a single node has height 1.
Key takeaways
- A rooted tree is a root with disjoint subtrees whose roots are its children.
- Leaves have no children; internal nodes have at least one; degree = number of children.
- Root at level 1; height = number of levels; depth of a node = edges from the root.
- Trees are non-linear and hierarchical; their operations are naturally recursive.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Give the recursive definition of a tree and explain the terms root, leaf, degree, level and height with a diagram.
Show worked answer
A tree is a finite set of one or more nodes such that one node is the root and the remaining nodes are partitioned into disjoint sets , each a tree whose root is a child of the root. (Draw: A at the top with children B, C, D; B with children E, F; D with child G.) Root: the node without a parent (A). Leaf: a node with no children (C, E, F, G). Degree of a node: its number of children (A has 3, D has 1); degree of the tree: the maximum (3). Level: the root is at level 1, its children at level 2, etc. (G is at level 3). Height: the number of levels (3).