堆
In computer science, a heap(堆) is data structure which means "a disorganized pile".
In fact, this word has other meanings in computer science, which refers to heap memory used for dynamic memory allocation. This topic, however, is unrelated to the data structure in this course.
Binary Heap
A binary heap is a complete binary tree(完全二叉树)[1], in which each node represents an item.
Values in the nodes satisfy heap-property:
- Max-heap: for each node except root, value of that node value of its parent.
- Min-heap: for each node except root, value of that node value of its parent.
A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. ↩︎
We can use an array to represent a binary heap. Obtaining parent and children are easy:
- Parent of node :
- Left child of :
- Right child of :
- All in time.
Common operations
Consider max-heap as an example.
Most common operations:
HeapInsert
: insert an element into the heap.HeapGetMax
: Return the item with maximum value.- Runtime is , as the maximum value is always at the root.
HeapExtractMax
: Remove the item with maximum value from the heap and return it.- …
HeapInsert
Insert an item into a binary max-heap represented by an array:
- Simply put the item to the end of the array.
- Along the path to root, compare and swap.
1 | HeapInsert(A, x): |
Runtime of HeapInsert
is .
HeapExtractMax
Remove the maximum item from the heap and return it:
- Remove and return root.
- Move the last item to the root.
- Compare with children, swap with bigger one, and do this recursively.
1 | HeapExtractMax(A): |
Application
Priority Queue
Priority queue: each item associated with a priority, Remove always deletes the item with max (or min) priority.
Use binary heap to implement priority queue:
Add(item)
:HeapInsert(item)
Remove()
:HeapExtractMax()
GetMax()
,UpdatePriority(item, val)
All these operations finish within time.
Application:
- Scheduling, Event simulation, …
- Used in more sophisticated algorithms
HeapSort
1 | HeapSort(I) |
In each iteration:
- Place one item in the array to its final position.
- This one is the maximum item in current heap
- That is, place -th biggest item to position .
The loop invariant: The largest elements are already in their correct position.
Total runtime of these iterations is :
How to build a max-heap for an array I[1...n]
? We can call HeapInsert
times, which time complexity is , but we can do it better.
An observation: each leaf node is a 1-item heap.
Bottom-up approach: Keep merging small heaps into larger one, until a single heap remains.
- Each leaf node is a 1-item heap.
- Go through remaining nodes in index decreasing order
- At each node, we are merging two heaps.
- Maintain heap property during merging: Use
MaxHeapify
1 | BuildMaxHeap(A): |
calls to MaxHeapify
, each costing . But the result is not .
Height of -items heap is . Any height has no more than nodes. So cost of all MaxHeapify
is[1]
Time complexity of HeapSort
is and extra space required during execution is .
for . Differentiate both sides to get . So . ↩︎