Heap Visualizer
Explore the heap data structure with an interactive visualizer that demonstrates heap properties, insertion with bubble-up, and removal with bubble-down operations.Try the Live Visualizer
Interact with the heap visualizer to see how elements maintain the heap property through insertion and removal
What is a Heap?
A heap is a specialized binary tree data structure that satisfies the heap property:- Max Heap
- Min Heap
In a max heap, every parent node has a value greater than or equal to its children. The root node always contains the maximum value.
Key Properties
Complete Binary Tree
Heaps are filled from left to right at each level. All levels are completely filled except possibly the last level.
Array Implementation
Heaps are efficiently stored as arrays since they’re always complete binary trees, making them space-efficient.
Parent-Child Relationships
Heaps use simple formulas to navigate the tree structure when stored as an array:For any element at index
i:- Parent index:
(i - 1) / 2 - Left child index:
(2 * i) + 1 - Right child index:
(2 * i) + 2
Operations
Insert Operation
Insertion maintains the heap property through a bubble-up process:Add to End
Insert the new element at the end of the array (bottom-right of the tree) to maintain the complete binary tree property.
Bubble Up
Compare the new element with its parent. If it violates the heap property (greater than parent in max heap), swap them.
[10, 5, 6, 5, 4, 6]
Time Complexity: O(log n) - In the worst case, the element bubbles up the entire height of the tree
Remove Operation
Removal always extracts the root and uses bubble-down to restore the heap:Replace with Last
Move the last element in the array to the root position to maintain the complete tree property.
Bubble Down
Compare the new root with its children. Swap with the larger child (in max heap) if it violates the heap property.
[10, 5, 6, 5, 4, 3]
Time Complexity: O(log n) - The element may need to bubble down the entire height of the tree
Using the Visualizer
The heap visualizer provides both a tree view and an array representation:Step Through Heapify
If the insertion violates the heap property, click Next Step to see the bubble-up process in action.
Remove Elements
Click Remove to extract the root. Use Next Step to visualize the bubble-down process.
Implementation Details
The visualizer implements these core helper methods:Time Complexity Summary
Insert
O(log n)Bubble up operation
Remove
O(log n)Bubble down operation
Peek
O(1)Access root element
Common Use Cases
- Priority Queues: Efficiently manage tasks by priority
- Heap Sort: O(n log n) sorting algorithm
- Dijkstra’s Algorithm: Finding shortest paths in graphs
- Median Maintenance: Track running median in streams
- Job Scheduling: Operating system task management
Related Visualizers
Priority Queue
See how heaps power priority queue operations
Binary Tree
Explore general binary tree traversals
Interactive Heap Visualizer
Practice inserting and removing elements with step-by-step visualization