Search Trees

Binary Search Trees · AVL Trees · B-Trees · B+ Trees

Course Information

Course

Data Structures
and Algorithms

Chapter

5.4 Search Trees

Estimated lecture

2.5 – 3 Hours

Prerequisites

Binary Trees Recursion Searching

Learning Outcomes

By the end of this lecture, you will be able to:

  • Explain search tree properties
  • Construct and balance BSTs
  • Apply BST Insertion/Deletion
  • Track AVL Rotations
  • Design B-Trees & B+ Trees
  • Choose the optimal tree structure

Lecture Agenda

  • Part 1: Introduction to Search Trees
  • Part 2: Binary Search Tree (BST) & Operations
  • Part 3: Self-Balancing Trees (AVL Tree)
  • Part 4: Disk-Optimized Multi-Way Trees (B-Tree)
  • Part 5: Indexing Trees (B+ Tree)
  • Part 6: Practice, MCQ Quiz & Summary
Note to Students

We will build from simple to complex structures. Keep your calculators ready to compute Balance Factors during AVL rotations!

Why Searching Matters

Searching is the single most frequent action executed on computers.

Data Structure Average Search Time Sorted?
Array O(n) No
Linked List O(n) No
Binary Tree (unordered) O(n) No
Search Tree O(log n) Yes

Evolution of Search Structures

  • Static Arrays: Allow binary search ($O(\log n)$) but insertion/deletion requires shifts ($O(n)$).
  • Linked Lists: Fast insertion ($O(1)$) but searching is linear ($O(n)$).
  • Search Trees: Combine structural flexibility of lists with the fast search of binary search ($O(\log n)$ for search, insert, and delete).
Array [Sorted] Linked List Search Tree

Real-Life Motivation

Where are Search Trees used in production systems?

Databases

B-Tree indexes in MySQL & PostgreSQL.

File Systems

ext4 and NTFS directory maps.

Auto-complete

Fast prefix search maps.

Routing IP

Prefix tables for packet forwarding.

Search Trees

"An organized tree architecture ensuring ordered lookup paths."

Search Tree Definition

A Search Tree is a node-based binary or multi-way tree structure where:

  • Nodes store comparable keys.
  • Keys are kept in sorted relative positions.
  • Every search path splits choices at each node.
Binary vs Multi-way

A binary search tree has at most 2 children per node. An m-way tree can have up to $m$ children.

Key < Key > Key

Pros & Cons of Search Trees

Advantages
  • Logarithmic time complexity ($O(\log n)$).
  • Keys are naturally ordered (easy range scans).
  • Dynamic allocation (no fixed buffer size).
Disadvantages
  • Skewed tree degrades search to linear $O(n)$.
  • Self-balancing (AVL/B-Trees) requires rotation overhead.
  • Pointer manipulation uses extra memory overhead.

Search Structures Compared

Operation Sorted Array Linked List Hash Table BST (Average)
Search O(log n) O(n) O(1) O(log n)
Insert O(n) O(1) O(1) O(log n)
Delete O(n) O(n) O(1) O(log n)
Range Queries O(log n + k) O(n) O(n) O(log n + k)

5.4.1 Binary Search Tree (BST)

"Formal definition, step-by-step constructions, and implementation details."

The BST Property

For every node $X$ in the tree:

  • All keys in the left subtree must be strictly smaller than $X$'s key:
    $\text{Key}(Y) < \text{Key}(X) \quad \forall Y \in \text{LeftSubtree}(X)$
  • All keys in the right subtree must be strictly greater than $X$'s key:
    $\text{Key}(Z) > \text{Key}(X) \quad \forall Z \in \text{RightSubtree}(X)$
50 30 70

BST Construction Step-by-Step

We will construct a BST from scratch using this sequence of inputs:

[50, 30, 70, 20, 40, 60, 80]

Each insertion requires starting at the root and recursively choosing left or right edges.

Rule of Insertion

Start from the root node. If input key is smaller than node key, go left. If greater, go right. Repeat until a NULL child pointer is found.

Insert 50

First value inserted into an empty tree becomes the root node.

