BCS301 · DATA STRUCTURE · Unit 4
Trees
Unit Scope & Exam Weightage: Unit 4 covers tree terminology, binary trees and their representations, BST operations, traversals, threaded binary trees, Huffman coding, AVL trees, B-trees, and binary heaps. AKTU frequently tests AVL insertion/rotations, BST construction and traversal, Huffman coding, binary tree terminology/path-length proofs, and threaded trees/B-trees/heaps.
Front Page: Importance-Wise Topic Roadmap (PYQ Frequency × Marks)
| Priority | Topic (Exact Syllabus) | PYQs Asked | Marks Weightage | Tag |
|---|---|---|---|---|
| 1 | Concept & Basic Operations for AVL Tree | 2 (2024-25, 2025-26) |
7m × 2 = 14m | Survival Mode Predicted |
| 2 | Binary Search Tree | 1 (2025-26) |
7m | Survival Mode Predicted |
| 3 | Basic terminology used with Tree | 2 (2023-24, 2024-25) |
7m + 2m = 9m | Core Concept |
| 4 | Huffman coding using Binary Tree | 2 (2023-24) |
7m + 2m = 9m | Numerical |
| 5 | Complete Binary Tree, A Extended Binary Trees | 1 (2025-26) |
7m | Core Concept |
| 6 | Binary Trees, Strictly Binary Tree | 2 (2024-25) |
7m × 2 = 14m | Core Concept |
| 7 | Tree Traversal algorithms: Inorder, Preorder and Postorder | 1 (2023-24) |
7m | Core Concept |
| 8 | Threaded Binary trees, B Tree & Binary Heaps | 1 (2024-25) |
2m | Quick Scoring |
2-Hour Last-Night Survival Checklist:
- AVL insertions: Insert like BST, update balance factor , and apply LL/RR/LR/RL rotations. Show tree after every insertion causing imbalance.
- BST operations: Construct BST from sequence, perform inorder/preorder/postorder; remember inorder of BST gives sorted order.
- Huffman coding: Build min-priority by frequency, repeatedly combine two smallest nodes, assign 0/1 on edges, compute average code length .
- Tree terminology: Revise root, leaf, internal node, degree, height, depth, level, path length, complete/full/strict/extended binary trees.
- Traversals and threaded trees: Practice recursive and non-recursive traversals; know threaded tree uses null pointers to store inorder predecessor/successor.
1. Basic Terminology and Binary Tree Types
Tree: A tree is a non-linear hierarchical data structure consisting of nodes connected by edges, with one distinguished node called root and no cycles. For nodes, a tree has exactly edges.
1.1 Basic Terminology Used with Tree
| Term | Meaning |
|---|---|
| Root | Topmost node with no parent. |
| Parent/Child | Immediate predecessor/successor relation. |
| Siblings | Nodes with same parent. |
| Leaf/External node | Node with no children. |
| Internal node | Node with at least one child. |
| Degree of node | Number of children. |
| Level/Depth | Distance from root (root at level 0 or 1 by convention; state convention). |
| Height | Maximum edge count from node to a leaf. |
| Path length | Sum of edges along a path. |
| Subtree | Tree formed by a node and its descendants. |
1.2 Binary Trees and Special Forms
A binary tree is a tree in which each node has at most two children: left and right.
| Type | Definition | Key Property |
|---|---|---|
| Strictly/Full binary tree | Every internal node has exactly 2 children | (leaves = internal + 1). |
| Complete binary tree | All levels filled except possibly last; last filled left to right | Efficient array representation. |
| Perfect binary tree | All internal nodes have 2 children and all leaves at same level | Nodes . |
| Extended binary tree | Replace every null child with special external node | Used in path-length proofs. |
| Skewed binary tree | Every node has only left or only right child | Height becomes . |
1.3 Binary Tree Representation
Array representation: For node at index (1-based), left child , right child , parent . Efficient for complete binary trees; wastes space for skewed trees.
Linked representation: Each node has data, left, and right pointers. Most common for general binary trees.
struct Node { int data; struct Node *left, *right; };
1.4 External and Internal Path Length
For a binary tree with internal nodes, internal path length is sum of path lengths to all internal nodes, and external path length is sum of path lengths to all external nodes in extended binary tree.
Result:
where is the number of internal nodes.
After-Topic PYQ (2023-24 Sec B Q4a, 7 Marks): If and denotes the external and internal path length of a binary tree having internal nodes then show that . After-Topic PYQ (2024-25 Sec A Q1g, 2 Marks): Explain the concept of NonLinear data structures with the help of example.
2. Binary Search Tree and Traversals
Binary Search Tree (BST): A binary tree where for every node, all keys in the left subtree are smaller, all keys in the right subtree are larger, and both subtrees are also BSTs (assuming distinct keys).
2.1 BST Operations
| Operation | Average | Worst (skewed) | Idea |
|---|---|---|---|
| Search | Compare key, go left/right. | ||
| Insertion | Search null position, attach node. | ||
| Deletion | Leaf: remove; one child: bypass; two children: replace with inorder successor/predecessor. | ||
| Modification | search + update/reinsert | Key change may require delete+insert. |
2.2 Tree Traversal Algorithms
| Traversal | Order | Use |
|---|---|---|
| Inorder (LNR) | Left, Node, Right | Sorted output in BST. |
| Preorder (NLR) | Node, Left, Right | Copy/serialize tree structure. |
| Postorder (LRN) | Left, Right, Node | Delete/free tree, expression evaluation. |
| Level order | Level by level | BFS using queue. |
Constructing Binary Tree from Traversals: Preorder+Inorder or Postorder+Inorder uniquely identify a binary tree (with distinct keys). Preorder+Postorder alone is not unique for general binary tree.
2.3 Threaded Binary Trees
In a normal binary tree, many null pointers waste space. A threaded binary tree replaces null pointers with threads:
- Left null pointer points to inorder predecessor.
- Right null pointer points to inorder successor.
Advantages: Inorder traversal without recursion/stack, faster traversal. Types: one-way (right-threaded) and two-way (fully threaded).
After-Topic PYQ (2025-26 Sec B Q4a, 7 Marks): What do you mean by Binary Search Tree? Construct a Binary Search Tree by inserting the sequence 10, 12, 5, 4, 20, 8, 7, 15, 13. After-Topic PYQ (2023-24 Sec C Q4b, 7 Marks): Write C function for non-recursive post order traversal. After-Topic PYQ (2024-25 Sec A Q1h, 2 Marks): Illustrate the significance of Threaded Binary Tree.
3. Huffman Coding Using Binary Tree
Huffman coding: A greedy lossless compression algorithm that assigns variable-length prefix codes to characters based on frequency: more frequent characters get shorter codes, less frequent get longer codes.
3.1 Algorithm
- Create a leaf node for each character with its frequency/probability.
- Insert all nodes into a min-priority queue.
- Extract two nodes with smallest frequencies.
- Create a new internal node with frequency = sum of two; make extracted nodes children.
- Insert new node back into queue.
- Repeat until one node remains (root).
- Assign 0 to left edge, 1 to right edge (or vice versa consistently); path from root gives code.
3.2 Average Code Length
where is probability/frequency weight and is code length in bits.
Significance of binary tree in Huffman: The binary tree ensures prefix property (no code is prefix of another), enabling unambiguous decoding.
After-Topic PYQ (2023-24 Sec C Q4a, 7 Marks): Suppose character a, b, c, d,e,f has probabilities 0.07, 0.09, 0.12, 0.22, 0.23, 0.27 respectively. Find an optional Huffman code and draw the Huffman tree. What is the average code length? After-Topic PYQ (2023-24 Sec A Q1g, 2 Marks): What is the significance of binary tree in Huffman algorithm?
4. AVL Tree, B-Tree and Binary Heaps
4.1 AVL Tree
AVL Tree: A self-balancing BST where for every node the balance factor is in . After insertion/deletion, rotations restore balance.
| Imbalance | Case | Rotation |
|---|---|---|
| BF = +2, left child BF = +1 | Left-Left | Right rotation (LL) |
| BF = -2, right child BF = -1 | Right-Right | Left rotation (RR) |
| BF = +2, left child BF = -1 | Left-Right | Left rotate child, then right rotate node (LR) |
| BF = -2, right child BF = +1 | Right-Left | Right rotate child, then left rotate node (RL) |
All BST operations remain because height stays logarithmic.
4.2 B-Tree
A B-tree of order is a balanced multiway search tree used in databases/file systems.
- All leaves at same level.
- Root has at least 2 children (unless leaf).
- Internal nodes (except root) have at least children.
- Node with children has keys in sorted order.
4.3 Binary Heaps
A binary heap is a complete binary tree satisfying heap property:
- Max-heap: Parent children.
- Min-heap: Parent children.
Used in priority queues and heap sort. Insert: add at end, heapify up. Delete root: replace with last, heapify down. Both .
After-Topic PYQ (2024-25 Sec B Q4a, 7 Marks) / (2025-26 Sec C Q4b, 7 Marks): Insert the sequence 71, 41, 91, 56, 60, 30, 40, 80, 50, 55 into an AVL tree starting with an empty tree.
Solved Exam-Ready Questions
Solved Q1 (2024-25 Sec B Q4a / 2025-26 Sec C Q4b, 7 Marks)
Q: Insert the sequence 71, 41, 91, 56, 60, 30, 40, 80, 50, 55 into an AVL tree starting with an empty tree.
Solution (key rotations):
- Insert 71,41,91: balanced.
- Insert 56: under 41 right.
- Insert 60: causes imbalance at 41 (LR case: left child 56 has right child 60). LR rotation at 41 → 56 becomes child of 71, 41 left, 60 right.
- Insert 30,40: 40 causes imbalance at 41 (RL? check): 41 has left 30, right 40? actually 41.left=30, insert 40 as right of 30 causes LR at 41. LR rotation at 41.
- Insert 80: balanced on right side.
- Insert 50: causes imbalance at 56 (LR case). LR rotation at 56 gives 50 as subtree root.
- Insert 55: may trigger rebalancing up to root depending on current tree; apply appropriate LL/RR/LR/RL.
Final AVL (after all insertions): root 56; left subtree 41 with 30,50(40,55); right subtree 71 with 60 and 91(80). (Verify BF of every node in { -1,0,1 } while drawing in exam.)
Result: AVL property maintained by performing required LR/RL/LL/RR rotations during insertion.
Solved Q2 (2025-26 Sec B Q4a, 7 Marks)
Q: What do you mean by Binary Search Tree? Construct a Binary Search Tree by inserting the sequence 10, 12, 5, 4, 20, 8, 7, 15, 13.
Answer:
A BST is a binary tree in which left subtree keys are less than node key and right subtree keys are greater.
Construction:
- 10 root.
- 12 > 10 → right of 10.
- 5 < 10 → left of 10.
- 4 < 10, < 5 → left of 5.
- 20 > 10, > 12 → right of 12.
- 8 < 10, > 5 → right of 5.
- 7 < 10, > 5, < 8 → left of 8.
- 15 > 10, > 12, < 20 → left of 20.
- 13 > 10, > 12, < 20, < 15 → left of 15.
Result: Inorder traversal gives 4, 5, 7, 8, 10, 12, 13, 15, 20 (sorted), confirming BST property.
Solved Q3 (2023-24 Sec C Q4a, 7 Marks)
Q: Suppose character a, b, c, d,e,f has probabilities 0.07, 0.09, 0.12, 0.22, 0.23, 0.27 respectively. Find an optional Huffman code and draw the Huffman tree. What is the average code length?
Solution:
Order by probability: a=0.07, b=0.09, c=0.12, d=0.22, e=0.23, f=0.27.
- Combine a+b = 0.16.
- Combine c+0.16 = 0.28.
- Combine d+e = 0.45.
- Combine f+0.28 = 0.55.
- Combine 0.45+0.55 = 1.00.
One possible code (left 0, right 1 by tree shape): f=00, c=010, a=0110, b=0111, d=10, e=11.
| Char | Prob | Code | Length |
|---|---|---|---|
| a | 0.07 | 0110 | 4 |
| b | 0.09 | 0111 | 4 |
| c | 0.12 | 010 | 3 |
| d | 0.22 | 10 | 2 |
| e | 0.23 | 11 | 2 |
| f | 0.27 | 00 | 2 |
Result: Average code length = 2.44 bits/symbol.
Practice Questions (Self-Test)
- (2 Marks) Define complete binary tree and strictly binary tree.
- (2 Marks) What is the significance of binary tree in Huffman algorithm?
- (7 Marks) Construct a BST using 50, 30, 70, 20, 40, 80, 35, 45. Perform inorder, preorder, and postorder traversals.
- (7 Marks) Explain threaded binary tree. How does it improve inorder traversal compared to normal binary tree?
- (7 Marks) Insert 10, 20, 30, 40, 50, 25 into an empty AVL tree. Show balance factor and rotations at each step.
- (7 Marks) Explain B-tree of order m. Construct a B-tree of order 4 for keys: 10, 20, 40, 50, 60, 70, 80, 30, 35.