# How Is Heap Data Structure Implemented?

//

Heather Bennett

How Is Heap Data Structure Implemented?

A heap is a specialized tree-based data structure that satisfies the heap property. In computer science, heaps are commonly used to implement priority queues, which allow efficient access to the element with the highest (or lowest) priority. In this article, we will explore how a heap data structure is implemented.

## Representation of a Heap

A heap can be represented as an array, where each element corresponds to a node in the heap. The relationships between elements in the array are derived from their indices.

To illustrate this, let’s consider a min-heap, where the value of each parent node is smaller than or equal to its children. In this case, given an element at index i:

• The left child is at index 2i + 1
• The right child is at index 2i + 2
• The parent is at index floor((i – 1) / 2)

## Operations on a Heap

### Insertion

When inserting an element into a heap, it is initially placed at the last position of the array representation. Then, it “bubbles up” by swapping with its parent until it reaches its correct position based on the heap property.

``````<script>
function insert(heapArray, value) {
heapArray.push(value);
let currentIndex = heapArray.length - 1;

while (currentIndex > 0) {
let parentIndex = Math.floor((currentIndex - 1) / 2);

if (heapArray[currentIndex] >= heapArray[parentIndex]) {
break; // Element is in correct position
}

// Swap element with its parent
[heapArray[currentIndex], heapArray[parentIndex]] = [heapArray[parentIndex], heapArray[currentIndex]];

currentIndex = parentIndex;
}
}

let minHeap = [];
insert(minHeap, 5);
insert(minHeap, 3);
insert(minHeap, 8);
</script>``````

### Deletion

When deleting the root element of a heap (the element with the highest priority in a max-heap or the lowest priority in a min-heap), we replace it with the last element in the array representation. Then, we “bubble down” this new root by swapping it with its smallest (or largest) child until it reaches its correct position.

``````<script>
function deleteRoot(heapArray) {
if (heapArray.length === 0) {
return; // Heap is empty
}

const rootValue = heapArray[0];
const lastValue = heapArray.pop();

if (heapArray.length > 0) {
heapArray[0] = lastValue;

let currentIndex = 0;

while (true) {
let leftChildIndex = 2 * currentIndex + 1;
let rightChildIndex = 2 * currentIndex + 2;
let smallestChildIndex = null;

if (leftChildIndex < heapArray.length) {
smallestChildIndex = leftChildIndex;

if (rightChildIndex < heapArray.length && heapArray[rightChildIndex] < heapArray[leftChildIndex]) {
smallestChildIndex = rightChildIndex;
}
}

if (
smallestChildIndex === null ||
heapArray[currentIndex] <= heapArray[smallestChildIndex]
) {
break; // Element is in correct position
}

// Swap element with its smallest child
[heapArray[currentIndex], heapArray[smallestChildIndex]] = [heapArray[smallestChildIndex], heapArray[currentIndex]];

currentIndex = smallestChildIndex;
}
}

return rootValue;
}

let maxHeap = [10, 8, 5, 3];
let rootValue = deleteRoot(maxHeap);
console.log(rootValue); // Output: 10
</script>``````

## Conclusion

A heap data structure is a powerful tool for efficiently managing priority queues. By understanding the array representation and the operations of insertion and deletion, you can leverage heaps to optimize your algorithms. Remember to consider whether you need a min-heap or a max-heap based on your specific requirements.