A heap is a specialized binary tree-based data structure that satisfies the heap property. The heap property is defined as follows:
Max-Heap Property: In a max-heap, the value of each node is greater than or equal to the values of its children, and the largest element is always at the root of the tree.
Min-Heap Property: In a min-heap, the value of each node is smaller than or equal to the values of its children, and the smallest element is always at the root of the tree.
Heaps are generally used to implement priority queues, and they support efficient operations like inserting elements, extracting the maximum or minimum, and building the heap from an unsorted array.
Shape Property:
Heap Property:
Heaps support the following key operations:
Let’s go through an example of a Max-Heap.
Given an array of elements:
[10, 20, 5, 6, 1, 8]
We will convert this array into a max-heap step-by-step.
We will apply the heapify operation starting from the last non-leaf node (index n/2 - 1).
Initial array:
[10, 20, 5, 6, 1, 8]
[10, 20, 8, 6, 1, 5]
Index 1 (Node 20): The left child is 6, and the right child is 1. Since 20 is already larger than both, no swap is needed.
Index 0 (Node 10): The left child is 20, and the right child is 8. Since 20 is the largest, we swap 10 and 20:
[20, 10, 8, 6, 1, 5]
Now, heapify is applied to the subtree rooted at index 1.
After these steps, the array represents the following max-heap structure:
20
/ \
10 8
/ \ /
6 1 5
Let’s now consider an example of a Min-Heap.
Given the same array of elements:
[10, 20, 5, 6, 1, 8]
We will convert this array into a min-heap.
We will apply the heapify operation starting from the last non-leaf node (index n/2 - 1).
Initial array:
[10, 20, 5, 6, 1, 8]
Index 2 (Node 5): The left child is 8, and the right child is out of bounds (no child). Since 5 is smaller than 8, no swap is needed.
Index 1 (Node 20): The left child is 6, and the right child is 1. The smallest value is 1, so we swap 20 and 1:
[10, 1, 5, 6, 20, 8]
Now, heapify is applied to the subtree rooted at index 4.
Index 0 (Node 10): The left child is 1, and the right child is 5. The smallest value is 1, so we swap 10 and 1:
[1, 10, 5, 6, 20, 8]
Now, heapify is applied to the subtree rooted at index 1.
[1, 6, 5, 10, 20, 8]
Now, heapify is applied to the subtree rooted at index 3.
After these steps, the array represents the following min-heap structure:
1
/ \
6 5
/ \ /
10 20 8
Insertion (Insert a value):
Insertion in a heap involves adding an element at the next available position in the tree (i.e., at the end of the array) and then "bubbling up" to restore the heap property.
Example: Insert 4 into the max-heap [20, 10, 8, 6, 1, 5].
4 at the end: [20, 10, 8, 6, 1, 5, 4]4 is less than 6 but greater than 1, no swap is needed, and the heap is valid.Extracting the Root (Extract the max in a max-heap or min in a min-heap):
Extracting the root removes the root element (max or min) and replaces it with the last element in the heap. Afterward, the heap property is restored by "bubbling down."
Example: Extract the root from the max-heap [20, 10, 8, 6, 1, 5].
20, replace it with 5: [5, 10, 8, 6, 1]5 with 10 to get [10, 5, 8, 6, 1], and then swap 5 with 6 to get [10, 6, 8, 5, 1].Peek (View the root):
The peek operation simply returns the root element without modifying the heap. In a max-heap, this will return the maximum value, and in a min-heap, this will return the minimum value.
Example: For the max-heap [20, 10, 8, 6, 1, 5], the peek operation will return 20.
Heapify (Restore the heap property):
The heapify operation restores the heap property by comparing a node with its children and swapping if necessary. It is applied recursively to ensure that the heap property is maintained.
Priority Queues:
Heap Sort:
Open this section to load past papers