Data Structures using C
Comprehensive exam revision notes for BCA-102N Unit 5 (AVL Trees, M-way Search Trees & B-Trees). Includes Section A (2M), Section B (5M), and Section C (15M) questions, answers, and diagrams.
Data Structures using C
AVL Trees, M-way Search Trees & B-Trees
Core Definitions & Key Concepts
State the defining property of a Binary Search Tree (BST)
For every node X in a BST: all keys in its left subtree are strictly less than X.key, and all keys in its right subtree are strictly greater than X.key (Left < Root < Right).
What traversal of a Binary Search Tree produces keys in strictly ascending sorted order
Inorder Traversal (Left-Root-Right) of a BST always yields keys in strictly ascending numerical order.
An AVL Tree and write the formula for Balance Factor (BF)
An AVL Tree is a self-balancing binary search tree where the heights of the two child subtrees of any node differ by at most 1.
Balance Factor (BF) = Height(Left_Subtree) - Height(Right_Subtree) ∈ {-1, 0, +1}.
Name the four basic rebalancing rotations used in AVL Trees
- LL (Left-Left) Rotation: Single Right rotation.
- RR (Right-Right) Rotation: Single Left rotation.
- LR (Left-Right) Rotation: Double rotation (Left rotate left child, then Right rotate node).
- RL (Right-Left) Rotation: Double rotation (Right rotate right child, then Left rotate node).
What is the worst-case search complexity in an AVL Tree versus an unbalanced BST
- Unbalanced BST: O(n) (degrades to a skewed linked list when keys arrive sorted).
- AVL Tree: Strictly O(log2 n) guaranteed due to height balance
h <= 1.44 log2 n.
An M-Way Search Tree
An M-Way search tree is a multi-way tree where each node can have at most M children and contains at most M - 1 sorted keys acting as partition boundaries.
What is a B-Tree of order m
A B-Tree of order m is a self-balancing balanced m-way search tree where:
- The root has at least 2 children (unless it is a leaf).
- Every internal node has at least
ceil(m/2)children and at mostmchildren. - All leaf nodes reside at the exact same depth level.
How does a B+ Tree differ from a standard B-Tree
In a B+ Tree, all user data records/pointers are stored exclusively in leaf nodes, while internal nodes store only search keys. Furthermore, all leaf nodes are linked sequentially as a linked list, enabling rapid range queries.
Conceptual & Architectural Analysis
The four AVL Tree rotations (LL, RR, LR, RL) with clear node transformation diagrams
1. LL Rotation (Single Right Rotation)
Triggered when an insertion occurs into the left subtree of the left child (node BF becomes +2 and left child BF is +1).
1 Z (+2) Y (0)2 / / \3 Y (+1) =====> X Z4 /5 X2. RR Rotation (Single Left Rotation)
Triggered when insertion occurs into the right subtree of right child (node BF becomes -2, right child BF is -1).
1 Z (-2) Y (0)2 \ / \3 Y (-1) =====> Z X4 \5 X3. LR Rotation (Left-Rotate Y, then Right-Rotate Z)
Triggered when insertion occurs into right subtree of left child.
1 Z (+2) Z X2 / / / \3 Y (-1) =====> X =====> Y Z4 \5 X Y4. RL Rotation (Right-Rotate Y, then Left-Rotate Z)
Triggered when insertion occurs into left subtree of right child.
Construct an AVL Tree by inserting the following sequence of keys step-by-step: 10, 20, 30, 40, 50, 25
- Insert 10, 20, 30:
- Insert 10 (BF 0).
- Insert 20: 10 has BF -1.
- Insert 30: 10 has BF -2, right child 20 has BF -1 -> RR Imbalance at 10. Apply Single Left Rotation on 10:
1 202 / \3 10 30- Insert 40: 30 has BF -1, 20 has BF -1. Tree is balanced.
- Insert 50: 30 has BF -2, 40 has BF -1 -> RR Imbalance at 30. Apply Single Left Rotation on 30:
1 202 / \3 10 404 / \5 30 50- Insert 25: Subtree at 20 has Left Height 1, Right Height 3 => BF = 1 - 3 = -2. Right child 40 has left height 2, right height 1 => BF = +1. RL Imbalance at 20.
- Rotate right on 40 (child):
- Rotate left on 20 (root):
Final Balanced AVL Tree:
1 302 / \3 20 404 / \ \5 10 25 50In-Depth Analysis & Implementation
Discuss B-Trees in detail. State all structural properties of a B-Tree of order m and construct a B-Tree of order 3 (2-3 Tree) by inserting: 10, 20, 30, 40, 50, 60, 70, 80
1. Structural Properties of B-Tree of Order m
- Root: Has at least 2 children (if not a leaf).
- Internal Nodes: Have at least \lceil m/2 \rceil children and at most m children.
- Keys per Node: Minimum keys = \lceil m/2 \rceil - 1, Maximum keys = m - 1.
- Sorted Keys: Keys within any node are stored in ascending order: k_1 < k_2 < \dots < k_n.
- Leaf Level: All leaf nodes are at the exact same depth level (perfect balance).
2. Construction of B-Tree of Order 3 (Max Keys = 2, Min Keys = 1)
- Insert 10, 20: Node
[10, 20]. - Insert 30: Node overflows
[10, 20, 30]. Middle key20splits and pushes up to new root:
1 [ 20 ]2 / \3 [ 10 ] [ 30 ]- Insert 40: Node
[30]becomes[30, 40]. - Insert 50: Node
[30, 40, 50]overflows. Middle key40pushes up to root[20, 40]:
[ 20, 40 ]
/ | \
[ 10 ] [ 30 ] [ 50 ]- Insert 60: Node
[50]becomes[50, 60]. - Insert 70: Node
[50, 60, 70]overflows. Middle key60pushes up. Root[20, 40, 60]overflows! Middle key40pushes up to form new root level:
1 [ 40 ]2 / \3 [ 20 ] [ 60 ]4 / \ / \5 [10] [30] [50] [70]- Insert 80: Node
[70]becomes[70, 80].
All leaves are at depth 2.
Write comprehensive algorithms for Insertion, Searching, and Deletion in a Binary Search Tree (BST) with C code implementation
#include <stdio.h>
#include <stdlib.h>
struct Node {
int key;
struct Node *left, *right;
};
struct Node* newNode(int item) {
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
struct Node* insert(struct Node* node, int key) {
if (node == NULL) return newNode(key);
if (key < node->key) node->left = insert(node->left, key);
else if (key > node->key) node->right = insert(node->right, key);
return node;
}
struct Node* search(struct Node* root, int key) {
if (root == NULL || root->key == key) return root;
if (key < root->key) return search(root->left, key);
return search(root->right, key);
}
struct Node* minValueNode(struct Node* node) {
struct Node* current = node;
while (current && current->left != NULL) current = current->left;
return current;
}
struct Node* deleteNode(struct Node* root, int key) {
if (root == NULL) return root;
if (key < root->key) root->left = deleteNode(root->left, key);
else if (key > root->key) root->right = deleteNode(root->right, key);
else {
// Case 1 & 2: 0 or 1 child
if (root->left == NULL) {
struct Node* temp = root->right; free(root); return temp;
} else if (root->right == NULL) {
struct Node* temp = root->left; free(root); return temp;
}
// Case 3: 2 children. Find Inorder Successor
struct Node* temp = minValueNode(root->right);
root->key = temp->key;
root->right = deleteNode(root->right, temp->key);
}
return root;
}Important Exam Points & Quick Revision
In BCA-102N Unit 5, drawing step-by-step AVL tree rotations (LL, RR, LR, RL) or B-Tree key insertion with node splits is virtually guaranteed in the university question paper for 10–15 marks. Draw each state clearly indicating the overflow key and the split point.
100% left on this page — finishing it closes the gap for AVL Trees, M-way Search Trees & B-Trees.
AVL Trees, M-way Search Trees & B-Trees still counts as a weak topic until you finish this note and answer its questions.
Practise this unitTest Your Understanding
Now that you've read through these notes, practice unit MCQs to identify weak points and solidify your learning before exams.