A heap is a binary tree-based data structure that satisfies the heap property. Depending on whether it is a max-heap or min-heap, the heap property states that:
The common operations that can be performed on a heap include insertion, extraction, heapification, and others. Let's break down these operations in detail.
The insert operation adds a new element to the heap while maintaining the heap property.
Suppose we have the following max-heap and we want to insert 15:
Initial max-heap:
20
/ \
15 10
/ \ /
8 6 5
Insert 15 at the end:
20
/ \
15 10
/ \ / \
8 6 5 15
Bubble up 15: Compare it with its parent (10). Since 15 > 10, swap them.
20
/ \
15 15
/ \ / \
8 6 5 10
Bubble up 15: Compare it with its new parent (20). Since 15 < 20, no further action is needed.
Final max-heap:
20
/ \
15 15
/ \ / \
8 6 5 10
The extract root operation removes and returns the root element of the heap (the maximum in a max-heap or the minimum in a min-heap) and then restores the heap property by re-adjusting the remaining elements.
Starting with the following max-heap:
20
/ \
15 10
/ \ /
8 6 5
Swap the root (20) with the last element (5):
5
/ \
15 10
/ \ /
8 6 20
Remove 20 (the last element).
Heapify the root (5):
5 with its children (15 and 10), and swap with the larger child (15). 15
/ \
5 10
/ \
8 6
5 with its new children (8 and 6). Swap with the larger child (8): 15
/ \
8 10
/ \
5 6
Final max-heap after extraction:
15
/ \
8 10
/ \
5 6
The peek operation simply returns the root element of the heap without removing it. This operation is useful when you need to access the maximum (in a max-heap) or minimum (in a min-heap) element, but don't want to modify the heap.
In the max-heap:
20
/ \
15 10
/ \ /
8 6 5
The peek operation will return 20 without altering the heap.
The heapify operation is used to restore the heap property for a subtree rooted at a given index. It is essential in operations like building a heap or extracting the root.
Heapify the subtree rooted at index 0 (root) in the following max-heap:
5
/ \
3 8
/ \
1 6
Compare 5 with its children (3 and 8). Since 8 is the largest, swap 5 and 8.
8
/ \
3 5
/ \
1 6
Now heapify the subtree rooted at index 2 (5). Compare 5 with its children (6), and swap them:
8
/ \
3 6
/ \
1 5
Final max-heap after heapify:
8
/ \
3 6
/ \
1 5
To convert an unsorted array into a heap, the build heap operation can be used. This process involves calling the heapify operation from the last non-leaf node (index n/2 - 1) down to the root.
Given the array [10, 20, 5, 6, 1, 8], we will build a max-heap.
Start from index 2 and heapify:
[10, 20, 5, 6, 1, 8]
Heapify index 2 (node 5):
5 with 8 (its right child) and swap them:[10, 20, 8, 6, 1, 5]
Move to index 1 and heapify:
20 with its children (6 and 1). No swap is needed because 20 is already the largest.Move to index 0 and heapify:
10 with its children (20 and 8). Swap 10 and 20:[20, 10, 8, 6, 1, 5]
Heapify index 1 again:
10 with 6 and 1. Swap 10 with 6:[20, 6, 8, 10, 1, 5]
Final max-heap:
20
/ \
6 8
/ \ /
10 1 5
The delete operation removes a node from the heap. To delete a node, we can:
Open this section to load past papers