How unit 3 is examined
Terminology, BSTs with traversals and AVL rotations carry most marks; B-tree, heap, red-black, expression trees and Huffman are asked once each.
Definitions - Height, depth, order, degree etc
High weight
Definition. A tree is a finite set of nodes with one special node called the root, where the remaining nodes form disjoint subtrees, each itself a tree.
Key points.
- Depth of a node is the number of edges from the root to it, so the root has depth 0.
- Height of a node is the number of edges on the longest path down to a leaf, so a leaf has height 0; tree height is the root's height.
- Degree of a node is the number of children it has; the degree of a tree is the largest node degree.
- Order is the maximum number of children any node may have (5 for a B-tree of order 5), fixed for the tree, whereas degree is counted per node.
- A leaf has degree 0; a forest is a set of disjoint trees, left when the root is deleted.
- An extended binary tree (2-tree) replaces every null pointer with an external square node, so every original node, leaves included, has exactly 2 children.
Internal and external nodes. An internal node has at least one child; an external node is a leaf, or, in an extended tree, a square box standing for a null position. Extending $A(B(D,E),C(,F))$ makes all $n=6$ nodes internal and adds $n+1=7$ boxes.
Internal path length $I$ (sum of internal-node depths) $=0+1+1+2+2+2=8$; external path length $E$ (sum of box depths) $=3\times6+2=20$, and $E=I+2n=8+12=20$. Average search cost is $(I+n)/n=14/6\approx2.33$ comparisons if successful, $E/(n+1)=20/7\approx2.86$ if not; Huffman coding minimises weighted $E$.
| Height | Depth |
|---|---|
| Counted downward from the node to its deepest leaf | Counted upward from the node to the root |
| Height of a leaf is 0 | Depth of the root is 0 |
| Tree height = height of root | Tree height = maximum depth |
| Degree | Order |
| --- | --- |
| Number of children of one node | Maximum children allowed in any node |
| Differs from node to node | Fixed for the whole tree |
Proof (induction on $h$). Base: $h=0$ is the root alone, $2^1-1=1$ node. Step: assume $N(h-1)\le2^h-1$; height $h$ means a root plus two subtrees of height $\le h-1$, so $N(h)\le1+2N(h-1)=1+2(2^h-1)=2^{h+1}-1$. Directly, level $i$ holds at most $2^i$ nodes:
$$N=\sum_{i=0}^{h}2^i=1+2+4+\dots+2^h=\frac{2^{h+1}-1}{2-1}=2^{h+1}-1$$
Maximum $=2^{h+1}-1$ (one node when $h=0$).
Answer frame. Open with the definition and a labelled tree; give the two tables; for internal/external nodes define both, draw the extended tree, compute $I$, $E$, search costs; prove the maximum by induction.
Asked: [7 marks] (Jun 2023, Jun 2024, Dec 2025) Differences between (i) height and depth (ii) order and degree Asked: [7 marks] (Dec 2025) Define height, depth, degree of a node and forest Asked: [4 marks] (Dec 2023) Define extended binary tree with example Asked: [7 marks] (Jun 2023, Jun 2024) Show that the maximum number of nodes in a binary tree of height $h$ is $2^{h+1}-1$ Asked: [7 marks] (Dec 2022) Short note on internal trees, explain with example
Binary Search Tree - Operations, Traversal, Search
High weight
Definition. A binary search tree (BST) is a binary tree in which, for every node, all keys in the left subtree are smaller and all keys in the right subtree are larger than the node's key.
Key points.
- Search goes left if the key is smaller and right if larger until a match or null; it costs $O(h)$: $O(\log n)$ balanced, $O(n)$ skewed.
- Insertion attaches the new node as a leaf where the search ended.
- Deleting a leaf removes it; deleting a node with one child replaces it by that child.
- Deleting a node with two children copies in the inorder successor (smallest key of the right subtree) or predecessor, then deletes that node, which has at most one child.
- Traversals are recursive: inorder is Left-Root-Right, preorder is Root-Left-Right, postorder is Left-Right-Root.
- Uses: inorder lists a BST in ascending order; preorder copies a tree and gives prefix form; postorder deletes or frees a tree (children first) and gives postfix form.
struct node *insert(struct node *t, int key) {
if (t == NULL) {
t = malloc(sizeof(struct node));
t->data = key; t->left = t->right = NULL;
} else if (key < t->data) t->left = insert(t->left, key);
else if (key > t->data) t->right = insert(t->right, key);
return t;
}
Example (Dec 2023, Dec 2024). Insert 45, 32, 90, 34, 68, 72, 15, 24, 30, 66, 11, 50, 10:
| Delete | Case | Tree afterwards |
|---|---|---|
| 45 | two children, successor 50 replaces it | 50(32(15(11(10,),24(,30)),34),90(68(66,72),)) |
| 90 | one child 68 takes its place | 50(32(15(11(10,),24(,30)),34),68(66,72)) |
| 11 | one child 10 takes its place | 50(32(15(10,24(,30)),34),68(66,72)) |
Second variant: insert 45, 26, 10, 60, 70, 30, 40; delete 10 (leaf), 60 (70 replaces it), 45 (successor 70):
void inorder(struct node *t) { if (t) { inorder(t->left); printf("%d ", t->data); inorder(t->right); } }
void preorder(struct node *t) { if (t) { printf("%d ", t->data); preorder(t->left); preorder(t->right); } }
void postorder(struct node *t) { if (t) { postorder(t->left); postorder(t->right); printf("%d ", t->data); } }
Steps: tree from inorder and postorder.
Step 1: The last postorder element is the root.
Step 2: Find it in inorder; keys to its left form the left subtree, keys to its right the right subtree.
Step 3: Split postorder by the same sizes (left part first) and repeat for each subtree.
Root A splits inorder into DGB (root B) and HEICF (root C).
For inorder DKIBAEGHJFC and postorder KDIEAGBFCJH the root is H; preorder H B I D K G A E J C F.
Expression tree. The lowest-precedence operator applied last is the root; operands are leaves.
Inorder $a+b*c+d*e+f*g$; preorder $+\,+\,a\,*\,b\,c\,*\,+\,*\,d\,e\,f\,g$; postorder $a\,b\,c\,*\,+\,d\,e\,*\,f\,+\,g\,*\,+$.
Steps: build from postfix with a stack. Push each operand as a one-node tree; for an operator pop two trees (first popped is the right child), make them its children and push it; the last tree is the root.
| Symbol | Action | Stack |
|---|---|---|
| a, b, c | push | a,b,c |
* |
T1 = *(b, c) |
a,T1 |
+ |
T2 = +(a, T1) |
T2 |
d, e; * |
push; T3 = *(d, e) |
T2,T3 |
f; + |
push; T4 = +(T3, f) |
T2,T4 |
g; * |
push; T5 = *(T4, g) |
T2,T5 |
+ |
root T6 = +(T2, T5) |
T6 |
Answer frame. Open with the BST property; redraw the tree after insertions and each deletion, naming the case; add the code; give traversals, outputs, uses; build expression trees with the stack; for construction take the root from postorder, split, recurse.
Asked: [7 marks] (Dec 2022, Dec 2023) Construct a tree from inorder DGBAHEICF and postorder GDBHIEFCA Asked: [7 marks] (Dec 2023, Dec 2024) Explain BST with example; build it from 45, 32, 90, 34, 68, 72, 15, 24, 30, 66, 11, 50, 10, delete 45, 90, 11; algorithm for insertion. Also build from 45, 26, 10, 60, 70, 30, 40 and delete 10, 60, 45 Asked: [7 marks] (Dec 2023) Construct binary tree from postorder K, D, I, E, A, G, B, F, C, J, H and inorder D, K, I, B, A, E, G, H, J, F, C; find its preorder Asked: [7 marks] (Dec 2024) How to insert and delete in a BST; write the insertion code Asked: [14 marks] (Dec 2024) Expression tree for $(a+b*c)+((d*e+f)*g)$ with preorder and postorder outputs Asked: [7 marks] (Dec 2025) Define BST; explain inorder, preorder, postorder algorithms with example
AVL Tree
High weight
Definition. ==An AVL tree is a self-balancing binary search tree in which the balance factor $BF=h_L-h_R$ of every node is -1, 0 or +1.==
Key points.
- A node with $|BF|=2$ is unbalanced and is repaired by a rotation.
- Insertion is done exactly as in a BST, then heights are updated upward and the lowest unbalanced node is rebalanced.
- LL (key in the left subtree of the left child) needs one right rotation, and RR, its mirror, one left rotation.
- LR (right subtree of the left child) needs a left rotation at the child, then a right rotation at the node; RL is the mirror.
- One rotation (single or double) after an insertion restores balance for the whole tree, and rotations keep the inorder order.
- Deletion is as in a BST, then walk up from the deleted node updating BFs; at an unbalanced $A$ after a right deletion, its left child $B$ has BF 0 (R0), +1 (R1) or -1 (R-1); L0, L1, L-1 mirror them.
- R0 and R1 are a right rotation and R-1 an LR rotation; R0 keeps the subtree height, so the walk stops, while R1 and R-1 shrink it, so the walk continues, possibly to the root with $O(\log n)$ rotations.
- Height is always $O(\log n)$, so search, insertion and deletion cost $O(\log n)$.
Diagram (rotations). $z$ is the lowest unbalanced node, $y$ its child, $x$ its grandchild on the new key's path; T1-T4 keep their left-to-right order.
BF update. Recompute $h=1+\max(h_L,h_R)$ bottom-up; an insertion rotation leaves the new root with BF 0 and the old height, but after deletion case R0 (L0) the new root has BF -1 (+1).
Example (Nov 2022 to Jun 2024). Insert 48, 25, 10, 5, 7, 3, 28, 20, 8, 16 (superscripts are BFs):
Example (Nov 2022, Dec 2023, Jun 2024). Insert the 18 keys 485 to 35 in the given order. Each tree follows its key and any rotation, with all BFs; labels give the key, case and node that hit $\pm2$.
Second variant: insert 14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20.
Deletion example. Deleting 40, right of $A$ = 30, leaves BF +2; $B$ = 20.
L0, L1 (left rotation) and L-1 (RL) are the mirrors. R0 keeps height 2 and stops the walk; R1 and R-1 drop it to 1 and continue it.
Answer frame. Open with the definition and BF rule; draw LL, RR, LR, RL with $z$, $y$, $x$, T1-T4; redraw the tree with BFs after each key, naming case and node; for deletion give the upward walk and R0, R1, R-1; close with height $O(\log n)$.
Pitfall: Rotate at the lowest unbalanced node on the path from the new key, not the root, and re-check BFs after each key.
Asked: [7 marks] (Dec 2022, Nov 2022, Dec 2023, Jun 2024) Explain AVL tree; insert 48, 25, 10, 5, 7, 3, 28, 20, 8, 16 Asked: [7 marks] (Nov 2022, Dec 2023, Jun 2024) Draw an AVL tree for 485, 575, 655, 745, 830, 910, 100, 110, 520, 130, 340, 450, 365, 525, 204, 155, 130, 35; define AVL, explain rotations, construct for 14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20 Asked: [7 marks] (Dec 2024) Illustrate insertion and deletion in an AVL tree with examples Asked: [7 marks] (Dec 2025) What are AVL trees? Explain LL, RR, LR, RL rotations
Heap
Low weight
Definition. A binary heap is a complete binary tree in which every node is at least as large as its children (max-heap) or at most as large (min-heap).
Key points.
- It is stored in an array where the children of index $i$ are $2i+1$ and $2i+2$, and the maximum sits at index 0, read in $O(1)$.
- Insert places the key at the next free leaf and moves it up while it beats its parent, in $O(\log n)$: 40 into
[30,20,25]gives[40,30,25,20]. - Heapify$(i)$ swaps node $i$ with its larger child while it is smaller, repeating at that child, and costs $O(\log n)$.
- Delete-max removes the root, moves the last element to the root and heapifies it, costing $O(\log n)$.
- Build-heap heapifies indices $\lfloor n/2\rfloor-1$ down to 0 in $O(n)$: at most $\lceil n/2^{h+1}\rceil$ nodes at height $h$ each sink $\le h$ levels, costing $\sum_{h}\frac{n}{2^{h+1}}\,h=\frac{n}{2}\sum_{h}\frac{h}{2^h}\le\frac{n}{2}\cdot2=n$, since $\sum_{h\ge0}h/2^h=2$.
Example (build max-heap). [5,3,17,10,84,19,6,22,9], $n=9$, so heapify $i=3,2,1,0$:
| $i$ | Swaps | Array |
|---|---|---|
| 3 | 10 with child 22 | 5,3,17,22,84,19,6,10,9 |
| 2 | 17 with child 19 | 5,3,19,22,84,17,6,10,9 |
| 1 | 3 with child 84 | 5,84,19,22,3,17,6,10,9 |
| 0 | 5 with 84, then 22, then 10 | 84,22,19,10,3,17,6,5,9 |
Example (delete-max). Removing 84 moves the last key 9 to the root, which sinks past 22 and then 10: 22,10,19,9,3,17,6,5.
Asked: [7 marks] (Dec 2022) Explain analysis of heap operations with example
Applications and comparison of various types of tree
Low weight
Definition. An expression tree is a binary tree whose leaves are operands and whose internal nodes are operators, so that its postorder gives postfix and its preorder gives prefix.
Key points.
- For $(3a-b)^2(4c+2d)^3$ the root is
*, each side is rooted at^, and products such as $3a$ are*with children 3 and a. - Uses: BST for searching, AVL and red-black for guaranteed $O(\log n)$, heap for priority queues, B-tree for databases, Huffman tree for compression.
- A tree is stored in memory either as an array (children of $i$ at $2i+1$, $2i+2$, parent at $\lfloor(i-1)/2\rfloor$; direct access, no pointers, but space wasted on skewed trees, fixed size, costly insertion and deletion as elements shift) or as linked nodes (left, right pointers; dynamic, extra pointer memory).
| Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|---|
| Tree 50(30(20,40),70(60,80)) | 50 | 30 | 70 | 20 | 40 | 60 | 80 |
| Right-skewed 10(,20(,30)) | 10 | - | 20 | - | - | - | 30 |
The full tree fills all 7 slots (parent of index 4 is $\lfloor3/2\rfloor=1$, key 30); the skewed 3-node tree wastes 4 of 7 slots, as height $h$ needs $2^{h+1}-1$.
struct node { int data; struct node *left, *right; };
root -> [ * | 50 | * ]
/ \
[ * | 30 | * ] [ * | 70 | * ]
/ \ / \
[N|20|N] [N|40|N] [N|60|N] [N|80|N] N = NULL
- A Huffman tree is a full binary tree whose leaves carry the symbol weights and whose weighted external path length $\sum w_i l_i$ is minimum, so the root-to-leaf paths give optimal prefix codes.
- Algorithm: put every symbol with its frequency in a min-heap; repeatedly remove the two smallest, merge them under a new node of their total weight and insert it until one tree remains; label left edges 0, right edges 1. "data structures" has t 3; a, s, r, u 2; d, space, c, e 1.
Ties: leaves before merged nodes, leaves in occurrence order (d, a, t, sp, s, r, u, c, e), merged nodes by creation; the first removed is the left child.
| Step | Merge | Min-heap after the merge (ds = d+sp, dc = ds+ce) |
|---|---|---|
| 1 | d+sp = 2 | c1,e1,a2,s2,r2,u2,ds2,t3 |
| 2 | c+e = 2 | a2,s2,r2,u2,ds2,ce2,t3 |
| 3 | a+s = 4 | r2,u2,ds2,ce2,t3,as4 |
| 4 | r+u = 4 | ds2,ce2,t3,as4,ru4 |
| 5 | ds+ce = 4 | t3,as4,ru4,dc4 |
| 6 | t+as = 7 | ru4,dc4,7 |
| 7 | ru+dc = 8 | 7,8 |
| 8 | 7+8 = 15 | 15 |
Codes: t 00, a 010, s 011, r 100, u 101, d 1100, space 1101, c 1110, e 1111; total $6+24+16=$ 46 bits, against $15\times8=$ 120 bits in 8-bit code (62% saved); any tie rule gives 46.
Asked: [7 marks] (Dec 2023) Construct an expression tree for $(3a-b)^2(4c+2d)^3$ Asked: [7 marks] (Dec 2023) How a tree is stored in memory; advantages and disadvantages of array representation Asked: [7 marks] (Dec 2023) Define Huffman tree, algorithm, construct for "data structures" with optimal codes
Introduction to forest
Not asked since 2022
Definition. A forest is a set of zero or more disjoint trees.
Key points.
- Removing the root of a tree leaves a forest of its subtrees.
- A forest converts to one binary tree: left pointer to the first child, right pointer to the next sibling.
- Forest traversals follow the corresponding binary tree.
multi-way Tree
Not asked since 2022
Definition. An m-way tree is a tree in which each node holds up to m-1 sorted keys and has up to m children.
Key points.
- A node with $k$ keys has $k+1$ children, and subtree keys lie between adjacent keys.
- Search picks the child whose key range contains the target.
- It is not balanced by itself; B-trees add balance rules.
B tree
Low weight
Definition. A B-tree of order m is a balanced multiway search tree in which every node has at most m children and m-1 keys, every non-root node has at least $\lceil m/2\rceil$ children, and all leaves are on one level.
Key points.
- For order 5 a node holds at most 4 keys and 5 children, and non-root nodes hold at least 2 keys.
- Keys go into the correct leaf in sorted order; when a node reaches 5 keys it splits: the middle (3rd) key moves up to the parent and the remaining keys form two nodes.
- A split that overfills the parent splits it too; a root split adds a level.
Example (Jun 2023). Insert 78, 21, 11, 97, 85, 74, 63, 45, 42, 57, 20, 16, 19, 32, 30, 31:
| Insert | Result |
|---|---|
| 78, 21, 11, 97 | [11,21,78,97] |
| 85 | 78 goes up: [78]โ[11,21][85,97] |
| 74, 63 | [11,21,63,74] |
| 45 | 45 goes up: [45,78]โ[11,21][63,74][85,97] |
| 42, 57 | [45,78]โ[11,21,42][57,63,74][85,97] |
| 20 | [11,20,21,42] |
| 16 | 20 goes up: [20,45,78]โ[11,16][21,42][57,63,74][85,97] |
| 19, 32, 30 | [20,45,78]โ[11,16,19][21,30,32,42][57,63,74][85,97] |
| 31 | [21,30,31,32,42] splits, 31 goes up |
Answer frame. Open with the order-5 properties; redraw at each split; close with the final tree.
Asked: [7 marks] (Jun 2023) Construct a B-tree of order 5 for 78, 21, 11, 97, 85, 74, 63, 45, 42, 57, 20, 16, 19, 32, 30, 31
B+ tree
Not asked since 2022
Definition. A B+ tree is a B-tree variant in which all data records are stored in leaf nodes and internal nodes hold only index keys.
Key points.
- Leaves are linked in sequence, so range and sequential scans are fast.
- Every search reaches a leaf, so cost is uniform.
- Internal nodes hold no data, so more keys fit per node and the tree is shallower.
B* tree and red-black tree
Low weight
Definition. A red-black tree is a self-balancing binary search tree in which every node is coloured red or black and the colouring rules keep the longest path at most twice the shortest.
Key points.
- The root is black and every NIL leaf is black.
- A red node cannot have a red child, so no two consecutive red nodes appear on a path.
- Every path from a node to its descendant NIL leaves contains the same number of black nodes, the black-height.
- Search, insertion and deletion take $O(\log n)$, with rebalancing by recolouring and rotations.
- A B* tree is a B-tree that keeps nodes at least two-thirds full by sharing keys with a sibling before splitting.
- A new key is a red leaf; under a red parent, a red uncle means recolour (parent, uncle black, grandparent red, repeat upward), a black uncle one or two rotations plus recolouring.
- Deleting a black node leaves an extra black, fixed by recolouring or at most 3 rotations; being looser than AVL ($2\log_2(n+1)$ against $1.44\log_2n$ height), it rotates less.
Asked: [7 marks] (Dec 2022) Explain Red Black Trees and discuss their properties
Last-minute revision
- Maximum nodes in a binary tree of height $h$ is $2^{h+1}-1$.
- BST inorder is ascending; two-child deletion uses the inorder successor.
- AVL: $BF=h_L-h_R\in\{-1,0,+1\}$; LL, RR, LR, RL.
- Heap: children $2i+1$, $2i+2$; insert, delete $O(\log n)$; build $O(n)$.
- B-tree of order 5: at most 4 keys; 5 keys split, middle goes up.
- Red-black: root black, no red-red, equal black-height.
- Huffman "data structures": 46 bits against 120.
- Extended tree: $n$ internal, $n+1$ external; $E=I+2n$.
Memory hooks
- Height goes down, depth goes up to the root.
- AVL rotation name is the path of the new key: LL, RR, then zig-zag LR, RL.
- Postorder's last is the root, inorder splits the sides.
Coverage checklist
- Definitions - Height, depth, order, degree etc: differences, definitions, extended tree, internal nodes, maximum nodes.
- Binary Search Tree - Operations, Traversal, Search: BST builds, code, traversals, expression tree.
- AVL Tree: all four AVL questions.
- Heap: heap operations.
- Applications and comparison of various types of tree: expression tree, memory, Huffman.
- Introduction to forest: definition.
- multi-way Tree: definition.
- B tree: order 5 construction.
- B+ tree: definition.
- B* tree and red-black tree: red-black properties.