Node 50 is created. Left and right subtrees are NULL.

50

Insert 30

  • Compare 30 with root 50.
  • Since $30 < 50$, branch left.
  • Left pointer is NULL; insert node 30 here.
50 30

Insert 70

  • Compare 70 with root 50.
  • Since $70 > 50$, branch right.
  • Right pointer is NULL; insert node 70 here.
50 30 70

Insert 20

  • Compare 20 with 50 $\rightarrow$ smaller (go left).
  • Compare 20 with 30 $\rightarrow$ smaller (go left).
  • Left of 30 is NULL; insert here.
50 30 70 20

Insert 40

  • Compare 40 with 50 $\rightarrow$ go left.
  • Compare 40 with 30 $\rightarrow$ go right ($40 > 30$).
  • Right of 30 is NULL; insert here.
50 30 70 20 40

Insert 60

  • Compare 60 with 50 $\rightarrow$ go right.
  • Compare 60 with 70 $\rightarrow$ go left ($60 < 70$).
  • Left of 70 is NULL; insert here.
50 30 70 20 40 60

Insert 80

  • Compare 80 with 50 $\rightarrow$ go right.
  • Compare 80 with 70 $\rightarrow$ go right ($80 > 70$).
  • Right of 70 is NULL; insert here.

BST Construction is now complete! The resulting tree is fully balanced.

50 30 70 20 40 60 80

BST Search Algorithm

Node* search(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);
}
Time Complexity: $O(h)$, where $h$ is tree height.
  • Base Cases:
    • Root is NULL $\rightarrow$ Unsuccessful search.
    • Root's key matches search key $\rightarrow$ Successful search.
  • Recursive Steps:
    • Key is smaller $\rightarrow$ search left subtree.
    • Key is larger $\rightarrow$ search right subtree.

Search 60 (Successful)

  • Compare 60 with root 50. Since $60 > 50$, search right.
  • Compare 60 with 70. Since $60 < 70$, search left.
  • Compare 60 with 60. Matches! Node found.
50 30 70 20 40 80 60

Search 65 (Unsuccessful)

  • Compare 65 with 50 $\rightarrow$ go right.
  • Compare 65 with 70 $\rightarrow$ go left.
  • Compare 65 with 60 $\rightarrow$ go right.
  • Right child of 60 is NULL. Search failed.
50 30 70 20 40 80 60

BST Insertion Algorithm

Node* insert(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;
}

Like search, we traverse recursively:

  • If the pointer is NULL, insert a new node here.
  • Reassign the child pointer as we backtrack.
  • Ignores duplicates.

BST Deletion

Deletion is more complex than insertion because we must maintain the BST property after removal.

Case 1

Node to delete is a Leaf node.

Case 2

Node has One Child.

Case 3

Node has Two Children.

Case 1: Delete Leaf (Delete 20)

Node 20 is a leaf. We can remove it directly without moving any other nodes.

Result: Set left child pointer of parent (30) to NULL and free memory of node 20.

50 30 70 40 20

Case 2: Delete One Child (Delete 30)

Assume we deleted 20, now we delete 30. Node 30 has only one child (40).

Bypassing node 30: Connect the parent of 30 (which is 50) directly to 30's child (40).

50 70 40 30

Case 3: Delete Root (Delete 50)

Node 50 has two children. We cannot delete it directly.

Instead, replace its value with its Inorder Successor (smallest key in the right subtree) or Inorder Predecessor (largest key in the left subtree).

Successor of 50 is 60. We copy 60 to the root, and then recursively delete 60 from the right subtree.

60 30 70 80 60

BST Deletion: C Code

          Node* deleteNode(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 {
                if (root->left == NULL) {
                    Node* temp = root->right; free(root); return temp;
                } else if (root->right == NULL) {
                    Node* temp = root->left; free(root); return temp;
                }
                Node* temp = minValueNode(root->right); // Successor
                root->key = temp->key;
                root->right = deleteNode(root->right, temp->key);
            }
            return root;
        }

BST Time & Space Complexities

