heap.definition

heap.descriptionPart1

heap.descriptionPart2

heap.keyPointsTitle

  • heap.keyPoint1
  • heap.keyPoint2
  • heap.keyPoint3
  • heap.keyPoint4

heap.exampleTitle

50
30
40
10
20
15
25

heap.exampleCaption

heap.syntaxTitle

// Java
class MaxHeap {
    int[] heap = new int[31];
    int size = 0;

    void insert(int value) {
        heap[size] = value;
        int i = size++;
        // Bubble up
        while (i > 0 && heap[i] > heap[(i-1)/2]) {
            swap(i, (i-1)/2);
            i = (i-1)/2;
        }
    }
}
# Python
class MaxHeap:
    def __init__(self):
        self.heap = []

    def insert(self, value):
        self.heap.append(value)
        i = len(self.heap) - 1
        # Bubble up
        while i > 0 and self.heap[i] > self.heap[(i-1)//2]:
            parent = (i-1)//2
            self.heap[i], self.heap[parent] = \
                self.heap[parent], self.heap[i]
            i = parent
// C++
class MaxHeap {
    vector<int> heap;
public:
    void insert(int value) {
        heap.push_back(value);
        int i = heap.size() - 1;
        // Bubble up
        while (i > 0 && heap[i] > heap[(i-1)/2]) {
            swap(heap[i], heap[(i-1)/2]);
            i = (i-1)/2;
        }
    }
};
// JavaScript
class MaxHeap {
  constructor() { this.heap = []; }

  insert(value) {
    this.heap.push(value);
    let i = this.heap.length - 1;
    // Bubble up
    while (i > 0 && this.heap[i] > this.heap[Math.floor((i-1)/2)]) {
      const parent = Math.floor((i-1)/2);
      [this.heap[i], this.heap[parent]] =
        [this.heap[parent], this.heap[i]];
      i = parent;
    }
  }
}

Interactive Max Heap Visualization

Array Representation

Heap is empty. Insert values to begin.

Tree Representation

๐ŸŒณ

Heap is empty. Insert values to begin.

Size:0
Max Value:โ€”
Capacity:31
Normal
Inserting
Bubble Up
Bubble Down
Comparing
Highlight
Extracting

Note: In a max heap, each parent node has a value greater than or equal to its children. Parent index = floor((i-1)/2), Left child = 2i+1, Right child = 2i+2.

enhanced.realWorldExamples.title

Operating SystemsProcess Scheduling

OS schedulers use priority queues (heaps) for CPU allocation.

Why? O(log n) insertion and extraction of highest priority.

Navigation AppsShortest Path Finding

GPS apps use heaps in Dijkstra's algorithm for routing.

Why? Efficiently extracts next closest node.

Time Complexity

OperationAverage CaseWorst CaseDescription
InsertO(log n)O(log n)Add to end, then bubble up to restore heap property
Extract MaxO(log n)O(log n)Remove root, replace with last element, bubble down
Find MaxO(1)O(1)Return root element (always at index 0)
Heapify (Build)O(n)O(n)Build heap from array using bottom-up approach
Increase KeyO(log n)O(log n)Update value and bubble up if needed

Heap property: In a max heap, every parent node is greater than or equal to its children.

Array representation: Parent index = floor((i-1)/2), Left child = 2i+1, Right child = 2i+2.

Space complexity: O(n) for storing n elements. Operations use O(1) auxiliary space.

enhanced.complexityAnalysis.title

enhanced.complexityAnalysis.mathematicalTitle

Heap operations involve traversing tree height. A complete binary tree of n nodes has height โŒŠlogโ‚‚(n)โŒ‹, so operations are O(log n).

๐ŸŸขBest Case

O(1) for peek/find-min. O(1) for insert when new element is largest (no bubble-up needed).

๐ŸŸกAverage Case

O(log n) for insert and extract-min/max operations.

๐Ÿ”ดWorst Case

O(log n) for insert (bubble up entire height) and extract (bubble down entire height).

๐Ÿ’พSpace Complexity

O(n) using array representation. No pointer overhead needed.

โšกCache Performance

Good cache locality with array-based implementation due to level-order storage.

enhanced.relatedTopics.prerequisites

enhanced.relatedTopics.related

enhanced.relatedTopics.advanced

heap.continueLearning

enhanced.author.title

enhanced.author.writtenBy:enhanced.author.teamName
enhanced.author.reviewedBy:enhanced.author.reviewerName
enhanced.author.lastUpdated:January 29, 2026