MCS-208 · Unit 6

Trees

Section 1 of 8

Block 2 · Unit 6Trees11 min · Section 1 of 8

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 k=0k = 0 the tree is a single node.

Vocabulary

A rooted tree with levels
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
TermMeaningIn the tree above
RootThe only node with no parentA
Parent, childA node directly above / below another, joined by an edgeB is the parent of E and F
SiblingsChildren of the same parentB, C, D
LeafA node with no children (also called external or terminal node)C, E, F, G
Internal nodeA node with at least one childA, B, D
Degree of a nodeNumber of its childrendeg(A) = 3, deg(D) = 1
Degree of a treeLargest degree of any node3
Ancestor / descendantNodes on the path up to the root / below a nodeA is an ancestor of G
LevelRoot at level 1, its children at level 2, and so onG is at level 3
Depth of a nodeNumber of edges from the root to the nodedepth(G) = 2
Height of a treeNumber 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'.

OperationMeaningError case
null(T)True if T is empty—
leaf(T)True if T is a single node with two empty subtreesleaf(nil)
fork(e, T, T')Make a tree with root e and subtrees T, T'—
left(T), right(T)The left / right subtreeleft(nil), right(nil)
contents(T)The element at the rootcontents(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 k≥0k \ge 0 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 1 · multiple choice

In the example tree (A with children B, C, D; B with E, F; D with G), which nodes are leaves?

Show answer

Answer: B. C, E, F, G

A leaf is any node without children; C has none even though it is on level 2. B and D are internal nodes.

Question 2 · multiple choice

What is the degree of the tree in the example?

Show answer

Answer: C. 3

The degree of a tree is the largest number of children of any node; A has 3. 7 is the number of nodes.

Question 3 · multiple choice

Using the study material’s ADT, what is height(fork(x, nil, nil))?

Show answer

Answer: B. 1

1 + max(height(nil), height(nil)) = 1 + 0 = 1. A single node has height 1 in this convention; the empty tree has height 0.

Question 4 · multiple choice

Which statement about a tree with root at level 1 is true?

Show answer

Answer: C. A node at level ℓ\ell has depth ℓ−1\ell - 1.

Depth counts edges from the root, and each level adds one edge; the root has depth 0. Siblings share a parent, so they are always on the same level.

Question 5 · multiple choice

Why is a tree called a non-linear data structure?

Show answer

Answer: D. Because a node can have several children, so there is no single next element

In linear structures every element has at most one successor. A tree branches.

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 k≥0k \ge 0 disjoint sets T1,…,TkT_1, \dots, T_k, 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).

↑ ↓ move↵ openTab switch type