Operation Best Case Average Case Worst Case
Search O(1) O(log n) O(n)
Insertion O(log n) O(log n) O(n)
Deletion O(log n) O(log n) O(n)
Space O(n) O(n) O(n)

Binary Search Tree Applications

  • Multilevel Indexing: Used in databases for quick access to records.
  • Dynamic Sorting: Useful for maintaining a constantly changing sorted dataset efficiently.
  • Virtual Memory Management: Used in Unix operating systems for managing virtual memory areas.
Real-World Impact

BSTs provide the foundational logic for more complex structures like AVL and B-Trees, which power these modern systems.

Interactive BST Playground

BST initialized. Type a value and click Insert/Search/Delete to watch the tree morph!

5.4.2 Balanced Search Tree (AVL)

"Self-balancing Binary Search Trees, balance calculations, and LL/RR/LR/RL rotations."

Motivation: Skewed Trees

If we insert keys in sorted order: [10, 20, 30, 40], a standard BST becomes skewed.

  • Height degrades from $\log n$ to $n$.
  • Operations slow down to $O(n)$ linear time.
  • We lose the logarithmic searching speed!
The Degradation

A skewed BST is structurally identical to a singly Linked List but consumes double the memory for pointers.

10 20 30 40

The Balance Factor (BF)

An AVL Tree maintains balance by computing the Balance Factor (BF) for every node.

\[ \text{BF} = \text{Height(Left Subtree)} - \text{Height(Right Subtree)} \]
The AVL Rule

For every node, the Balance Factor must be:

−1, 0, or +1

If \( \text{BF} > 1 \) or \( \text{BF} < -1 \), the node becomes imbalanced and requires one or more tree rotations to restore balance.

42 BF = 0 25 70

Height(Left) = 1
Height(Right) = 1
BF = 1 − 1 = 0

Calculating Heights & BFs

Let's trace how heights propagate upwards:

  • NULL children have Height = 0.
  • Leaves have Height = 1.
  • A node's Height = $1 + \max(h_L, h_R)$.
30 BF = +2 (Imbalance!) h=3 20 BF = +1 h=2 10 BF = 0 h=1

The Four AVL Rotations

Depending on the direction of imbalance, we apply one of four rotations:

LL Rotation

Single Right Rotation. Occurs when left child of left child overflows.

RR Rotation

Single Left Rotation. Occurs when right child of right child overflows.

LR Rotation

Double Rotation (Left-Right). Left child's right child overflows.

RL Rotation

Double Rotation (Right-Left). Right child's left child overflows.

LL Rotation (Single Right Rotation)

When is LL Rotation Needed?

  • Balance Factor of node = +2
  • Insertion occurs in the Left subtree of Left child
  • Tree becomes left-heavy.

Algorithm


RightRotate(z)

1. y = z->left
2. T3 = y->right

3. y->right = z
4. z->left = T3

5. Update Height(z)
6. Update Height(y)

7. return y
Before Rotation 30 20 10 Rotate node 30 to the Right After Rotation 20 10 30

Result

  • BST property is preserved.
  • Tree becomes balanced again.
  • Height decreases.
  • Time Complexity: O(1)

RR Rotation (Single Left Rotation)

When is RR Rotation Used?

  • Root node has BF = -2
  • Insertion occurs in the Right subtree of the Right child.
  • Tree becomes Right Heavy.

Algorithm (Left Rotation)



LeftRotate(z)

1. y = z->right;
2. T2 = y->left;

3. y->left = z;
4. z->right = T2;

5. UpdateHeight(z);
6. UpdateHeight(y);

7. return y;

Time Complexity: O(1)
Before Rotation 10 20 30 Rotate Left After Rotation 20 10 30
Result:
  • BST property is preserved.
  • Tree becomes balanced.
  • Balance Factor becomes 0.

LR Rotation (Double Rotation)

When is LR Rotation Used?

  • Root node has BF = +2
  • Left child has BF = -1
  • Insertion occurs in the Right subtree of the Left child.
  • The tree forms a Zig-Zag pattern.

Algorithm



LRRotate(z)

