session: "5101-design-analysis-of-algo/2026-11-07 0000 DAA 07 Nov"
title: "DAA 07 Nov"
date: "2026-11-07 00:00"
duration: "1h 03m"
language: "en"
model: "deepseek-ai/DeepSeek-V3.2"
parts: 2
transcript_chars: 31677
generated: "2026-11-23T13:40:54+05:30"
DAA 07 Nov
Overview
This class reviewed B-tree deletion operations with detailed examples for orders 5 and 3, highlighting the cases of simple removal, borrowing, and merging. It then introduced B+ trees, contrasting their structure (data only in leaves, linked leaves, duplicate keys in internal nodes) with B-trees. The lecture transitioned to heaps, covering definitions, properties, and core operations (insertion, heapify, deletion). It concluded with an overview of the heap sort algorithm and a brief mention of forests in graph theory, setting the stage for red-black trees in the next session.
Learning outcomes
- Perform deletions on B-trees of a given order, handling underflow through borrowing or merging.
- Explain the structural and functional differences between B-trees and B+ trees.
- Construct a max-heap or min-heap from a given set of numbers.
- Execute insertion and deletion operations on a heap, applying the heapify process.
- Describe the steps of the heap sort algorithm using a max-heap.
- Analyze the time complexity of basic heap operations and heap sort.
Core concepts
B-tree Deletion Cases
Deletion from a B-tree must maintain the tree's balance and key-count properties. Cases include: deletion from a leaf with sufficient keys, deletion from a leaf causing underflow (solved by borrowing from a sibling or merging), and deletion from an internal node (requiring replacement with an inorder predecessor/successor). This matters because it ensures the tree remains efficient for search operations, and the cascading effects can reduce tree height.
B+ Tree Key Differences
A B+ tree is a variant where all actual data records reside in leaf nodes, which are linked sequentially. Internal nodes only store key values and pointers, often duplicating keys found in leaves. This structure matters because it optimizes range queries and full record scans, common in database indexing, while maintaining the same search efficiency as B-trees.
Heap Properties and Structure
A heap is a complete binary tree satisfying either the max-heap (parent >= child) or min-heap (parent <= child) property. It allows duplicates and has no ordering between siblings. This structure matters because it enables efficient priority queue operations and forms the basis for heap sort. It is typically implemented using an array, leveraging index relationships.
Heapify Operation
Heapify is the process of restoring the heap property after an insertion or deletion. For insertion, it involves swapping the new element upward with its parent until the property holds. For deletion (of the root), it involves moving the last element to the root and swapping it downward with the larger (in max-heap) child. This operation is fundamental to maintaining heap integrity and has O(log n) complexity.
Heap Sort Algorithm
Heap sort involves building a heap from an unsorted array and then repeatedly deleting the root (the max or min element) to produce a sorted list. For a max-heap, this yields a descending-order list. The algorithm leverages the heap's structure for in-place sorting (with some care) and has a time complexity of O(n log n).
Formulas and maths
- B-tree minimum children (internal nodes, except root): $\lceil m/2 \rceil$
Symbols: $m$ is the order of the B-tree (max children).
Used when: Determining if a node is underflowing during deletion.
- B-tree maximum keys in a node: $m - 1$
Symbols: $m$ is the order of the B-tree.
Used when: Checking if a node can accommodate a new key during insertion or a borrowed key during deletion.
- Heap array index relationships: For a node at index $k$, its left child is at $2k$ and right child at $2k+1$ (for 1-based indexing).
Symbols: $k$ is the array index of a parent node.
Used when: Implementing a heap as an array to navigate the tree structure.
- Heap sort complexity: $O(n \log n)$
Symbols: $n$ is the number of elements.
Used when: Analyzing the time efficiency of the heap sort algorithm.
- Single heap operation complexity: $O(\log n)$
Symbols: $n$ is the number of elements in the heap.
Used when: Analyzing the cost of a single insertion or deletion (heapify) operation.
Worked examples
B-tree Order 5 Deletion
Scenario: Delete keys from a B-tree of order 5 (max children = 5, min keys per node = $\lceil 5/2 \rceil - 1 = 2$).
1. Delete 95: Key found in a leaf node containing 4 keys (> min). Simply remove the key.
2. Delete 226: Key found in an internal node with the minimum number of keys. Replace it with its inorder predecessor, 300, which had more than the minimum keys. Result: 300 takes the place of 226.
3. Delete 221: Node has minimum keys; its right sibling has more than the minimum. The smallest key from the right sibling (440) moves up to the parent, and the parent key (300) moves down into the node containing 221.
4. Delete 70: Node has minimum keys; both left and right siblings also have only the minimum. Bring down the parent key (65) and merge the node with its left sibling. This is a two-step process: the root key is brought down, forming a new merged node.
B-tree Order 3 Deletion
Scenario: Delete keys from a B-tree of order 3 (a 2-3 tree; min keys = 1, max keys = 2; min children = 2, max children = 3).
1. Delete 35: Leaf had 3 keys (> min of 1). Simply removed the key.
2. Delete 65: Node had the minimum keys (1); its sibling also had the minimum. Merge: bring down the parent key (60), then another key (70) moves down to form a new node with 75.
Heap Construction from Array
Numbers: 97, 88, 95, 66, 55, 95, 48, 66, 38, 48, 55, 62, 77, 25, 38, 18, 40, 36, 26, 24.
A max-heap was built by arranging these numbers into a complete binary tree structure that satisfies the max-heap property.
Heap Insertion Example
Sequence: Insert 44, 30, 50, 22, 60, 55, 77, 55 into an initially empty max-heap.
- Insert 50: Placed, violates heap property, swaps upward.
- Insert 60: Placed, swaps with 30, then swaps with 50.
- Insert 55: Swaps with 40.
- Insert 77: Swaps upward repeatedly.
Heap Sort Simulation (Max-Heap)
Initial Heap Array: 77, 55, 60, 50, 30, 44, 55.
1. Delete 77 (root): Replace root with last element (20). Heapify: 20 swaps down → 60 becomes new root. Sorted list: [77].
2. Delete 60 (root): Replace root with last element (20). Heapify: 20 swaps down → 55 becomes new root. Sorted list: [77, 60].
3. Delete 55 (root): Replace root with last element (44). Heapify: 44 swaps down? (Process described). Sorted list: [77, 60, 55].
4. Delete 50 (root): Replace with 20, heapify → 44 becomes root. Sorted list: [77, 60, 55, 50].
5. Delete 44 (root): Replace with 20, heapify → 30 becomes root. Sorted list: [77, 60, 55, 50, 44].
6. Delete 30 (root): Replace with 20, heapify → 20 becomes root. Sorted list: [77, 60, 55, 50, 44, 30].
7. Delete 20 (root): Final element. Sorted list: [77, 60, 55, 50, 44, 30, 20] (descending order).
Heapify During Deletion (Detailed Step)
Example: After deleting root 77, element 20 is placed at root.
- Compare root (20) with children (60 and another). Swap 20 with 60 (the larger child).
- Now 20 is at a new node. Compare 20 with its new children (45 and 55). Swap 20 with 55 (the larger child).
- Continue until 20 reaches a position where it is greater than or equal to its children (or is a leaf).
Questions asked in class
- Q (Anant): In a B+ tree, if we delete data from a leaf, what happens to the link/address in the internal node?
- A: We delete the link initially; if there is no pointer to that address, it is automatically eliminated in memory.
- Q (Anant): How does insertion traversal work in a B+ tree?
- A: Insertion primarily happens in a leaf. If inserting directly to a leaf, it's a direct pointer. If the leaf level changes, we establish a connection and the corresponding address in root/internal nodes. The root will have the same B-tree structure with duplicate keys stored in the leaves.
- Q (Student): Can you explain duplicate entries as pointers in a B+ tree with an example?
- A: Example: The root/internal node has key 57; the leaf also has 57. The internal node's 57 is an address/pointer; the leaf's 57 holds the actual data. Similarly for keys like 50, 40, etc. More details will be shown in slides next class.
- Q (Student): Why is the last element chosen to replace the root during deletion in a heap?
- A: To maintain the complete binary tree property in the array representation, avoiding gaps. The last element (a leaf) is always chosen.
- Q (Student): Can you explain heapify again?
- A: After placing 20 at the root (replacing 77), since the root must be the maximum, compare 20 with its children. Swap with the larger child (60). Then, compare 20 again with its new children (45 and 55), swap with the larger (55). This process continues until 20 reaches a position where the heap property is satisfied.
Key takeaways
- B-tree deletion has three main cases: simple removal from a non-deficient leaf, borrowing from a sibling, or merging with a sibling, which may cascade upwards.
- In a B+ tree, all data resides in leaf nodes, which are linked together; internal nodes contain only keys and pointers, optimizing range queries.
- A heap is a complete binary tree satisfying the max-heap or min-heap property, efficiently implemented using an array.
- The heapify operation restores the heap property after an insertion (bubble-up) or a root deletion (bubble-down) in O(log n) time.
- Heap sort builds a heap and then repeatedly extracts the root to produce a sorted sequence, with overall O(n log n) time complexity.
- During heap deletion, the last element of the array replaces the root to preserve the complete tree structure before heapify is applied.
- A B-tree of order 3 is a 2-3 tree, where internal nodes (except root) must have between 2 and 3 children.
- The minimum number of keys in a B-tree internal node (except root) is ⌈m/2⌉ - 1.
- A forest in graph theory is an undirected, acyclic graph, equivalently a collection of trees.
- Heap operations (insert, delete max/min) form the basis for an efficient priority queue.
- For a max-heap, heap sort results in a descending order list if the extracted roots are stored in sequence.
- The time complexity for a single insertion or deletion in a heap is O(log n).
Exam-style questions
1. Given a B-tree of order 5, perform the deletion of keys 95, 226, 221, and 70, as described in class. Show the tree after each deletion, explaining which case (simple removal, borrowing, merging) applies.
- Answer Outline: Start with the initial tree. For 95: simple removal from leaf. For 226: replacement with inorder predecessor (300). For 221: borrow smallest key (440) from right sibling, moving parent key (300) down. For 70: merge with left sibling after bringing down parent key (65). Show final structure.
2. Compare and contrast B-trees and B+ trees, listing three key structural differences and one operational advantage of B+ trees for database systems.
- Answer Outline: Structural: Data storage (B-tree: all nodes, B+: leaves only), leaf linking (B+: yes, B-tree: no), key duplication (B+: internal nodes have duplicates as pointers). Operational advantage: B+ trees support efficient range queries and full scans via linked leaves.
3. Insert the elements [44, 30, 50, 22, 60, 55, 77, 55] into an initially empty max-heap. Show the heap (as a tree or array) after each insertion that causes a swap, and describe the heapify process for the insertion of 60.
- Answer Outline: Draw the heap after each insertion. For inserting 60: Add as last node, compare with parent (30), swap (since 60>30). Compare new parent (50), swap (since 60>50). Now 60 is root/child of root, heap property restored.
4. Explain the steps of the heap sort algorithm using a max-heap. Given the max-heap [77, 55, 60, 50, 30, 44, 55], simulate the first two deletions (extract max) showing the heapify process and the growing sorted list.
- Answer Outline: Step 1: Swap root (77) with last element (55), remove 77. Heapify: 55 at root, swap with larger child (60). Sorted list: [77]. Step 2: Swap root (60) with last element (44), remove 60. Heapify: 44 at root, swap with larger child (55). Sorted list: [77, 60]. Show resulting heap.
5. Define a forest in the context of graph theory. How is it related to a tree? What is the key property of its connected components?
- Answer Outline: A forest is an undirected acyclic graph. It is a collection of one or more trees. Its connected components are trees (acyclic and connected). A single tree is a special case of a forest with one connected component.
Glossary
- B-tree - A balanced, M-way search tree where all leaves are at the same level and nodes have between ⌈m/2⌉ and m children.
- B+ tree - A B-tree variant where data is stored only in linked leaf nodes, and internal nodes store duplicate keys as pointers.
- Heap - A complete binary tree satisfying the max-heap (parent ≥ child) or min-heap (parent ≤ child) property.
- Heapify - The process of swapping elements upward or downward to restore the heap property after an insertion or deletion.
- Heap Sort - A sorting algorithm that builds a heap and repeatedly extracts the root element to form a sorted list.
- Complete Binary Tree - A binary tree where all levels are fully filled except possibly the last, which is filled from left to right.
- Forest - An undirected, acyclic graph; a collection of disjoint trees.
- Order (of a B-tree) - The maximum number of children a node can have, denoted by m.
- Inorder Predecessor/Successor - For a key in a tree, the immediately smaller/larger key in the sorted order of the tree.
- Priority Queue - An abstract data type where the highest (or lowest) priority element is accessed first, often implemented using a heap.
Follow-ups
- Detailed B+ tree insertion and deletion examples will be covered in the next class.
- Visual slides showing B+ tree structure and operations will be presented.
- Heap sort's detailed algorithm and time complexity analysis will be explored further.
- Red-black tree properties are scheduled to be covered in the next class (after Diwali).
- The course will transition to graphs after finishing the topic of trees.