Download presentation
Presentation is loading. Please wait.
1
CS Data Structure: Heaps
2
Objectives Heaps Priority Queues Heap sort
3
Priority Queues In a number of applications a data structure that can produce the smallest item in a collection is useful. Obviously any sorted list can do the job. However, we may wish to have a data structure with the ability to add items at any time and remove the smallest item at any time. In the interest of efficiency a novel data structure can be utilized to produce good results. But first, let's be clear on exactly what is our target application.
4
007 Or take, for example, a queue of Qs: (That’s Q from the James Bond
movies)
5
008 Perhaps we need to find the smallest in this queue of Qs.
6
Alternatives We might search for the smallest one:
Or we might keep them in order:
7
Priority Queues A better example is a queue of
Perhaps we wish to find the smallest document, and print it first. A better example is a queue of documents waiting to be printed. This is known as the Shortest Job First scheduling algorithm
8
Priority Queues Take for example a printer queue. 90 mins. 20 mins.
The small jobs take less time to print. So it’s a better policy to print them first Take for example a printer queue. 90 mins. 20 mins. 5 secs. 5 mins. Simply printing ‘in order’ can cause bottle necks – maybe we need to pump up throughput!
9
Sound Unlikely? Finding or removing the smallest element is fast
So, we need a data structure that has the following property: Finding or removing the smallest element is fast Now, exactly how might we implement this?
10
Data Structure: Heap Vocabulary Terms: binary heap,
Queuing Use: used to implement a priority queue. Sorting Use: Heapsort uses a heap. Without qualification, the term “heap” refers to binary heap. So what is a heap? A heap is binary tree The binary tree is “completely filled” there are no gaps, except for the bottom level. A picture might help. . .
11
Completeness Incomplete Complete "Complete" is a stronger
B B D E F G D E F G H I J H I J "Complete" is a stronger property than a BST since it does not permit gaps in any rows This missing node makes the tree "incomplete".
12
and Instead of the typical binary search tree relationship between node values (smaller to the left, etc.)... A heap has one simple rule: In a heap, for every node X with parent P, the key value in P is smaller than or equal to X Which forces the minimum value to be at the tree root node P X Y P<X P<Y X?Y
13
Basic Heap Properties Structure Property Order Property
organize the data as a complete binary tree enables operation in log N time Order Property In a heap, for every node X with parent P, the key value in P is smaller than or equal to X Forces the minimum value to be the tree’s root Tree Operations may disturb the structure or the order or both properties must restore those properties
14
Heap: Array Implementation
Heaps are essentially binary trees Their requirements and properties allow us to use a very clever implementation: We implement a binary tree using an Array!!! Consider a typical array: 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15
15
Heap: Array Implementation
Now think of it this way: 00 Ignore this one 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15
16
Heap: Array Implementation
Now think of it this way: 00 Ignore this one 01 The lines are not real! I have drawn them in to help show "tree-like" nature of this arrangement! 02 03 04 05 06 07 08 09 10 11 12 13 14 15
17
Heap: Array Implementation
Interesting math facts regarding indices: Note: The parent of any node i is given by i/2 (integer division) 01 Note: The right child of any node i is given by 2i+1 02 03 Note: The left child of any node i is given by 2i 04 05 06 07 08 09 10 11 12 13 14 15
18
Why not use arrays for all trees?
Advantages of conventional dynamic tree implementation Can always add one more node easily Can move whole chunks of tree by simply manipulating pointers. Important for balanced tree algorithms Doesn't lose efficiency if tree not complete Typical tree usage does not call for “complete” trees! Advantages of array implementation of complete tree Space efficient: A node only holds data, no pointers Quick access to "next" available spot if size is maintained. O(1). Can find a node’s parent without lots of extra pointers and manipulation
19
“Dynamic” behavior of arrays achieved by reallocating
Array Implementation For any element at array position i: left child is at ( 2i ) right child is at ( 2i+1 ) parent is at (int) (i / 2) 1 A 3 2 C B 4 5 6 7 D E F G “Dynamic” behavior of arrays achieved by reallocating larger array blocks 8 9 10 H I J A B D E F G H I J C 1 2 3 4 5 6 7 8 9 10 11 12 13 Can just skip this one
20
Priority Queue: Basic Behavior
Priority Queue’s basic behavior: insert(item) "item" should be a data that does have an order property (like integers, strings (alphabetic ordering), etc) findMin() returns the minimum value without changing the priority queue deleteMin() delete and return the smallest item in the structure isEmpty() makeEmpty()
21
Priority Queue: Implementation
An excellent choice is a heap. The heap was originally developed as part of a sorting algorithm called Heapsort Later, we will show how a heap can be used to perform the Heapsort
22
Insertion size = 26 A correct heap to start with... 2 10 26 18 21 29
27 57 20 47 37 69 69 29 58 A correct heap to start with... 95 85 90 31 94 55 98 71 79 91 86
23
the size, and place the 25 in that slot
Insertion size = 27 2 Increment the size, and place the 25 in that slot 10 26 18 21 29 27 57 20 47 37 69 69 29 58 95 85 90 31 94 55 98 71 79 91 86 25 Insert 25
24
Insertion size = 27 Insert 25 2 10 26 18 21 29 27 57 20 47 37 69 69 29
58 while(i > 1 && A[i] < A[i/2]) { swap(A[i],A[i/2]); i = i/2; } 95 85 90 31 94 55 98 71 79 91 86 25 Insert 25
25
Insertion size = 27 Insert 25 2 10 26 18 21 29 27 57 20 47 37 69 25 29
58 while(i > 1 && A[i] < A[i/2]) { swap(A[i],A[i/2]); i = i/2; } 95 85 90 31 94 55 98 71 79 91 86 69 Insert 25
26
Insertion size = 27 Insert 25 2 10 26 18 21 25 27 57 20 47 37 69 29 29
58 while(i > 1 && A[i] < A[i/2]) { swap(A[i],A[i/2]); i = i/2; } 95 85 90 31 94 55 98 71 79 91 86 69 Insert 25
27
Insertion size = 27 Insert 25 2 10 25 18 21 26 27 57 20 47 37 69 29 29
58 while(i > 1 && A[i] < A[i/2]) { swap(A[i],A[i/2]); i = i/2; } 95 85 90 31 94 55 98 71 79 91 86 69 Insert 25
28
Insertion size = 27 2 10 25 18 21 26 27 57 20 47 37 69 29 29 58 95 85 90 31 94 55 98 71 79 91 86 69
29
Will insert work starting with an empty heap?
Question? Will insert work starting with an empty heap?
30
removeMin We must take the value from the root node and return it to the user. Then we must remove the node. Easy array implementation: Take the last element in the heap and put it in root Then call heapify!
31
heapify Heapify is called on a node. It checks that the heap property is being maintained. If not, it fixes it. 42 27 36 i 2i 2i+1 27 42 36 i 2i 2i+1 If a fix was made, then heapify calls itself recursively on the value that was swapped down i 27 Details such as checking to make sure elements 2i and 2i+1 are in the heap are omitted 2i 2i+1 42 36
32
removeMin size = 27 2 10 25 18 21 26 27 57 20 47 37 69 29 29 58 95 85 90 31 94 55 98 71 79 91 86 69
33
removeMin save returnValue: 2 size = 27 2 10 25 18 21 26 27 57 20 47
37 69 29 29 58 95 85 90 31 94 55 98 71 79 91 86 69
34
removeMin returnValue: 2 size = 27 69 10 25 18 21 26 27 57 20 47 37 69
29 29 58 95 85 90 31 94 55 98 71 79 91 86 69
35
removeMin returnValue: 2 size = 26 69 Decrement the size 10 25 18 21
27 57 20 47 37 69 29 29 58 95 85 90 31 94 55 98 71 79 91 86 69
36
removeMin returnValue: 2 size = 26 69 10 25 18 21 26 27 57 20 47 37 69
29 29 58 95 85 90 31 94 55 98 71 79 91 86
37
removeMin returnValue: 2 size = 26 Heapify 69 10 25 18 21 26 27 57 20
47 37 69 29 29 58 95 85 90 31 94 55 98 71 79 91 86 Heapify
38
removeMin returnValue: 2 size = 26 Heapify 10 69 25 18 21 26 27 57 20
47 37 69 29 29 58 95 85 90 31 94 55 98 71 79 91 86 Heapify
39
removeMin returnValue: 2 size = 26 Heapify 10 69 25 18 21 26 27 57 20
47 37 69 29 29 58 95 85 90 31 94 55 98 71 79 91 86 Heapify
40
removeMin returnValue: 2 size = 26 Heapify 10 18 25 69 21 26 27 57 20
47 37 69 29 29 58 95 85 90 31 94 55 98 71 79 91 86 Heapify
41
removeMin returnValue: 2 size = 26 Heapify 10 18 25 69 21 26 27 57 20
47 37 69 29 29 58 95 85 90 31 94 55 98 71 79 91 86 Heapify
42
removeMin returnValue: 2 size = 26 Heapify 10 18 25 20 21 26 27 57 69
47 37 69 29 29 58 95 85 90 31 94 55 98 71 79 91 86 Heapify
43
removeMin returnValue: 2 size = 26 Heapify 10 18 25 20 21 26 27 57 69
47 37 69 29 29 58 95 85 90 31 94 55 98 71 79 91 86 Heapify
44
removeMin returnValue: 2 size = 26 Heapify 10 18 25 20 21 26 27 57 31
47 37 69 29 29 58 95 85 90 69 94 55 98 71 79 91 86 Heapify
45
removeMin returnValue: 2 size = 26 10 18 25 20 21 26 27 57 31 47 37 69
29 29 58 95 85 90 69 94 55 98 71 79 91 86
46
removeMin size = 26 10 18 25 20 21 26 27 57 31 47 37 69 29 29 58 95 85 90 69 94 55 98 71 79 91 86
47
Details isEmpty makeEmpty
Simply tests size makeEmpty Can just set size to zero. At this point our heap can easily implement a priority queue given that we start with an empty queue and add and remove elements as necessary. If we were given an array of numbers it might be useful to turn the array into a heap "in-place"
48
BuildHeap Given an array of numbers how do we convert it into a heap.
We could iterate through the array and insert each number into heap but this requires us to have space big enough for twice the quantity of numbers we have. We want to convert in place. Note: We will assume that element 0 is not used. Don't confuse this with heapify. BuildHeap will use heapify and is used to create the Heap
49
BuildHeap Assume that we have the number of elements in a variable called size Iterate index from element arraySize/2 down to index 1 heapify(index)
50
BuildHeap size = 26 87 43 68 6 77 33 9 11 19 99 6 23 89 2 14 1 5 27 35 7 42 12 71 3 67 22
51
BuildHeap size = 26 Start at element arraySize/2 87 43 68 6 77 33 9 11
13 11 19 99 6 23 89 2 14 Start at element arraySize/2 1 5 27 35 7 42 12 71 3 67 22
52
BuildHeap size = 26 heapify 87 43 68 6 77 33 9 11 19 99 6 23 89 2 14 1
13 11 19 99 6 23 89 2 14 1 5 27 35 7 42 12 71 3 67 22 heapify
53
BuildHeap size = 26 heapify 87 43 68 6 77 33 9 11 19 99 6 23 22 2 14 1
13 11 19 99 6 23 22 2 14 1 5 27 35 7 42 12 71 3 67 89 heapify
54
BuildHeap size = 26 heapify 87 43 68 6 77 33 9 11 19 99 6 23 22 2 14 1
13 11 19 99 6 23 22 2 14 1 5 27 35 7 42 12 71 3 67 89 heapify
55
BuildHeap size = 26 heapify 87 43 68 6 77 33 9 11 19 99 6 3 22 2 14 1
13 11 19 99 6 3 22 2 14 1 5 27 35 7 42 12 71 23 67 89 heapify
56
BuildHeap size = 26 heapify 87 43 68 6 77 33 9 11 19 99 6 3 22 2 14 1
13 11 19 99 6 3 22 2 14 1 5 27 35 7 42 12 71 23 67 89 heapify
57
BuildHeap size = 26 heapify 87 43 68 6 77 33 9 11 19 99 6 3 22 2 14 1
13 11 19 99 6 3 22 2 14 1 5 27 35 7 42 12 71 23 67 89 heapify
58
BuildHeap size = 26 heapify 87 43 68 6 77 33 9 11 19 7 6 3 22 2 14 1 5
13 11 19 7 6 3 22 2 14 1 5 27 35 99 42 12 71 23 67 89 heapify
59
BuildHeap size = 26 heapify 87 43 68 6 77 33 9 11 19 7 6 3 22 2 14 1 5
13 11 19 7 6 3 22 2 14 1 5 27 35 99 42 12 71 23 67 89 heapify
60
BuildHeap size = 26 heapify 87 43 68 6 77 33 9 11 19 7 6 3 22 2 14 1 5
13 11 19 7 6 3 22 2 14 1 5 27 35 99 42 12 71 23 67 89 heapify
61
BuildHeap size = 26 heapify 87 43 68 6 77 33 9 1 19 7 6 3 22 2 14 11 5
13 1 19 7 6 3 22 2 14 11 5 27 35 99 42 12 71 23 67 89 heapify
62
BuildHeap size = 26 heapify 87 43 68 6 77 33 9 1 19 7 6 3 22 2 14 11 5
13 1 19 7 6 3 22 2 14 11 5 27 35 99 42 12 71 23 67 89 heapify
63
BuildHeap size = 26 heapify 87 43 68 6 77 33 2 1 19 7 6 3 22 9 14 11 5
13 1 19 7 6 3 22 9 14 11 5 27 35 99 42 12 71 23 67 89 heapify
64
BuildHeap size = 26 heapify 87 43 68 6 77 33 2 1 19 7 6 3 22 9 14 11 5
13 1 19 7 6 3 22 9 14 11 5 27 35 99 42 12 71 23 67 89 heapify
65
BuildHeap size = 26 heapify 87 43 68 6 77 3 2 1 19 7 6 33 22 9 14 11 5
13 1 19 7 6 33 22 9 14 11 5 27 35 99 42 12 71 23 67 89 heapify
66
BuildHeap size = 26 heapify 87 43 68 6 77 3 2 1 19 7 6 33 22 9 14 11 5
13 1 19 7 6 33 22 9 14 11 5 27 35 99 42 12 71 23 67 89 heapify
67
BuildHeap size = 26 heapify 87 43 68 6 77 3 2 1 19 7 6 23 22 9 14 11 5
13 1 19 7 6 23 22 9 14 11 5 27 35 99 42 12 71 33 67 89 heapify
68
BuildHeap size = 26 heapify 87 43 68 6 77 3 2 1 19 7 6 23 22 9 14 11 5
13 1 19 7 6 23 22 9 14 11 5 27 35 99 42 12 71 33 67 89 heapify
69
BuildHeap size = 26 heapify 87 43 68 6 6 3 2 1 19 7 77 23 22 9 14 11 5
13 1 19 7 77 23 22 9 14 11 5 27 35 99 42 12 71 33 67 89 heapify
70
BuildHeap size = 26 heapify 87 43 68 6 6 3 2 1 19 7 77 23 22 9 14 11 5
13 1 19 7 77 23 22 9 14 11 5 27 35 99 42 12 71 33 67 89 heapify
71
BuildHeap size = 26 heapify 87 43 68 6 6 3 2 1 19 7 12 23 22 9 14 11 5
13 1 19 7 12 23 22 9 14 11 5 27 35 99 42 77 71 33 67 89 heapify
72
BuildHeap size = 26 heapify 87 43 68 6 6 3 2 1 19 7 12 23 22 9 14 11 5
13 1 19 7 12 23 22 9 14 11 5 27 35 99 42 77 71 33 67 89 heapify
73
BuildHeap size = 26 heapify 87 43 68 1 6 3 2 6 19 7 12 23 22 9 14 11 5
13 6 19 7 12 23 22 9 14 11 5 27 35 99 42 77 71 33 67 89 heapify
74
BuildHeap size = 26 heapify 87 43 68 1 6 3 2 6 19 7 12 23 22 9 14 11 5
13 6 19 7 12 23 22 9 14 11 5 27 35 99 42 77 71 33 67 89 heapify
75
BuildHeap size = 26 heapify 87 43 68 1 6 3 2 5 19 7 12 23 22 9 14 11 6
13 5 19 7 12 23 22 9 14 11 6 27 35 99 42 77 71 33 67 89 heapify
76
BuildHeap size = 26 heapify 87 43 68 1 6 3 2 5 19 7 12 23 22 9 14 11 6
13 5 19 7 12 23 22 9 14 11 6 27 35 99 42 77 71 33 67 89 heapify
77
BuildHeap size = 26 heapify 87 43 2 1 6 3 68 5 19 7 12 23 22 9 14 11 6
13 5 19 7 12 23 22 9 14 11 6 27 35 99 42 77 71 33 67 89 heapify
78
BuildHeap size = 26 heapify 87 43 2 1 6 3 68 5 19 7 12 23 22 9 14 11 6
13 5 19 7 12 23 22 9 14 11 6 27 35 99 42 77 71 33 67 89 heapify
79
BuildHeap size = 26 heapify 87 43 2 1 6 3 9 5 19 7 12 23 22 68 14 11 6
13 5 19 7 12 23 22 68 14 11 6 27 35 99 42 77 71 33 67 89 heapify
80
BuildHeap size = 26 heapify 87 43 2 1 6 3 9 5 19 7 12 23 22 68 14 11 6
13 5 19 7 12 23 22 68 14 11 6 27 35 99 42 77 71 33 67 89 heapify
81
BuildHeap size = 26 heapify 87 1 2 43 6 3 9 5 19 7 12 23 22 68 14 11 6
13 5 19 7 12 23 22 68 14 11 6 27 35 99 42 77 71 33 67 89 heapify
82
BuildHeap size = 26 heapify 87 1 2 43 6 3 9 5 19 7 12 23 22 68 14 11 6
13 5 19 7 12 23 22 68 14 11 6 27 35 99 42 77 71 33 67 89 heapify
83
BuildHeap size = 26 heapify 87 1 2 5 6 3 9 43 19 7 12 23 22 68 14 11 6
13 43 19 7 12 23 22 68 14 11 6 27 35 99 42 77 71 33 67 89 heapify
84
BuildHeap size = 26 heapify 87 1 2 5 6 3 9 43 19 7 12 23 22 68 14 11 6
13 43 19 7 12 23 22 68 14 11 6 27 35 99 42 77 71 33 67 89 heapify
85
BuildHeap size = 26 heapify 87 1 2 5 6 3 9 6 19 7 12 23 22 68 14 11 43
13 6 19 7 12 23 22 68 14 11 43 27 35 99 42 77 71 33 67 89 heapify
86
BuildHeap size = 26 heapify 87 1 2 5 6 3 9 6 19 7 12 23 22 68 14 11 43
13 6 19 7 12 23 22 68 14 11 43 27 35 99 42 77 71 33 67 89 heapify
87
BuildHeap size = 26 heapify 1 87 2 5 6 3 9 6 19 7 12 23 22 68 14 11 43
13 6 19 7 12 23 22 68 14 11 43 27 35 99 42 77 71 33 67 89 heapify
88
BuildHeap size = 26 heapify 1 87 2 5 6 3 9 6 19 7 12 23 22 68 14 11 43
13 6 19 7 12 23 22 68 14 11 43 27 35 99 42 77 71 33 67 89 heapify
89
BuildHeap size = 26 heapify 1 5 2 87 6 3 9 6 19 7 12 23 22 68 14 11 43
13 6 19 7 12 23 22 68 14 11 43 27 35 99 42 77 71 33 67 89 heapify
90
BuildHeap size = 26 heapify 1 5 2 87 6 3 9 6 19 7 12 23 22 68 14 11 43
13 6 19 7 12 23 22 68 14 11 43 27 35 99 42 77 71 33 67 89 heapify
91
BuildHeap size = 26 heapify 1 5 2 6 6 3 9 87 19 7 12 23 22 68 14 11 43
13 87 19 7 12 23 22 68 14 11 43 27 35 99 42 77 71 33 67 89 heapify
92
BuildHeap size = 26 heapify 1 5 2 5 6 3 9 87 19 7 12 23 22 68 14 11 43
13 87 19 7 12 23 22 68 14 11 43 27 35 99 42 77 71 33 67 89 heapify
93
BuildHeap size = 26 heapify 1 5 2 5 6 3 9 11 19 7 12 23 22 68 14 87 43
13 11 19 7 12 23 22 68 14 87 43 27 35 99 42 77 71 33 67 89 heapify
94
BuildHeap size = 26 1 5 2 5 6 3 9 13 11 19 7 12 23 22 68 14 87 43 27 35 99 42 77 71 33 67 89
95
Use the heap for sorting. Heapsort!
What else can we do? Use the heap for sorting. Heapsort!
96
Basic Idea Given an unsorted array we use BuildHeap to convert it into a heap While heap is not empty: removeMin - remember that replaces the top element with the last element of the “heap/array” and then heapifies The heap (logical concept) is one smaller but the array (physical concept) hasn't changed Put the item just removed in the element just after the logical “end” of the heap At conclusion the array is sorted
97
HeapSort after BuildHeap
size = 26 1 5 2 5 6 3 9 11 19 7 12 23 22 68 14 87 43 27 35 99 42 77 71 33 67 89
98
HeapSort size = 26 removeMin 1 5 2 5 6 3 9 11 19 7 12 23 22 68 14 87
43 27 35 99 42 77 71 33 67 89
99
HeapSort size = 26 removeMin 89 5 2 5 6 3 9 11 19 7 12 23 22 68 14 87
43 27 35 99 42 77 71 33 67 89
100
HeapSort size = 25 removeMin = 1 Not in heap now 2 5 3 5 6 22 9 11 19
7 12 23 89 68 14 Not in heap now 87 43 27 35 99 42 77 71 33 67
101
HeapSort size = 25 2 5 3 5 6 22 9 11 19 7 12 23 89 68 14 87 43 27 35 99 42 77 71 33 67 1 heap is 1 element smaller, smallest element is at end of array
102
HeapSort size = 25 Now do it again! 2 5 3 5 6 22 9 11 19 7 12 23 89 68
14 87 43 27 35 99 42 77 71 33 67 1 Now do it again!
103
HeapSort size = 24 3 5 9 5 6 22 14 11 19 7 12 23 89 68 67 87 43 27 35 99 42 77 71 33 2 1
104
HeapSort size = 23 5 5 9 11 6 22 14 33 19 7 12 23 89 68 67 87 43 27 35 99 42 77 71 3 2 1
105
HeapSort size = 22 5 6 9 11 7 22 14 33 19 42 12 23 89 68 67 87 43 27 35 99 71 77 5 3 2 1
106
HeapSort size = 21 6 7 9 11 7 22 14 33 19 42 12 23 89 68 67 87 43 27 35 99 71 5 5 3 2 1
107
HeapSort size = 20 7 7 9 11 12 22 14 33 19 42 71 23 89 68 67 87 43 27 35 99 6 5 5 3 2 1
108
HeapSort size = 19 7 11 9 19 12 22 14 33 27 42 71 23 89 68 67 87 43 99 35 7 6 5 5 3 2 1
109
HeapSort size = 18 9 11 14 19 12 22 35 33 27 42 71 23 89 68 67 87 43 99 7 7 6 5 5 3 2 1
110
HeapSort size = 17 11 12 14 19 42 22 35 33 27 99 71 23 89 68 67 87 43 9 7 7 6 5 5 3 2 1
111
HeapSort size = 16 12 19 14 27 42 22 35 33 43 99 71 23 89 68 67 87 11 9 7 7 6 5 5 3 2 1
112
HeapSort size = 15 14 19 22 27 42 23 35 33 43 99 71 87 89 68 67 12 11 9 7 7 6 5 5 3 2 1
113
HeapSort size = 14 19 27 22 33 42 23 35 67 43 99 71 87 89 68 14 12 11 9 7 7 6 5 5 3 2 1
114
HeapSort size = 13 22 27 23 33 42 68 35 67 43 99 71 87 89 19 14 12 11 9 7 7 6 5 5 3 2 1
115
HeapSort size = 12 23 27 35 33 42 68 89 67 43 99 71 87 22 19 14 12 11 9 7 7 6 5 5 3 2 1
116
HeapSort size = 11 27 33 35 43 42 68 89 67 87 99 71 23 22 19 14 12 11 9 7 7 6 5 5 3 2 1
117
HeapSort size = 10 33 42 35 43 71 68 89 67 87 99 27 23 22 19 14 12 11 9 7 7 6 5 5 3 2 1
118
HeapSort size = 9 35 42 68 43 71 99 89 67 87 33 27 23 22 19 14 12 11 9 7 7 6 5 5 3 2 1
119
HeapSort size = 8 42 43 68 67 71 99 89 87 35 33 27 23 22 19 14 12 11 9 7 7 6 5 5 3 2 1
120
HeapSort size = 7 43 67 68 87 71 99 89 42 35 33 27 23 22 19 14 12 11 9 7 7 6 5 5 3 2 1
121
HeapSort size = 6 67 71 68 87 89 99 43 42 35 33 27 23 22 19 14 12 11 9 7 7 6 5 5 3 2 1
122
HeapSort size = 5 68 71 99 87 89 67 43 42 35 33 27 23 22 19 14 12 11 9 7 7 6 5 5 3 2 1
123
HeapSort size = 4 71 87 99 89 68 67 43 42 35 33 27 23 22 19 14 12 11 9 7 7 6 5 5 3 2 1
124
HeapSort size = 3 87 89 99 71 68 67 43 42 35 33 27 23 22 19 14 12 11 9 7 7 6 5 5 3 2 1
125
HeapSort size = 2 89 99 87 71 68 67 43 42 35 33 27 23 22 19 14 12 11 9 7 7 6 5 5 3 2 1
126
HeapSort size = 1 99 89 87 71 68 67 43 42 35 33 27 23 22 19 14 12 11 9 7 7 6 5 5 3 2 1
127
Summary – you should now …
Heaps Priority Queues Heapsort
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.