1. z->left = LeftRotate(z->left);

2. return RightRotate(z);

Double Rotation Required
  1. Left Rotation on Left Child
  2. Right Rotation on Root
Before 30 10 20 Step 1 Left Rotate (10) 30 20 10 Step 2 Right Rotate (30) 20 10 30
Result
  • BST property is preserved.
  • AVL balance is restored.
  • Final Balance Factor = 0.
  • Time Complexity = O(1)

RL Rotation (Double Rotation)

When is RL Rotation Used?

  • Root node has BF = -2
  • Right child has BF = +1
  • Insertion occurs in the Left subtree of the Right child.
  • The tree forms a Zig-Zag pattern.

Algorithm



RLRotate(z)

1. z->right = RightRotate(z->right);

2. return LeftRotate(z);

Double Rotation Required
  1. Right Rotation on Right Child
  2. Left Rotation on Root
Before 10 30 20 Step 1 Right Rotate (30) 10 20 30 Step 2 Left Rotate (10) 20 10 30
Result
  • BST property is preserved.
  • AVL balance is restored.
  • Final Balance Factor = 0.
  • Time Complexity = O(1)

Rotation C Code (Right Rotate)

Node* rightRotate(Node* y) {
    Node* x = y->left;
    Node* T2 = x->right;
    
    // Perform rotation
    x->right = y;
    y->left = T2;
    
    // Update heights
    y->height = max(height(y->left), height(y->right)) + 1;
    x->height = max(height(x->left), height(x->right)) + 1;
    
    // Return new root
    return x;
}

AVL Construction Trace

Insert: [10, 20, 30] into AVL.

  • Insert 10, 20 $\rightarrow$ fine.
  • Insert 30 $\rightarrow$ BF at 10 becomes -2.
  • RR Rotation is triggered at node 10.

Subtree is balanced automatically. Height remains 2 instead of 3.

20 10 30

AVL Complexity

Because the tree guarantees balance, heights are bound by $\log n$.

Operation Average Case Worst Case
Search O(log n) O(log n)
Insertion O(log n) O(log n)
Deletion O(log n) O(log n)

Interactive AVL Playground

AVL Tree initialized. Insert a value that triggers a rotation (e.g. 90 on a right-skewed node).

5.4.3 M-Way Search Trees

"Extending search trees to hold multiple keys per node — optimizing database and disk page storage limits."

Disk Storage Limitation

Why do binary search trees fail for large database tables containing millions of rows?

  • Disk access is slow (mechanically reading blocks).
  • A binary tree of height 20 requires up to 20 disk operations.
  • We need a wider tree with a larger branching factor so that we can fetch more keys per disk load!
Disk Block Fetching

Hardware reads data in blocks (pages), typically 4KB or 8KB. Multi-way trees pack multiple keys inside a single block to minimize block loads.

M-Way Search Tree Layout

An M-Way Search Tree node contains:

  • Up to $m-1$ keys sorted: $K_1 < K_2 < \dots < K_{m-1}$
  • Up to $m$ child pointers: $P_1, P_2, \dots, P_m$

Subtree $P_i$ contains keys between $K_{i-1}$ and $K_i$.

Key 1 (20) Key 2 (50) Key 3 (80) < 20 20-50 > 80

Searching an M-Way Tree

To find a key $X$ in a node with $k$ keys ($K_1 \dots K_k$):

  • Do a linear or binary search through the keys in the node.
  • If $X == K_i$, search is successful.
  • If $X < K_1$, follow pointer $P_1$.
  • If $K_i < X < K_{i+1}$, follow pointer $P_{i+1}$.
  • If $X > K_k$, follow pointer $P_{k+1}$.
Example: Find 40

In node [20 | 50 | 80]:

1. 40 > 20
2. 40 < 50
Result: Follow pointer $P_2$ (middle child).

Limitations of M-Way Trees

The Problem: No Balance Guarantee

Just like standard BSTs, basic M-way trees don't balance themselves. They can become skewed, leading to $O(n)$ search times.

10 | 20 30 | 40 50 | 60

