initializing a max heap
DESCRIPTION
1. 3. 2. 4. 5. 6. 7. 11. 8. 9. 7. 7. 8. 10. Initializing A Max Heap. input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]. Initializing A Max Heap. 1. 3. 2. 4. 5. 6. 7. Start at rightmost array position that has a child. 11. 8. 9. 7. 7. 8. 10. Index is n/2. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/1.jpg)
Initializing A Max Heap
input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
8
4
7
6 7
8 9
3
710
1
11
5
2
![Page 2: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/2.jpg)
Initializing A Max Heap
Start at rightmost array position that has a child.
8
4
7
6 7
8 9
3
710
1
11
5
2
Index is n/2.
![Page 3: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/3.jpg)
Initializing A Max Heap
Move to next lower array position.
8
4
7
6 7
8 9
3
710
1
5
11
2
![Page 4: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/4.jpg)
Initializing A Max Heap
8
4
7
6 7
8 9
3
710
1
5
11
2
![Page 5: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/5.jpg)
Initializing A Max Heap
8
9
7
6 7
8 4
3
710
1
5
11
2
![Page 6: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/6.jpg)
Initializing A Max Heap
8
9
7
6 7
8 4
3
710
1
5
11
2
![Page 7: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/7.jpg)
Initializing A Max Heap
8
9
7
6 3
8 4
7
710
1
5
11
2
![Page 8: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/8.jpg)
Initializing A Max Heap
8
9
7
6 3
8 4
7
710
1
5
11
2
![Page 9: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/9.jpg)
Initializing A Max Heap
8
9
7
6 3
8 4
7
710
1
5
11
Find a home for 2.
![Page 10: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/10.jpg)
Initializing A Max Heap
8
9
7
6 3
8 4
7
75
1
11
Find a home for 2.
10
![Page 11: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/11.jpg)
Initializing A Max Heap
8
9
7
6 3
8 4
7
72
1
11
Done, move to next lower array position.
10
5
![Page 12: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/12.jpg)
Initializing A Max Heap
8
9
7
6 3
8 4
7
72
1
11
10
5
Find home for 1.
![Page 13: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/13.jpg)
11
Initializing A Max Heap
8
9
7
6 3
8 4
7
72
10
5
Find home for 1.
![Page 14: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/14.jpg)
Initializing A Max Heap
8
9
7
6 3
8 4
7
72
11
10
5
Find home for 1.
![Page 15: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/15.jpg)
Initializing A Max Heap
8
9
7
6 3
8 4
7
72
11
10
5
Find home for 1.
![Page 16: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/16.jpg)
Initializing A Max Heap
8
9
7
6 3
8 4
7
72
11
10
5
Done.
1
![Page 17: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/17.jpg)
Time Complexity
87
6 3
4
7
710
11
5
2
9
8
1
Height of heap = h.
Number of subtrees with root at level j is <= 2 j-1.
Time for each subtree is O(h-j+1).
![Page 18: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/18.jpg)
ComplexityTime for level j subtrees is <= 2j-1(h-j+1) = t(j).
Total time is t(1) + t(2) + … + t(h-1) =
)log( )O(
)O(2
2)22(
2)22...22(S
2232... )1(2 2 2S
22...)2(2)1(22SLet
1
1122
1221
2210
nhn
h
h
hh
hhh
h
hh
hhh
hh
h
)1(21
1
1
jhh
j
j
![Page 19: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/19.jpg)
Heap Sort
• Heap Sort (program 9.12)– create a max heap– keep extracting the root element from the max
heap, and put the element into the result array accordingly
• time complexity = initialization time + n × deleting element time = O(n)+O(n × log n) = O(n log n)
![Page 20: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/20.jpg)
Heap Sort --example
![Page 21: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/21.jpg)
![Page 22: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/22.jpg)
![Page 23: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/23.jpg)
Comparison of sorting methods
![Page 24: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/24.jpg)
Comparison of sorting methods
![Page 25: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/25.jpg)
Comparison of sorting methods
![Page 26: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/26.jpg)
Leftist Tree
Linked binary tree.
Can do everything a heap can do and in the same asymptotic complexity.
Can meld two leftist tree priority queues in O(log n) time.
![Page 27: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/27.jpg)
Extended Binary Trees
Start with any binary tree and add an external node wherever there is an empty subtree.
Result is an extended binary tree.
![Page 28: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/28.jpg)
A Binary Tree
![Page 29: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/29.jpg)
An Extended Binary Tree
number of external nodes is n+1
![Page 30: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/30.jpg)
The Function s()
For any node x in an extended binary tree, let s(x) be the length of a shortest path from x to an external node in the subtree rooted at x.
![Page 31: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/31.jpg)
s() Values Example
![Page 32: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/32.jpg)
s() Values Example
0 0 0 0
0 0
0 0
0
0
1 1 1
2 1 1
2 1
2
![Page 33: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/33.jpg)
Properties Of s()
If x is an external node, then s(x) = 0.
Otherwise,s(x) = min {s(leftChild(x)),
s(rightChild(x))} + 1
![Page 34: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/34.jpg)
Height Biased Leftist Trees
A binary tree is a (height biased) leftist tree iff for every internal node x, s(leftChild(x)) >= s(rightChild(x))
![Page 35: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/35.jpg)
A Leftist Tree
0 0 0 0
0 0
0 0
0
0
1 1 1
2 1 1
2 1
2
![Page 36: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/36.jpg)
Leftist Trees--Property 1
In a leftist tree, the rightmost path is a shortest root to external node path and the length of this path is s(root).
![Page 37: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/37.jpg)
A Leftist Tree
0 0 0 0
0 0
0 0
0
0
1 1 1
2 1 1
2 1
2
Length of rightmost path is 2.
![Page 38: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/38.jpg)
Leftist Trees—Property 2
• The number of internal nodes is at least2s(root) - 1– (note: because level 1 through level s(root)
have no external nodes.)
• If there are n nodes in the leftist tree, then s(root) log (n+1)– (because 2s(root) – 1 n )
![Page 39: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/39.jpg)
A Leftist Tree
0 0 0 0
0 0
0 0
0
0
1 1 1
2 1 1
2 1
2
Levels 1 and 2 have no external nodes.
![Page 40: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/40.jpg)
Leftist Trees—Property 3
Length of rightmost path is O(log n), where n is the number of nodes in a leftist tree.– Follows from Properties 1 and 2.
![Page 41: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/41.jpg)
Leftist Trees As Priority Queues
Min leftist tree … leftist tree that is a min tree.
Used as a min priority queue.
Max leftist tree … leftist tree that is a max tree.
Used as a max priority queue.
![Page 42: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/42.jpg)
A Min Leftist Tree
8 6 9
6 8 5
4 3
2
![Page 43: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/43.jpg)
Some Min Leftist Tree Operations
put()
remove()
meld()
initialize()
put() and remove() use meld().
![Page 44: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/44.jpg)
Put Operation
put(7)
8 6 9
6 8 5
4 3
2
![Page 45: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/45.jpg)
Meld operation
• The meld strategy is the best described using recursion
• Let A and B be the two min HBLTs that are to be melded
• The root with smaller element becomes the root of the melded HBLT
![Page 46: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/46.jpg)
Meld operation – cont.• Suppose that A has the smaller root and that
its left subtree is L.• Let C be the min HBLT that results from
melding the right subtree of A and the min HBLT B
• The result of melding A and B is the min HBLT has A as its root and L and C as its subtrees.
• If the s values of L is smaller than that of C, the C is the left subtree. Otherwise, L is.
![Page 47: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/47.jpg)
Put Operation
put(7)
8 6 9
6 8 5
4 3
2
Create a single node min leftist tree. 7
![Page 48: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/48.jpg)
Put Operation
put(7)
8 6 9
6 8 5
4 3
2
Create a single node min leftist tree.
Meld the two min leftist trees.
7
![Page 49: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/49.jpg)
Remove Min
8 6 9
6 8 5
4 3
2
![Page 50: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/50.jpg)
Remove Min
8 6 9
6 8 5
4 3
2
Remove the root.
![Page 51: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/51.jpg)
Remove Min
8 6 9
6 8 5
4 3
2
Remove the root.
Meld the two subtrees.
![Page 52: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/52.jpg)
Meld Two Min Leftist Trees
8 6 9
6 8 5
4 3
6
Traverse only the rightmost paths so as to get logarithmic performance.
![Page 53: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/53.jpg)
Meld Two Min Leftist Trees
8 6 9
6 8 5
4 3
6
Meld right subtree of tree with smaller root and all of other tree.
![Page 54: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/54.jpg)
Meld Two Min Leftist Trees
8 6 9
6 8 5
4 3
6
Meld right subtree of tree with smaller root and all of other tree.
![Page 55: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/55.jpg)
Meld Two Min Leftist Trees
8 6
6 8
4 6
Meld right subtree of tree with smaller root and all of other tree.
![Page 56: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/56.jpg)
Meld Two Min Leftist Trees
8 6
Meld right subtree of tree with smaller root and all of other tree.
Right subtree of 6 is empty. So, result of melding right subtree of tree with smaller root and other tree is the other tree.
![Page 57: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/57.jpg)
Meld Two Min Leftist Trees
Swap left and right subtree if s(left) < s(right).
Make melded subtree right subtree of smaller root.
8 6
6
8
6
8
![Page 58: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/58.jpg)
Meld Two Min Leftist Trees
8 6
6 6
4
88 6
6
46
8
Make melded subtree right subtree of smaller root.
Swap left and right subtree if s(left) < s(right).
![Page 59: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/59.jpg)
Meld Two Min Leftist Trees
9
5
3
Swap left and right subtree if s(left) < s(right).
Make melded subtree right subtree of smaller root.
8 6
6 6
4
8
![Page 60: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/60.jpg)
Meld Two Min Leftist Trees
9
5
3
8 6
6 6
4
8
![Page 61: Initializing A Max Heap](https://reader034.vdocuments.net/reader034/viewer/2022051401/56815570550346895dc33c9a/html5/thumbnails/61.jpg)
Initializing In O(n) Time• create n single node min leftist trees
and place them in a FIFO queue
• repeatedly remove two min leftist trees from the FIFO queue, meld them, and put the resulting min leftist tree into the FIFO queue
• the process terminates when only 1 min leftist tree remains in the FIFO queue
• analysis is the same as for heap initialization