Inserting in a Heap
Some basics :
Heap is stored in an array representation.
Starting index is considered to be 1. Element at index 0 will be 0.
For an element in index i, its left child will be at index 2i and right child will be at index 2i + 1.
For elements at index > 2, its parent element will be at the floor value of index /2.
Inserting in a Max Heap :
void insertMaxHeap(int h[], int data, int index) {
// Check if data is greater than parent
while (index > 1 && data > h[index/2]) {
h[index] = h[index/2];
index = index/2;
}
h[index] = data;
}Inserting in a Min Heap :
Creating a Heap from Existing Array :
Last updated
Was this helpful?