B-Tree

"Self-balancing, multi-way search tree. Definition, properties, split promotions, and complexities."

B-Tree Properties

A B-Tree of order $m$ satisfies:

  • Every node has at most $m$ children (and $m-1$ keys).
  • Every internal node (except root) has at least $\lceil m/2 \rceil$ children (and $\lceil m/2 \rceil - 1$ keys).
  • The root has at least 2 children (unless it is a leaf).
  • All leaves are at the exact same depth.
  • Keys within a node are sorted.
Perfect Balance

Leaves being at the same depth guarantees that the tree height remains strictly balanced $O(\log n)$. It grows and shrinks from the root, not the leaves.

Order 4 Example ($m=4$)
  • Max keys: 3
  • Min keys (internal): 1

B-Tree Insertion: Simple

Insert: [10, 20, 30] in a B-Tree of Order 4.

  • An Order 4 B-Tree can hold a maximum of 3 keys per node.
  • We start with an empty root node.
  • We insert 10, then 20, then 30 into the root.
  • Since the node has not exceeded 3 keys, no split occurs.
10 20 30

B-Tree Insertion: Node Split

Insert: [40]

  • Inserting 40 creates an overflow (4 keys: 10, 20, 30, 40).
  • Action: Split the node at the median.
  • The median key (20) is promoted to the parent.
  • The node splits into two leaves.
20 10 30 | 40

B-Tree Insertion: Root Split

Insert: [50, 60, 70]

  • 50 and 60 go into the right child. It overflows and splits, promoting 40 to the root.
  • Root becomes [20 | 40]. Right child becomes [50 | 60].
  • Insert 70 into [50 | 60]. It overflows (50, 60, 70), promoting 60 to root.
  • Root is now [20 | 40 | 60]. Node is full!
  • Next insert (e.g. 80) will cause root overflow, promoting 40 to a NEW root, increasing tree height.
20 40 60 10 30 50 70

B-Tree Deletion: Borrowing

