780 likes | 802 Views
9. 8. 7. 6. 7. 2. 6. 5. 1. A Heap Is Efficiently Represented As An Array. 9. 8. 7. 6. 7. 2. 6. 5. 1. 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 1. 9. 2. 3. 8. 7. 4. 7. 5. 6. 6. 7. 2. 6. 5. 1. 8. 9. Moving Up And Down A Heap. 9. 8. 7. 6. 7. 2. 6. 5. 1.
E N D
9 8 7 6 7 2 6 5 1 A Heap Is Efficiently Represented As An Array 9 8 7 6 7 2 6 5 1 0 1 2 3 4 5 6 7 8 9 10
1 9 2 3 8 7 4 7 5 6 6 7 2 6 5 1 8 9 Moving Up And Down A Heap
9 8 7 6 7 2 6 5 1 7 Inserting An Element Into A Max Heap Complete binary tree with 10 nodes.
9 Inserting An Element Into A Max Heap 8 7 6 7 2 6 New element is 5. 5 1 7 5
9 Inserting An Element Into A Max Heap 8 7 6 2 6 7 New element is 20. 5 1 7 7
9 Inserting An Element Into A Max Heap 8 7 6 2 6 New element is 20. 5 1 7 7 7
9 Inserting An Element Into A Max Heap 7 6 8 2 6 New element is 20. 5 1 7 7 7
20 Inserting An Element Into A Max Heap 9 7 6 8 2 6 New element is 20. 5 1 7 7 7
20 Inserting An Element Into A Max Heap 9 7 6 8 2 6 Complete binary tree with 11 nodes. 5 1 7 7 7
20 Inserting An Element Into A Max Heap 9 7 6 8 2 6 New element is 15. 5 1 7 7 7
20 Inserting An Element Into A Max Heap 9 7 6 2 6 New element is 15. 8 5 1 7 7 8 7
20 Inserting An Element Into A Max Heap 7 15 6 9 2 6 New element is 15. 8 5 1 7 7 8 7
20 7 15 6 9 2 6 8 5 1 7 7 8 7 Complexity of Inserting Complexity is O(log n), where n is heap size.
20 7 15 6 9 2 6 8 5 1 7 7 8 7 Deleting the Max Element Max element is in the root.
Deleting the Max Element 7 15 6 9 2 6 After max element is removed. 8 5 1 7 7 8 7
Deleting the Max Element 7 15 6 9 2 6 Heap with 10 nodes. 8 5 1 7 7 8 7 Reinsert 8 into the heap.
Deleting the Max Element 7 15 6 9 2 6 Reinsert 8 into the heap. 5 1 7 7 7
15 Deleting the Max Element 7 6 9 2 6 Reinsert 8 into the heap. 5 1 7 7 7
15 Deleting the Max Element 9 7 6 2 6 8 Reinsert 8 into the heap. 5 1 7 7 7
15 Deleting the Max Element 9 7 6 2 6 8 Max element is 15. 5 1 7 7 7
Deleting the Max Element 9 7 6 2 6 8 After max element is removed. 5 1 7 7 7
Deleting the Max Element 9 7 6 2 6 8 Heap with 9 nodes. 5 1 7 7 7
Deleting the Max Element 9 7 6 2 6 8 Reinsert 7. 5 1
9 Deleting the Max Element 7 6 2 6 8 Reinsert 7. 5 1
9 Deleting the Max Element 8 7 6 7 2 6 Reinsert 7. 5 1
9 8 7 6 7 2 6 5 1 Complexity of Remove Max Element Complexity is O(log n).
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]
1 Initializing a Max Heap 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.
1 Initializing a Max Heap 3 2 4 11 6 7 Move to next lower array position. 5 8 9 7 7 8 10
1 Initializing a Max Heap 3 2 4 11 6 7 5 8 9 7 7 8 10
1 Initializing a Max Heap 3 2 9 11 6 7 5 8 4 7 7 8 10
1 Initializing a Max Heap 3 2 9 11 6 7 5 8 4 7 7 8 10
1 Initializing a Max Heap 7 2 9 11 6 3 5 8 4 7 7 8 10
1 Initializing a Max Heap 7 2 9 11 6 3 5 8 4 7 7 8 10
1 Initializing a Max Heap 7 11 9 6 3 5 8 4 7 7 8 10 Find a home for 2.
1 Initializing a Max Heap 7 11 9 10 6 3 8 4 8 7 7 5 Find a home for 2.
1 Initializing a Max Heap 7 11 9 10 6 3 8 4 7 7 8 5 2 Done, move to next lower array position.
1 Initializing a Max Heap 7 11 9 10 6 3 2 8 4 7 7 8 5 Find home for 1.
11 Initializing a Max Heap 7 9 10 6 3 2 8 4 7 7 8 5 Find home for 1.
11 Initializing a Max Heap 10 7 9 6 3 2 8 4 7 7 8 5 Find home for 1.
11 Initializing a Max Heap 10 7 9 5 6 3 2 8 4 7 7 8 Find home for 1.
11 Initializing a Max Heap 10 7 9 5 6 3 2 8 4 7 7 8 1 Done.
11 Time Complexity 7 9 8 5 6 3 1 4 7 7 8 2 10 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).
Time for level j subtrees is <= 2j-1(h-j+1) = t(j). Total time is t(1) + t(2) + … + t(h-1) = O(n). Complexity
Leftist Trees 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.
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.
An Extended Binary Tree number of external nodes is n+1
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.