Heap/Priority Queue
Properties
- 任意节点小于它的所有后裔,最小元素在root上
- heap总是complete tree. 可以将所有元素放在一个array里。 complete tree: 每一层都fully filled, 除了最后一层,如果最后一层有缺,尽量往左靠。
- 将根结点最大的heap叫max heap,根结点最小的heap叫min heap。
- index of lChild = index of parent*2+1
- index of rChild = index of parent*2 +2
- unsorted but follow rules above
Operations
- insert: 先放在最后,持续往上swap。时间复杂度O(log(n))
- update: 将新元素提升使其符合heap的性质,如果新元素比原来的元素大,和更小的child互换。时间复杂度O(log(n))
- get/top
- pop: pop之后,把最后一个元素放在上面
- heapify: 使得一个unsorted array变成一个heap。时间复杂度O(n)。
经典考题
Find smallest k elements from an unsorted array of size n.
Assumption: What is the relationship between k and n?
Solution 1:
sort it and return the first k elements. O(log(n).
Solution 2:
- Build a min-heap: heapify it -> O(n).
- Keep popping out k element O(klogn). 因为pop完还要重新组合。
Solution 3:
- use a max-heap of size k -> O(logk),先heapify前k个elements
- from the k+1th element to the nth element, and for the current element x
Case1: if x < max-heap.top(), max-heap.pop(), and max-heap.insert(x);
Case2: else, do nothing - O(k) + O((n-k)log(k))
Compare Solution 2 and Solution 3
- Case1: k<<<<<<<n, it depends, how small is k
- Case2: k = n, it depends, hard to say, depending on the value of k versus n