WEEK 6
4 堆 Priority Queues (Heaps)
4.1 ADT Model
- Objects :A finite ordered list with zero or more elements.
- Operations :
- PriorityQueue Initialize( int MaxElements );
- void Insert( ElementType X, PriorityQueue H );
- ElementType DeleteMin( PriorityQueue H );
- ElementType FindMin( PriorityQueue H );
4.2 Implementations
Array
-
Insertion — add one item at the end ~\(\Theta(1)\)
-
Deletion — find the largest / smallest key ~\(\Theta(n)\)
remove the item and shift array ~\(O(n)\)
Linked List
-
Insertion — add to the front of the chain ~\(\Theta(1)\)
-
Deletion — find the largest / smallest key ~\(\Theta(n)\)
remove the item ~\(\Theta(1)\)
- Never more deletions than insertions
Ordered Array
- Insertion — find the proper position ~\(O(\log n)\)
shift array and add the item ~\(O(n)\)
- Deletion — remove the first / last item ~\(\Theta(1)\)
Ordered Linked List
- Insertion — find the proper position ~\(O(n)\)
add the item ~\(\Theta(1)\)
- Deletion — remove the first / last item ~\(\Theta(1)\)
Binary Search Tree
- Both insertion and deletion will take \(O(\log N)\) only.
- Only delete the the minimum element, always delete from the left subtrees.
- Keep a balanced tree
- But there are many operations related to AVL tree that we don't really need for a priority queue.
4.3 Binary Heap
Structure Property
[Definition] A binary tree with \(n\) nodes and height \(h\) is complete if its nodes correspond to the nodes numbered from \(1\) to \(n\) in the perfect binary tree of height \(h\).
-
A complete binary tree of height \(h\) has between \(2^h\) and \(2^{h+1}-1\) nodes.
-
\(h=\lfloor\log N\rfloor\)
-
Array Representation : BT[n + 1] ( BT[0] is not used)
[Lemma]
- \(index\,of\,parent(i)=\left\{ \begin{array}{rcl} \lfloor i/2\rfloor && {i\neq1}\\ None && {i=1}\\ \end{array} \right.\)
- \(index\,of\,left\_child(i)=\left\{ \begin{array}{rcl} 2i && {2i\leq n}\\ None && {2i>n}\\ \end{array} \right.\)
- \(index\,of\,right\_child(i)=\left\{ \begin{array}{rcl} 2i+1 && {2i+1\leq n}\\ None && {2i+1>n}\\ \end{array} \right.\)
Heap Order Property
[Definition] A min tree is a tree in which the key value in each node is no larger than the key values in its children (if any). A min heap is a complete binary tree that is also a min tree.
- We can declare a max heap by changing the heap order property.
Basic Heap Operations
- Insertion
$$ T(N)=O(\log N) $$
- DeleteMin
Other Heap Operations
-
查找除最小值之外的值需要对整个堆进行线性扫描
-
DecreaseKey — Percolate up
-
IncreaseKey — Percolate down
-
Delete
-
BuildHeap
将N 个关键字以任意顺序放入树中,保持结构特性,再执行下滤
$$ T(N)=O(N) $$
[Theorem] For the perfect binary tree of height \(h\) containing \(2^{h+1}-1\) nodes, the sum of the heights of the nodes is \(2^{h+1}-1-(h+1)\).
4.4 Applications of Priority Queues
Heap Sort
查找一个序列中第k小的元素
The function is to find the K
-th smallest element in a list A
of N
elements. The function BuildMaxHeap(H, K)
is to arrange elements H[1]
... H[K]
into a max-heap.
4.5 \(d\)-Heaps — All nodes have \(d\) children
Note :
- DeleteMin will take \(d-1\) comparisons to find the smallest child. Hence the total time complexity would be \(O(d \log_d N)\).
- 2 or /2 is merely *a bit shift**, but *d or /d is not.
- When the priority queue is too large to fit entirely in main memory, a d-heap will become interesting.
正确答案是4,注意“in the process”