Unit 2: Trees
Data Structures notes · PTU syllabus (PGCA1913)
On this page
- Unit summary
- Tree definitions: height, depth, order, degree and relationships
- Binary trees
- Binary tree theorems
- Complete and almost complete binary trees
- Binary tree representations
- Recursive and non-recursive traversals
- Expression trees
- Threaded binary trees
- Forests
- Heap definition
- Key terms
- Quick revision
- Important questions
Unit summary
Trees store hierarchies and support fast search. This unit covers tree terminology — height, depth, order, degree and parent–child relationships — binary trees and their theorems, complete and almost complete binary trees, recursive and non-recursive traversals, expression trees, threaded binary trees, forests and the definition of a heap.
After this unit you can
- Define tree terminology and prove binary tree properties
- Distinguish complete and almost complete binary trees
- Implement recursive and non-recursive traversals
- Build expression trees, threaded binary trees, forests and heaps
PTU syllabus topics
- Tree definitions (height, depth, order, degree, parent-child relationships)
- binary trees and theorems
- complete and almost-complete binary trees
- tree traversals (preorder, inorder, postorder) and recursive/non-recursive implementations
- expression trees
- threaded binary trees
- forests
- heap definition
- Root
- Top node with no parent
- Leaf
- Node with no children
- Height
- Longest path from root to a leaf
- Degree
- Number of children of a node
- Complete binary tree
- All levels full except maybe the last, filled left to right
- Heap
- Complete tree where parent ≥ children (max-heap)
Topic 1
Tree definitions: height, depth, order, degree and relationships
A tree is a finite set of nodes with one special node called the root, and the remaining nodes divided into disjoint subtrees.
- Root
- Top node with no parent
- Parent and child
- A node and the nodes directly below it
- Leaf (terminal node)
- A node with no children
- Degree
- Number of children of a node
- Level
- Root is level 0 (or 1); children are one level lower
- Height (depth)
- Number of levels in the tree
- Siblings
- Nodes with the same parent
Topic 2
Binary trees
A binary tree is a tree in which each node has at most two children: a left child and a right child.
- Strictly binary tree: every non-leaf node has exactly two children.
- Complete binary tree: all levels are full except possibly the last, which is filled from the left.
- Full (perfect) binary tree: every level is completely filled.
- Maximum nodes at level l = 2ˡ (root at level 0); maximum nodes in a tree of height h = 2ʰ − 1.
Topic 3
Binary tree theorems
Nodes at level i
At most 2ⁱ (root at level 0)
Nodes in a tree of height h
At most 2ʰ − 1 (h levels)
Leaves and degree-2 nodes
n0 = n2 + 1 for any non-empty binary tree
Height of a complete tree with n nodes
ceil(log₂(n + 1))
Edges
A tree with n nodes has n − 1 edges
- Proof of n0 = n2 + 1: total nodes n = n0 + n1 + n2; every node except the root has one incoming edge, so edges = n − 1 = n1 + 2n2. Equating gives n0 = n2 + 1.
Topic 4
Complete and almost complete binary trees
Levels
Every level completely filled
All levels full except possibly the last
Last level
Full
Filled from left to right without gaps
Nodes for height h
Exactly 2ʰ − 1
Between 2ʰ⁻¹ and 2ʰ − 1
Use
Theoretical bound
Heaps and array representation
- Terminology varies between textbooks: some call the second form "complete" and the first "full" or "perfect".
Topic 5
Binary tree representations
- Array (sequential) representation: root at index 1; for a node at index i, the left child is at 2i and the right child at 2i + 1; the parent is at i/2. Efficient for complete trees, wasteful for skewed trees.
- Linked representation: each node has three fields — left pointer, data, right pointer.
cstruct node {
struct node *left;
int data;
struct node *right;
};Topic 6
Recursive and non-recursive traversals
Traversal means visiting every node exactly once.
- Preorder
- Root, Left, Right
- Inorder
- Left, Root, Right
- Postorder
- Left, Right, Root
Example
For the tree with root A, left child B (children D, E) and right child C: Preorder = A B D E C; Inorder = D B E A C; Postorder = D E B C A.
cvoid inorder(struct node *t) {
if (t != NULL) {
inorder(t->left);
printf("%d ", t->data);
inorder(t->right);
}
}Non-recursive traversal uses an explicit stack in place of recursion. Level-order traversal uses a queue.
Exam tip
A tree can be rebuilt uniquely from its inorder plus preorder (or inorder plus postorder) sequences — a frequent long question.
c/* Non-recursive inorder traversal using an explicit stack */
void inorderIter(struct node *root) {
struct node *stack[100]; int top = -1;
struct node *cur = root;
while (cur != NULL || top != -1) {
while (cur != NULL) { stack[++top] = cur; cur = cur->left; } /* go left */
cur = stack[top--];
printf("%d ", cur->data); /* visit */
cur = cur->right; /* go right */
}
}Topic 7
Expression trees
- An expression tree has operands at leaves and operators at internal nodes; traversals give prefix (preorder), infix (inorder, with brackets) and postfix (postorder) forms.
- 1Scan postfix left to right
- 2Operand: create a node and push it
- 3Operator: pop two nodes, make them right and left children of a new operator node, push it
- 4At the end the stack holds the root
Example
Postfix a b + c d − ×: the root is ×, left subtree + (a, b), right subtree − (c, d). Inorder (a + b) × (c − d).
Topic 8
Threaded binary trees
In a linked binary tree with n nodes, n + 1 pointers are NULL. A threaded binary tree replaces these NULL pointers with threads — pointers to the inorder predecessor (left) or successor (right). This allows inorder traversal without a stack or recursion. Each node keeps a flag to tell a thread from a real child link.
Topic 9
Forests
- A forest is a set of disjoint trees. Removing the root of a tree gives a forest of its subtrees.
- Converting a forest (or general tree) to a binary tree — left-child, right-sibling: each node's first child becomes its left child, and its next sibling becomes its right child; the roots of the trees are linked as right siblings.
- Forest traversals: preorder of the forest equals preorder of its binary-tree form; inorder (postorder of the forest) equals inorder of the binary form.
Topic 10
Heap definition
A heap is a complete binary tree that satisfies the heap property:
- Max-heap: every parent ≥ its children; the largest value is at the root.
- Min-heap: every parent ≤ its children; the smallest value is at the root.
Heaps are stored in arrays and are used for priority queues and heap sort.
- 1Build a max-heap from the array
- 2Swap the root with the last element
Largest goes to the end
- 3Reduce heap size by one
- 4Heapify the root
Restore the heap property
- 5Repeat until one element remains
Heap sort runs in O(n log n) time in all cases.
Key terms
- Degree
- Number of children of a node
- Almost complete binary tree
- Tree full except the last level, filled from the left
- Expression tree
- Binary tree representing an arithmetic expression
- Threaded binary tree
- Tree whose null links point to inorder neighbours
- Forest
- Set of disjoint trees
Quick revision
- Root, parent, child, sibling, leaf, level, depth, height, degree, order.
- 2ⁱ nodes per level; 2ʰ − 1 total; n0 = n2 + 1.
- Complete vs almost complete; array index 2i and 2i + 1 (root at 1).
- Preorder, inorder, postorder, level order; iterative versions with a stack.
- Expression trees from postfix; threads; forest to binary tree; max- and min-heaps.
Important exam questions
Practice questions written to the PTU exam pattern for this unit's syllabus: short answers (Section A style) and long answers (Sections B and C style).
Short-answer questions
- Q1.Define the degree and the height of a tree.
- Q2.Prove that n0 = n2 + 1.
- Q3.Distinguish complete and almost complete binary trees.
- Q4.Which data structure does non-recursive traversal use?
- Q5.What is a threaded binary tree?
- Q6.How is a forest converted to a binary tree?
Long-answer questions
- Q1.Explain binary trees and prove their properties.
- Q2.Explain recursive and non-recursive tree traversals.
- Q3.Construct an expression tree and write its traversals.
- Q4.Explain threaded binary trees, forests and heaps.
Stuck on this unit?
Message SBS on WhatsApp for help with Data Structures, or to ask about studying M.Sc IT at Synetic.
