Binary Heap
📚 What is a Binary Heap?
A Binary Heap is a complete binary tree that satisfies the heap property.
-
Complete Binary Tree: All levels are completely filled except possibly the last level, and the last level has all nodes as left as possible.
-
Heap Property:
-
Max-Heap: The value of each parent node is greater than or equal to its child nodes.
-
Min-Heap: The value of each parent node is less than or equal to its child nodes.
-
🔥 Types of Binary Heaps
Max Heap➔ Root node has the largest value.
➔ Each parent is ≥ its children.
➔ Root node has the smallest value.
➔ Each parent is ≤ its children.
📈 How is a Binary Heap Represented?
-
Using an array (very important!)
-
If a node is at index
i, then:-
Left Child is at index
2*i + 1 -
Right Child is at index
2*i + 2 -
Parent is at index
(i - 1)/2
-
Array representation saves space and makes operations efficient.
⚡ Operations on Binary Heap
1. Insert a Node
-
Add the new element at the end (next available spot).
-
"Heapify Up" → Move up until the heap property is restored.
Algorithm for Insert in Max-Heap:
2. Delete Root Node (Extract Max or Min)
-
Root holds max (in Max-Heap) or min (in Min-Heap).
-
Remove the root node.
-
Replace it with the last node.
-
"Heapify Down" → Move down to maintain heap property.
Algorithm for Delete in Max-Heap:
3. Heapify
-
Heapify is the process of adjusting a node and its subtree to satisfy the heap property.
-
Used in insertion, deletion, and heap creation.
There are two types:
-
Heapify Up: when inserting
-
Heapify Down: when deleting root
🛠️ Important Points
-
Insertion → O(log n) time
-
Deletion → O(log n) time
-
Building a heap from array → O(n) time
Get Maximum or Minimum → O(1) time
-
Heaps are mainly used to implement priority queues.
🎯 Quick Example of Max-Heap
Suppose we insert: 40, 20, 30, 35, 25, 80, 32
Step-by-Step Insertions
🔹 Insert 40
Heap:
🔹 Insert 20
✔ Heap property holds.
🔹 Insert 30
✔ Heap property holds.
🔹 Insert 35
Insert at next available spot (left child of 20):
Now compare with parent 20 → 35 > 20 → swap.
✔ Heap property holds.
🔹 Insert 25
Insert at right child of 35:
Parent 35 ≥ 25 → no swap needed.
🔹 Insert 80
Insert at left child of 30:
Compare 80 > 30 → swap:
Now compare 80 > 40 → swap again:
✔ Heap property restored.
🔹 Insert 32
Insert at right child of 40:
Parent 40 ≥ 32 → ✔ heap property holds.
✅ Final Max Heap after all insertions:
80 is at the root (largest element).
✅ Final Max Heap (Array Representation)
✨ Summary Table
| Operation | Description | Time Complexity |
|---|---|---|
| Insert | Add element at end and heapify up | O(log n) |
| Delete (Root) | Replace root with last element and heapify down | O(log n) |
| Build Heap | Create a heap from an unsorted array | O(n) |
| Find Max/Min | Return root element (no changes needed) | O(1) |
✨ C Program - Binary Heap
🛡️ Important Points:
-
Insert: Add at the end and heapify up (O(log n))
-
Delete Root: Replace root with last, heapify down (O(log n))
-
Display: Just traverse array
🔥 Summary
With these simple algorithms:
-
You can build a heap.
-
Insert elements dynamically.
-
Delete the root (max or min value depending on the heap).
-
View the heap structure any time.


Comments
Post a Comment