Top Banner Ad

BCS301 — Unit 4: Trees

Table of Contents (22 sections)

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:

  1. AVL insertions: Insert like BST, update balance factor BF=hL−hRBF=h_L-h_R, and apply LL/RR/LR/RL rotations. Show tree after every insertion causing imbalance.
  2. BST operations: Construct BST from sequence, perform inorder/preorder/postorder; remember inorder of BST gives sorted order.
  3. Huffman coding: Build min-priority by frequency, repeatedly combine two smallest nodes, assign 0/1 on edges, compute average code length ∑pili\sum p_i l_i.
  4. Tree terminology: Revise root, leaf, internal node, degree, height, depth, level, path length, complete/full/strict/extended binary trees.
  5. 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 nn nodes, a tree has exactly n−1n-1 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 ne=ni+1n_e = n_i + 1 (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 2h+1−12^{h+1}-1.
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 nn.

1.3 Binary Tree Representation

Array representation: For node at index ii (1-based), left child 2i2i, right child 2i+12i+1, parent ⌊i/2⌋\lfloor i/2\rfloor. 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 nn internal nodes, internal path length II is sum of path lengths to all internal nodes, and external path length EE is sum of path lengths to all external nodes in extended binary tree.

Result:

E=I+2nE = I + 2n

where nn is the number of internal nodes.

After-Topic PYQ (2023-24 Sec B Q4a, 7 Marks): If EE and II denotes the external and internal path length of a binary tree having nn internal nodes then show that E=I+2nE=I+2n. 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 O(log⁡n)O(\log n) O(n)O(n) Compare key, go left/right.
Insertion O(log⁡n)O(\log n) O(n)O(n) Search null position, attach node.
Deletion O(log⁡n)O(\log n) O(n)O(n) Leaf: remove; one child: bypass; two children: replace with inorder successor/predecessor.
Modification search + update/reinsert O(n)O(n) 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

  1. Create a leaf node for each character with its frequency/probability.
  2. Insert all nodes into a min-priority queue.
  3. Extract two nodes with smallest frequencies.
  4. Create a new internal node with frequency = sum of two; make extracted nodes children.
  5. Insert new node back into queue.
  6. Repeat until one node remains (root).
  7. Assign 0 to left edge, 1 to right edge (or vice versa consistently); path from root gives code.

3.2 Average Code Length

Lavg=∑i=1npiliL_{avg}=\sum_{i=1}^{n} p_i l_i

where pip_i is probability/frequency weight and lil_i 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 BF=hleft−hrightBF = h_{left} - h_{right} is in {−1,0,+1}\{-1,0,+1\}. 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 O(log⁡n)O(\log n) because height stays logarithmic.

4.2 B-Tree

A B-tree of order mm 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 ⌈m/2⌉\lceil m/2 \rceil children.
  • Node with kk children has k−1k-1 keys in sorted order.

4.3 Binary Heaps

A binary heap is a complete binary tree satisfying heap property:

  • Max-heap: Parent ≥\ge children.
  • Min-heap: Parent ≤\le children.

Used in priority queues and heap sort. Insert: add at end, heapify up. Delete root: replace with last, heapify down. Both O(log⁡n)O(\log n).

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.

  1. Combine a+b = 0.16.
  2. Combine c+0.16 = 0.28.
  3. Combine d+e = 0.45.
  4. Combine f+0.28 = 0.55.
  5. 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
Lavg=0.07∗4+0.09∗4+0.12∗3+0.22∗2+0.23∗2+0.27∗2=2.44 bits/symbolL_{avg}=0.07*4+0.09*4+0.12*3+0.22*2+0.23*2+0.27*2=2.44\text{ bits/symbol}

Result: Average code length = 2.44 bits/symbol.


Practice Questions (Self-Test)

  1. (2 Marks) Define complete binary tree and strictly binary tree.
  2. (2 Marks) What is the significance of binary tree in Huffman algorithm?
  3. (7 Marks) Construct a BST using 50, 30, 70, 20, 40, 80, 35, 45. Perform inorder, preorder, and postorder traversals.
  4. (7 Marks) Explain threaded binary tree. How does it improve inorder traversal compared to normal binary tree?
  5. (7 Marks) Insert 10, 20, 30, 40, 50, 25 into an empty AVL tree. Show balance factor and rotations at each step.
  6. (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.