Heap
- Also called Priority Queue
- It is implemented by a Complete binary tree and can be stored in an array. So the index can be computed easily.
- index of left child = index of parent * 2 + 1
- index of right child = index of parent * 2 + 2
- index of parent = (index of child - 1) / 2
- index of root = 0m
- 任意节点小于它的所有后裔,最小元素在堆的根上(堆序性)。
- 根最小的堆叫最小堆,根最大的堆叫最大堆。
Operations
insert
- -percolateUp
update
-get
/top
-pop
- -percolateDown
heapify
- make an unsorted array into a heap.- heap sort
Useless Operations
find
- 因为左右儿子没关系remove(int index)
- 需要先find
Problems
Q1. Find smallest k elements from an unsorted array of size n
- Solution 0 - sort
- Solution 1 - 一批全吃掉
- heap sort using a min heap of size n
- Step 1 - heapify
- Step 2 - call
pop()
k times to get the k smallest elements
- Solution 2 - 数据量很大,一个批次做不到。
- use a max heap of size k as smallest k candidates
- Step 1 - heapify the first k elements to form a max-heap of size=k
- Step 2 - iterate over the rest n-k elements one by one to get the real k smallest elements.
- Compare Solution 1 vs. Solution 2
Compare | min heap of size n | max heap of size k |
---|---|---|
Time | O(n + klogn) | O(k + (n-k) log k) |
k<<n | O(n) | O(n log k) |
k~~n | O(n log n) | O(n log n) |
- Solution 3 - quick select
- average time =
- worst time =
Q2. Kth smallest number in sorted matrix
12345
34569
489AB
- use a N * N-size min heap to store everything and poll k times -
- Best First Search
- use a priority queue to store the candidates and pop the smallest every time
- pop the smallest for k times
- Merge K sorted lists and use a k-size max heap to store the heads of the lists