Deletion requires maintaining minimum keys (1 for Order 4 internal, 1 for leaves here).

  • Scenario: Delete 30.
  • Node [30] becomes empty (underflow).
  • Action (Borrow): Look at left sibling [10]. (Only 1 key, can't borrow).
  • Look at right sibling [50]. (Only 1 key, can't borrow).
  • Wait, this requires merging. Let's assume right sibling had [50 | 55]. We would borrow 50!
  • Pull parent key (40) down, push sibling key (50) up to parent.
Borrowing Rule (Transfer)

If a node underflows, borrow a key from a rich sibling through the parent.

Sibling Key → Parent → Underflowed Node.

B-Tree Deletion: Merging

What if siblings are also at minimum capacity?

  • Scenario: Delete 30 (from previous tree). Siblings [10] and [50] have only 1 key.
  • Action (Merge): Merge the empty node, the sibling [10], and the parent key 20 into a single node [10 | 20].
  • Parent loses key 20. If parent underflows, apply rules recursively upwards.
40 60 10 | 20 50 70

Parent 20 merged down with 10.

B-Tree Deletion: Internal Node

Deleting a key that is not in a leaf.

  • Just like BST, we cannot simply delete an internal key because it acts as a separator for subtrees.
  • Action: Replace the key with its inorder predecessor (largest key in left subtree) or inorder successor (smallest in right subtree).
  • Then, delete the predecessor/successor from the leaf!
  • If the leaf underflows, use borrow or merge.
Example

To delete root 40, find predecessor (e.g., 30 in leaf). Replace 40 with 30. Now delete 30 from the leaf.

B-Tree Complexity

Operation Average Case Worst Case Disk Reads
Search O(log n) O(log n) O(log_m n)
Insertion O(log n) O(log n) O(log_m n)
Deletion O(log n) O(log n) O(log_m n)

Interactive B-Tree Playground

B-Tree (Order 4) initialized. Insert keys to see nodes split and overflow!

B+ Tree

"Optimizing B-Trees for database indexing. Leaf node chains, range queries, and structural layout."

B+ Tree Structure

How does a B+ Tree differ from a B-Tree?

  • Internal nodes only store index keys and child pointers (no actual data).
  • Leaf nodes store all data records.
  • Leaves are linked together via a singly or doubly linked list.
Database Scan Benefit

We can perform full range queries ($O(\log n)$ to find start, then linear scan along leaves) without visiting parent nodes again.

30 10 | 20 30 | 40 next

B+ Tree Insertion: Leaf Split

Insert: [10, 20, 30, 40] in B+ Tree (Order 4)

  • 10, 20, 30 fit in the leaf root. Insert 40 causes overflow.
  • Leaf Split Rule: Split at median (20 or 30). Let's pick 30.
  • COPY UP: The median 30 is copied to the parent index node. It ALSO stays in the right leaf!
  • Left leaf: [10, 20]. Right leaf: [30, 40]. Root (Index): [30].
30 Index (Copy) 10 | 20 30 | 40

B+ Tree Insertion: Internal Split

What happens when an internal (index) node overflows?

  • Just like a standard B-Tree!
  • PUSH UP: The median key is moved to the parent and is removed from the current index node.
Rule of Thumb

Leaves → Split & Copy up.
Internals → Split & Push up.

Internal overflow during large bulk inserts:

50 30 70 | 90

Data leaves not shown.

B+ Tree Deletion

Deleting a key always happens at the leaf level.

  • Scenario 1: Key is only in leaf. Delete it directly. Borrow/Merge if underflow.
  • Scenario 2: Key is also an index (routing) key in an internal node.
  • We can simply delete the key from the leaf and leave the index key alone! It's still a valid routing value.
  • However, if we merge leaves, we might need to delete the index key to fix the parent underflow.
Stale Index Keys

In B+ Trees, an index key can exist even if its corresponding data record was deleted, because it still correctly routes $X < K$ and $X \ge K$.

Range Query Performance

Query: Find all keys in range 15 to 35.

  • Step 1: Use index to search for key 15 ($O(\log n)$). Reaches first leaf containing 20.
  • Step 2: Follow leaf link pointers to get keys 20, 30, and 40. Stops when key exceeds 35 ($O(k)$).
Efficiency

Requires zero backtracking to parent nodes. Standard B-Trees would require multiple upward/downward traversals to fetch ordered ranges.

Comparison of Search Trees

Tree Type Max Child Height Balance Search Time Ideal Application
BST 2 None (can skew) $O(n)$ Worst Case General in-memory maps
AVL 2 Strict height $\le 1$ $O(\log n)$ Guaranteed Lookup-heavy in-memory maps
B-Tree $m$ Perfect leaf level $O(\log_m n)$ Storage/Database systems
B+ Tree $m$ Perfect leaf level $O(\log_m n)$ DB indexing (MySQL, SQLite)

Industrial Applications

File Systems

NTFS uses B-Trees. ext4 uses HTrees (similar structure).

DB Engines

MySQL InnoDB, SQLite, and PostgreSQL use B+ Trees.

OS Memory

Linux kernel uses red-black trees (similar to AVL) for VM space.

Common Student Mistakes

The Confusions
  • Calculating Balance Factor as LeftHeight - RightHeight, then switching it to RightHeight - LeftHeight halfway through an exam.
  • Choosing the wrong rotation direction during LR/RL double rotations.
  • Forgetting to split parent nodes when a B-Tree overflow propagates upwards.
Pro Tips

Double-check balance factor signs. Draw out parent-child boundaries during rotations to keep tracks clear.

Practice Sheet

Work on these problems during this workshop segment:

  • Exercise 1: Construct a BST using keys: [15, 6, 18, 3, 7, 16, 20, 10].
  • Exercise 2: Delete node 6 from your BST. Explain the case.
  • Exercise 3: Insert 40, 50, 60 in AVL. Balance it.
Answer Checks

We will solve these live on the board in 10 minutes. Discuss your solutions with your partner!

Quiz Question 1

What is the maximum number of children in a Binary Search Tree (BST) node?
A: 2
B: 4
C: Unlimited
D: 1
Correct Answer: A. By definition, a binary tree node has at most 2 children (left and right).

Quiz Question 2

In a skewed BST containing $n$ nodes, what is the worst-case search complexity?
A: O(log n)
B: O(n)
C: O(1)
D: O(n log n)
Correct Answer: B. A skewed BST degrades to a linked list structure, requiring a linear scan.

Quiz Question 3

What is the balance factor (BF) value range for an AVL Tree node?
A: Height difference <= 2
B: {0, 1}
C: {-1, 0, 1}
D: Any integer value
Correct Answer: C. AVL properties require the balance factor to remain within -1, 0, or 1.

Quiz Question 4

Which rotation is required for a zig-zag imbalance on the left child's right child?
A: LR Rotation
B: LL Rotation
C: RR Rotation
D: RL Rotation
Correct Answer: A. A zig-zag left-right path requires an LR rotation.

Quiz Question 5

Where are keys and data stored in a B+ Tree?
A: Internal nodes only
B: Root node only
C: Every node holds duplicate data
D: Actual data records only in leaf nodes
Correct Answer: D. B+ Trees separate index keys (internal nodes) from data records (leaf nodes).

Quiz Question 6

Which tree traversal yields sorted keys in a Binary Search Tree?
A: Preorder
B: Inorder
C: Postorder
D: Level-order
Correct Answer: B. Inorder traversal (Left, Root, Right) visited nodes in sorted order.

Quiz Question 7

What is the maximum number of keys in a B-Tree node of order 4?
A: 4
B: 2
C: 3
D: 5
Correct Answer: C. Max keys = order - 1 = 4 - 1 = 3.

Quiz Question 8

What are leaf nodes in a B+ Tree linked as?
A: Linked list
B: Binary tree
C: Circular queue
D: Direct disk sectors
Correct Answer: A. Leaf nodes are linked together as a list to speed up sequential range reads.

Quiz Question 9

If we search for key 50 in a BST, and the current node is 40, which direction do we go?
A: Left
B: Up to parent
C: Right
D: Stop search
Correct Answer: C. Since $50 > 40$, we search in the right subtree of node 40.

Quiz Question 10

What is the height of an empty AVL tree?
A: 1
B: 0
C: -1
D: Undefined
Correct Answer: B. An empty tree has height 0. (Some textbooks use -1, but in our code definition, it evaluates to 0).

Quiz Question 11

Which structure is most commonly used in database storage systems?
A: Array
B: AVL Tree
C: BST
D: B+ Tree
Correct Answer: D. B+ Trees are standard because of page optimization and fast range scans.

Quiz Question 12

What is the best-case search complexity in an AVL tree?
A: O(1)
B: O(log n)
C: O(n)
D: O(1) in average
Correct Answer: A. The search target is found immediately at the root node in 1 check.

Quiz Question 13

What is the balance factor of node 40 if left height is 3 and right height is 1?
A: -2
B: 1
C: +2
D: 0
Correct Answer: C. BF = Left Height - Right Height = 3 - 1 = +2.

Quiz Question 14

Which file system uses B-Trees?
A: FAT32
B: NTFS
C: ext2
D: FAT16
Correct Answer: B. NTFS (Windows standard file system) uses B-Trees to index directories.

Quiz Question 15

What occurs when the key count of a B-Tree node exceeds the order limit?
A: Rotation
B: Deletion
C: Node split & promotion
D: Tree collapse
Correct Answer: C. Node overflows trigger splits, promoting the median key to parent levels.

Lecture Summary

  • BSTs offer simple structures but risk degrading to $O(n)$ if unbalanced.
  • AVL Trees fix this by strictly rebalancing (using LL/RR/LR/RL rotations) to maintain $O(\log n)$.
  • B-Trees expand child capacity, optimizing layout for disk structures.
  • B+ Trees link leaves together to enable fast sequential database range queries.
The Unified Takeaway

Choose AVL for fast memory lookups, B+ Trees for large disk storage indexes, and BST for simple prototype implementations.

Thank You!

Any Questions?

contact@university.edu
cs.university.edu/dsa