A tree arranges data in a hierarchy, with one element at the top and others branching below it.

Definition and Terminology

A tree is a finite set of nodes with one special node called the root, and the remaining nodes divided into disjoint subsets, each of which is itself a tree, called a subtree. Equivalently, it is a connected graph with no cycles. The nodes directly below a node are its children, and the node above is its parent. Nodes with the same parent are siblings. A node with no children is a leaf (terminal node), and others are internal nodes. The degree of a node is its number of children. The level of the root is 0, and the level of a child is one more than its parent's. The height (depth) of a tree is the largest level of any node. Every node except the root has exactly one parent, and a tree with n nodes has n − 1 edges. The nodes on the path from the root to a node are its ancestors, and the nodes below it are its descendants.

Definition: Binary tree. A tree in which each node has at most two children, called the left child and the right child.

Binary Trees

A binary tree is a tree in which every node has at most two children, the left and the right child, and the order matters. A full (strictly) binary tree has 0 or 2 children at every node. A complete binary tree has all levels full except possibly the last, which is filled from the left. A perfect binary tree has all leaves at the same level. A binary tree of height h has at most 2^(h+1) − 1 nodes, and level i has at most 2^i nodes. A skewed tree has every node with only one child, and behaves like a linked list.

Representation

In the linked representation, each node has three fields: left, data and right, with NULL for a missing child. In the array (sequential) representation, the root is at index 0, and the children of the node at index i are at 2i + 1 and 2i + 2, while its parent is at (i − 1)/2. The array form suits complete trees, such as heaps, but wastes space for sparse or skewed trees.

Table 4.3: Tree terminology

Term Meaning Example (tree below)
Root Top node A
Leaf Node with no children D, E, F
Level Distance from the root Level of D = 2
Height Largest level 2
Degree Number of children deg(B) = 2

Example 4.3: Array representation

Problem: Store the complete binary tree with root A, children B and C, B's children D and E, and C's left child F in an array, and find the parent and children of B.

Solution:

  1. Level order: A, B, C, D, E, F at indices 0 to 5.
  2. B is at index 1. Its children are at 2(1) + 1 = 3 (D) and 2(1) + 2 = 4 (E).
  3. Its parent is at (1 − 1)/2 = 0 (A).

Result: The array is [A, B, C, D, E, F], with B's children D and E and its parent A.

#include <stdio.h>
int main(void) {
    char t[] = "ABCDEF";
    int n = 6;
    for (int i = 0; i < n; i++) {
        int l = 2 * i + 1, r = 2 * i + 2;
        printf("%c: left=%c right=%c\n", t[i],
               l < n ? t[l] : '-', r < n ? t[r] : '-');
    }
    return 0;
}

Output

A: left=B right=C
B: left=D right=E
C: left=F right=-
D: left=- right=-
E: left=- right=-
F: left=- right=-

Common mistakes

  • Thinking that the left and right children of a binary tree are interchangeable.
  • Taking the level of the root as 1 in one place and 0 in another.
  • Using the array representation for a skewed tree, which wastes space.

Key takeaways

  • A tree has a root, and every other node has exactly one parent.
  • A tree with n nodes has n − 1 edges.
  • A binary tree node has at most two children, left and right.
  • In an array, the children of i are at 2i + 1 and 2i + 2.

Practice questions

  1. What is a leaf node?
  2. What is the maximum number of nodes in a binary tree of height h?
  3. Where are the children of the node at index i in the array form?

Answers to the practice questions

  1. A node with no children.
  2. 2^(h+1) − 1.
  3. At 2i + 1 and 2i + 2.

Sources

  • Official BCA syllabus Data Structures, course code 25BCA402DS02, Unit IV