Heap structure Pasi Frnti 29.9.2014. Tree-based data structure Partial sorting Every node satisfies...
-
Upload
lauren-black -
Category
Documents
-
view
221 -
download
0
description
Transcript of Heap structure Pasi Frnti 29.9.2014. Tree-based data structure Partial sorting Every node satisfies...
![Page 1: Heap structure Pasi Frnti 29.9.2014. Tree-based data structure Partial sorting Every node satisfies heap property: x x.child zy x 87 7 5 43215 9 Max.](https://reader036.fdocuments.us/reader036/viewer/2022062311/5a4d1b687f8b9ab0599b1c6d/html5/thumbnails/1.jpg)
Heap structurePasi Fränti
29.9.2014
![Page 2: Heap structure Pasi Frnti 29.9.2014. Tree-based data structure Partial sorting Every node satisfies heap property: x x.child zy x 87 7 5 43215 9 Max.](https://reader036.fdocuments.us/reader036/viewer/2022062311/5a4d1b687f8b9ab0599b1c6d/html5/thumbnails/2.jpg)
• Tree-based data structure• Partial sorting• Every node satisfies heap
property:x ≥ x.child
zy
x
87
7
5
43 2 15
9
Max
Heap as priority queue
Example:
y ≤ x z ≤ x
HeapHeap
![Page 3: Heap structure Pasi Frnti 29.9.2014. Tree-based data structure Partial sorting Every node satisfies heap property: x x.child zy x 87 7 5 43215 9 Max.](https://reader036.fdocuments.us/reader036/viewer/2022062311/5a4d1b687f8b9ab0599b1c6d/html5/thumbnails/3.jpg)
Heapsort(A[1,N])H CreateHeap();FOR i1 TO N DO
Insert(H, A[i]);FOR iN DOWNTO 1 DO
A[i] RemoveMax();
HeapsortStraightforward implementation
O(1)
O(N logN)
O(N logN)
Requires O(N) extra space for
the heap
7 4 6 1 8 …
1 4 6 7 8 …
Insert
Remove
![Page 4: Heap structure Pasi Frnti 29.9.2014. Tree-based data structure Partial sorting Every node satisfies heap property: x x.child zy x 87 7 5 43215 9 Max.](https://reader036.fdocuments.us/reader036/viewer/2022062311/5a4d1b687f8b9ab0599b1c6d/html5/thumbnails/4.jpg)
HeapsortInplace variant
i
2i 2i+1
1 2 3 4 5 6 7 8 9 …
…
2 34 5 6 7
8 9
1
Indexingin heap:
Indexing in list:
• Use array itself as heap (no extra memory)• Heap is always balanced• Father-son relationships calculated via indexes
Father(i) i = k/2
LeftChild(i) k1 = 2iRightftChild(i) k2 = 2i+1
![Page 5: Heap structure Pasi Frnti 29.9.2014. Tree-based data structure Partial sorting Every node satisfies heap property: x x.child zy x 87 7 5 43215 9 Max.](https://reader036.fdocuments.us/reader036/viewer/2022062311/5a4d1b687f8b9ab0599b1c6d/html5/thumbnails/5.jpg)
HeapsortInplace variant
Heapsort(A[1,N])FOR iN/2 DOWNTO 1 DO
Sink(A, i, N);FOR iN DOWNTO 2 DO
Swap(A[1],A[i]);Sink(A, 1, i-1);
List Heapi
List
Heap
List
Heap i
Insert
Remove
Swap
iLoop
Heap
List
Sinki
1
![Page 6: Heap structure Pasi Frnti 29.9.2014. Tree-based data structure Partial sorting Every node satisfies heap property: x x.child zy x 87 7 5 43215 9 Max.](https://reader036.fdocuments.us/reader036/viewer/2022062311/5a4d1b687f8b9ab0599b1c6d/html5/thumbnails/6.jpg)
Sink function
Sink(A, i, j)IF 2i ≤ j THEN
IF 2i+1 ≤ j THENIF A[2i]>A[2i+1] THEN k2i ELSE k=2i+1;
ELSEk2i;
IF A[i]<A[k] THENSwap(A[i], A[k]);Sink(A, k, j);
Select bigger child
Left child existsRight child exists
Swap if neededSink recursively
![Page 7: Heap structure Pasi Frnti 29.9.2014. Tree-based data structure Partial sorting Every node satisfies heap property: x x.child zy x 87 7 5 43215 9 Max.](https://reader036.fdocuments.us/reader036/viewer/2022062311/5a4d1b687f8b9ab0599b1c6d/html5/thumbnails/7.jpg)
Example of sinkAdded value 1
87
7
5
43 2 15
1Added
17
7
5
43 2 15
8Swap
77
1
5
43 2 15
8
Swap
![Page 8: Heap structure Pasi Frnti 29.9.2014. Tree-based data structure Partial sorting Every node satisfies heap property: x x.child zy x 87 7 5 43215 9 Max.](https://reader036.fdocuments.us/reader036/viewer/2022062311/5a4d1b687f8b9ab0599b1c6d/html5/thumbnails/8.jpg)
n
i
n
i
n
i
inn
inniT
jiTcjiT
1
1 1
loglog
)log(log),(
),2(),(
Heap creation is linear time
iilog
nlogn
Sink(i,n)
• Half of the elements need no work• Next half only one level• Element at location i needs (logn - logi) work
![Page 9: Heap structure Pasi Frnti 29.9.2014. Tree-based data structure Partial sorting Every node satisfies heap property: x x.child zy x 87 7 5 43215 9 Max.](https://reader036.fdocuments.us/reader036/viewer/2022062311/5a4d1b687f8b9ab0599b1c6d/html5/thumbnails/9.jpg)
n
i
inn1
loglog
Heap creation is linear time
1 2
1
3 4 5
2
log N
N
N
i
i1
log
nnnnnnnnnn
dxxnnn
loglogloglog
)(loglog1
Rectangle total
Difference nO
![Page 10: Heap structure Pasi Frnti 29.9.2014. Tree-based data structure Partial sorting Every node satisfies heap property: x x.child zy x 87 7 5 43215 9 Max.](https://reader036.fdocuments.us/reader036/viewer/2022062311/5a4d1b687f8b9ab0599b1c6d/html5/thumbnails/10.jpg)
Empty space for notes
![Page 11: Heap structure Pasi Frnti 29.9.2014. Tree-based data structure Partial sorting Every node satisfies heap property: x x.child zy x 87 7 5 43215 9 Max.](https://reader036.fdocuments.us/reader036/viewer/2022062311/5a4d1b687f8b9ab0599b1c6d/html5/thumbnails/11.jpg)
Empty space for notes
![Page 12: Heap structure Pasi Frnti 29.9.2014. Tree-based data structure Partial sorting Every node satisfies heap property: x x.child zy x 87 7 5 43215 9 Max.](https://reader036.fdocuments.us/reader036/viewer/2022062311/5a4d1b687f8b9ab0599b1c6d/html5/thumbnails/12.jpg)
Another example
100
93 74
92 93 67 65
9122 22 35 43 5 27 17
Just back-up