ds 09 heaps.ppt [相容模式]symmetric min-max heap, smmh double-ended priority queue, depq...
TRANSCRIPT
-
Lesson 9
Heaps
Data Structures
James C.C. Cheng
Department of Computer Science
National Chiao Tung University
-
Overview Heap
A complete binary tree
It is implemented by an array
Its node has four member data
Max-heap for every node i other than the root, A[A[i].parent].key ≥ A[i].key
Min-heap for every node i other than the root, A[A[i].parent].key ≤ A[i].key
2
template struct HeapNode{
int left, right, parentT key;HeapNode(const T& k = T(), int L=-1, int R = -1, int P = -1):
key(T), left(L), right( R ), parent(P){}}
-
Heapify The kernel function of Heap maintaining
Time = O(log n), where n is the number of nodes
3
// A is a heap data array, i is an index of node // heap_size is the number of nodes in Atemplate void MaxHeapify (HeapNode * A, int i, int heapSize){
int L =A[i].left, R = A[i].right;int largest;
if (L < heapSize and A[L] > A[i]) largest = L;else largest = i;
if (R < heapSize and A[R] > A[largest] ) largest = R;
if (largest != i ){swap( A[i], A[largest]);MaxHeapify (A, largest, heapSize);
}}
-
Heapify Example:
4
2 8
14
1
7 9 3
4 10
16
i=1
0
2
3 4 5 67 8 9
2 8
4
1
7 9 3
14 10
16
1
0
2
i=3 4 5 67 8 9
2 4
8
1
7 9 3
14 10
16
1
0
2
3 4 5 67 i=8 9
-
Heap Building Bottom-up heap building:
Example:
5
template void BuildMaxHeap (HeapNode* A, int heapSize){
for(int i = (heapSize-1) / 2; i >=0; --i)MaxHeapify (A, i, heapSize)
}
14 8
2
7
16 9 10
1 3
41
0
2
3 i=4 5 6
7 8 9
14 8
2
7
16 9 10
1 3
41
0
2
i=3 4 5 6
7 8 9
-
Heap Building Example:
6
2 8
14
7
16 9 10
1 3
41
0
i=2
3 4 5 6
7 8 92 8
14
7
16 9 3
1 10
4i=1
0
2
3 4 5 6
7 8 9
2 8
14
7
1 9 3
16 10
4i=1
0
2
3 4 5 6
7 8 9
2 8
14
1
7 9 3
16 10
41
i=0
2
3 4 5 6
7 8 9
-
Heap Building Example:
7
2 8
14
1
7 9 3
4 10
161
i=0
2
3 4 5 6
7 8 9
2 8
4
1
7 9 3
14 10
161
i=0
2
3 4 5 6
7 8 9
2 4
8
1
7 9 3
14 10
161
i=0
2
3 4 5 6
7 8 9
-
Heap Building Time complexity
Given n nodes, and the height of heap tree
the depth of node i:
The maximum time of MaxHeapify() on node i:
8
)(
12
2log2
log2
log2
loglog2
)1log(log2
1))1log(log(
)()(
21
2/)1(
0
2/)1(
0
2/)1(
0
nO
nnnnnnxdxnn
innin
hhnT
n
n
i
n
i
n
ii
nh log1 )1log(1 ihi
ihh
-
Heap Sort
Time complexity: O(n log n),
Example
9
template void HeapSort (HeapNode* A, int heapSize, int arraySize){
BuildMaxHeap(A, heapSize)for( int i = arraySize – 1; i>=1; --i){
swap( A[0], A[i] );--heapSize;MaxHeapify( A, 0, heapSize);
}}
-
Heap Sort Example
10
-
Priority Queues A priority queue is a data structure for maintaining a set A of
elements, each with an associated value called a key. A max-priority queue supports the following operations. T& HeapMax (A) ------ ϴ(1)
returns the element of A with the largest key.
T HeapExtractMax (A , int& heapSize) ------ O(logn) removes and returns the element of A with the largest key.
void HeapIncreaseKey (A, int i, const T& k) ------ O(logn) increases the value of node i's key to the new value k, where k > A[i].
void HeapInsert (A, const T& x , int& heapSize ) ------ O(logn) inserts the element x into the set A = A ∪ {x}.
11
-
Priority Queues HeapMax: the root of a max heap is the largest key
HeapExtractMax
12
template inline T& HeapMax (HeapNode* A) { return A[0]; }
template T HeapExtractMax(HeapNode* A , int& heapSize){
if (heapSize
-
Priority Queues HeapIncreaseKey
Time: the maximum running time of the while loop is the height of heap tree O(logn)
13
template void HeapIncreaseKey (HeapNode* A, int i, const T& k){
if (k > A[i]){A[i] = keywhile( i >= 0 && A[ A[i].parent ] < A[i] ){
swap( A[i], A[ A[i].parent ] );i = A[i].parent ;
}}
}
-
Priority Queues HeapInsert
Time = O(logn)
14
template void HeapInsert (HeapNode* A, const T& x , int& heapSize ){
A[heapSize++] = -∞;HeapIncreaseKey ( A, heapSize - 1, x)
}
-
Priority Queues Example of insertion
15
4 4
-∞
4
1
4
1 -∞
4
1 3
4
1 3
-∞
4
1 3
2
4
2 3
1
4
2 3
1 -∞
4
2 3
1 16
16
4 3
1 2 -∞
16
4 3
1 2 9
16
4 9
1 2 3 -∞
16
4 9
1 2 3 10
-
Priority Queues Example of insertion
16
16
4 10
1 2 3 9
-∞
16
4 10
1 2 3 9
14
16
14 10
4 2 3 9
1 -∞
16
14 10
4 2 3 9
1 8
16
14 10
8 2 3 9
1 4 -∞
16
14 10
8 7 3 9
1 4 2
-
Priority Queues Top-down heap building
Using HeapInsert Time:
Best case: O(n)
worst case: Θ(n lg n)
17
-
Exercise Finish the deletion operation for max-heap
template void HeapDelete (HeapNode* A, int i, int& heapSize ); A is the array of heap, i is the node to be deleted and heapSize is the number
of nodes
Time: O(logn)
It is similar to HeapInsert
Using a boolean variable, MaxMin, to switch Max-heap and Min-heap If MaxMin is true max-heap
Else min-heap
18
-
Symmetric Min-Max Heap, SMMH Double-Ended Priority Queue, DEPQ
Properties (old version) The root is empty
The Left subtree is a min-heap
The Right subtree is a max-heap
If the right subtree is not empty, then let i be any node in the left subtree. Let j be the corresponding node in the right subtree. If such node j does not exist, then let j be the node in the right subtree that corresponds to the parent of i. The key in node i is less than or equal to that of j.
19
5 45
10 8 25 40
15 19 9 30 20
1log
1log
2
2
22
)(,2
j
i
ji
jjnjifijroot: i= 1 1
32
4 5 6 7
89 10
11
12
-
Symmetric Min-Max Heap, SMMH Check whether the node i is in the max-heap, Θ(1)
The maximum number of nodes on height h is 2h-1 , h ≥ 1
The maximum number of nodes in a binary tree of height h is 2h -1, h ≥ 1
The index of mid node m on height h is
20
bool MaxHeap(int i){if ( i > 3 * pow(2, floor(log2i) - 1) – 1 ) return true;else return false;
}
2,1232)12( 221
hm hhh
-
Symmetric Min-Max Heap, SMMH Insertion
21
5 45
10 8 25 40
15 19 9 30 20
1
32
4 5 6 7
89 10 11 12
4
13
5 45
10 8 25 40
15 4 9 30 20
1
32
4 5 6 7
89 10 11 12
19
13
5 45
4 8 25 40
15 10 9 30 20
1
32
4 5 6 7
89 10 11 12
19
13
4 45
5 8 25 40
15 10 9 30 20
1
32
4 5 6 7
89 10 11 12
19
13
4 < 19? Yes, then swap them
-
Symmetric Min-Max Heap, SMMH Insertion
22
4 45
5 8 25 40
15 10 9 30 20 19 50
50 < 9? No!
4 50
5 8 25 45
15 10 9 30 20 19 40
-
Symmetric Min-Max Heap, SMMH Insertion
23
4 45
5 8 25 40
32 > 25?
55
4 45
5 8 55 40
swap
25
4 55
5 8 45 40
25
4 55
5 8 45 40
25 22 > 40? No!
2 55
4 8 45 40
25 5
-
Symmetric Min-Max Heap, SMMH Insertion
Time complexity = O(logn) Check whether the node is in max-heap: Θ(1)
Find the corresponding node: Θ(1)
Swap: Θ(1)
Heap adjustment: Heapify(): O(logn)
24
-
Symmetric Min-Max Heap, SMMH Deletion
Only the maximum node and the minimum node can be removed
25
4 45
5 8 25 40
15 10 9 30 20 19
Remove the minimum node:1. Clear it.2. Move the last node as t3. Top-down min heap adjustment.4. Insert t to the empty node.
5 45
10 8 25 40
15 9 30 20 19
5 45
10 8 25 40
15 19 9 30 20
∞
-
Symmetric Min-Max Heap, SMMH Deletion
26
Remove the maximum node:1. Clear it.2. Move the last node as t3. Top-down max-heap adjustment.4. Insert t to the empty node
5 45
10 8 25 40
15 19 9 30 20
-∞
5 40
10 8 25
15 19 9 30 20
5 40
10 8 25 20
15 19 9 30
-
Symmetric Min-Max Heap, SMMH Deletion
27
5 40
10 8 25 20
15 19 9 30
8 40
10 9 25 20
15 19 30
8 40
10 9 25 20
15 19 30
8 40
10 9 25 30
15 19 20
-
Symmetric Min-Max Heap, SMMH Deletion
28
8 40
10 9 25 30
15 19 20
9 40
10 25 30
15 19 20
9 40
10 20 25 30
15 19
-
SMMH v2.0 Double-Ended Priority Queue, DEPQ
Properties (new version) The root is empty
Let X be any node in SMMH
X’s Left subtree is a min-heap
X’s Right subtree is a max-heap
Left sibling ≤ Right sibling
29
4 80
8 60 6 40
12 20 10 16 14
Let Y be any node in SMMH and Y has grandparent GGL, the left child of G ≤ YGR, the right child of G ≥ Y
30Y
GGL GR
-
SMMH v2.0 Sibling adjustment
30
// Note that the index of root is 0;int AdjSibling(T* smmh, int Y, int MaxSize ){
int s;if (Y is left child){
s = Y + 1;if( s >= MaxSize) return Y;if(smmh[Y] > smmh[s]) {
swap(smmh[Y], smmh[s]);return s;
}}else{ // Y is right child
s = Y – 1;if(smmh[Y] < smmh[s]){
swap(smmh[Y], smmh[s]);return s;
}} return Y;
}
4 80
8 60 6 40
12 20 10 16 45 30
Y s
-
SMMH v2.0 Grandparent adjustment
31
// Note that the index of root is 0;int AdjG(T* smmh, int Y, int MaxSize ){
if (Y smmh[Y]){
swap(smmh[GL], smmh[Y]);return GL;
}else if(smmh[GR] < smmh[Y]) {
swap(smmh[GR], smmh[Y]);return GR;
}return Y;
}
4 80
8 60 6 40
12 20 10 16 14 56
Y
G
GL GR
-
SMMH v2.0 Insertion
1. Expand the size of the complete binary tree by one node. Assume that the id of new node is Y
2. Y = AdjSibling(Y);
3. X = AdjG(Y);
4. If X == Y stop;
5. Else Y X and repeat step 2
Time complexity: O(log n)
32
-
SMMH v2.0 Insertion example
33
4 80
8 60 6 40
12 20 10 16 14 30 2
4 80
8 60 2 40
12 20 10 16 14 30 6
2 80
8 60 4 40
12 20 10 16 14 30 6
-
SMMH v2.0 Insertion example
34
2 80
8 60 4 40
12 20 10 16 14 30 6 50
2 80
8 60 4 50
12 20 10 16 14 30 6 40
-
SMMH v2.0 Insertion example
35
4 80
8 60 6 40
12 20 10 16 14 2
4 80
8 60 6 40
12 20 10 16 2 14
4 80
8 60 2 40
12 20 10 16 6 14
2 80
8 60 4 40
12 20 10 16 6 14
-
SMMH v2.0 Grandchild adjustment
36
// Note that the index of root is 0;int AdjGC(T* smmh, int Y, int MaxSize ){
if (Y is a leaf) return Y;if( Y is a left child){
int CL = Y’s left child, CR = right child of Y’s sibling;int C = CL;if(smmh[CR] < smmh[CL]) C = CR;if(smmh[C] < smmh[Y]){
swap(smmh[C], smmh[Y]);return C;
} }else{ // Y is a rightchild/* exercise */}return Y;
}
4 80
55 60 6 40
12 20 10 16 14 56
Y
G
CL CR
-
SMMH v2.0 Deletion
1. Clear the target node, and move the last node to the target node
2. AdjSibling(Y);
3. X = AdjGC(Y);
4. If X == Y stop;
5. Else Y = X and repeat step 2
Time complexity: O(log n)
37
-
SMMH v2.0 Deletion Example
38
80
8 60 4 50
12 20 10 16 14 30 6 40
40 80
8 60 4 50
12 20 10 16 14 30 6
4 80
8 60 40 50
12 20 10 16 14 30 6
4 80
8 60 6 50
12 20 10 16 14 30 40
-
SMMH v2.0 Deletion Example
39
4
8 60 6 50
12 20 10 16 14 30 40
4 40
8 60 6 50
12 20 10 16 14 30
4 60
8 40 6 50
12 20 10 16 14 30