520 likes | 703 Views
Details. 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". BuildHeap.
E N D
Details • 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"
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 with heapify. BuildHeap will use heapify
BuildHeap • Assume that we have the number of elements in a variable called size Iterate i from element int(size/2) down to element 1 heapify(i)
BuildHeap size = 26 87 68 43 6 77 33 9 11 19 99 6 23 89 2 14 22 27 35 7 42 12 71 1 5 3 67
BuildHeap size = 26 87 68 43 6 77 33 9 13 11 19 99 6 23 89 2 14 Start at element int(size/2) 22 27 35 7 42 12 71 1 5 3 67
BuildHeap size = 26 87 68 43 6 77 33 9 13 11 19 99 6 23 89 2 14 22 27 35 7 42 12 71 1 5 3 67 heapify
BuildHeap size = 26 87 68 43 6 77 33 9 13 11 19 99 6 23 22 2 14 89 27 35 7 42 12 71 1 5 3 67 heapify
BuildHeap size = 26 87 68 43 6 77 33 9 13 11 19 99 6 23 22 2 14 89 27 35 7 42 12 71 1 5 3 67 heapify
BuildHeap size = 26 87 68 43 6 77 33 9 13 11 19 99 6 3 22 2 14 89 27 35 7 42 12 71 1 5 23 67 heapify
BuildHeap size = 26 87 68 43 6 77 33 9 13 11 19 99 6 3 22 2 14 89 27 35 7 42 12 71 1 5 23 67 heapify
BuildHeap size = 26 87 68 43 6 77 33 9 13 11 19 99 6 3 22 2 14 89 27 35 7 42 12 71 1 5 23 67 heapify
BuildHeap size = 26 87 68 43 6 77 33 9 13 11 19 7 6 3 22 2 14 89 27 35 99 42 12 71 1 5 23 67 heapify
BuildHeap size = 26 87 68 43 6 77 33 9 13 11 19 7 6 3 22 2 14 89 27 35 99 42 12 71 1 5 23 67 heapify
BuildHeap size = 26 87 68 43 6 77 33 9 13 11 19 7 6 3 22 2 14 89 27 35 99 42 12 71 1 5 23 67 heapify
BuildHeap size = 26 87 68 43 6 77 33 9 13 1 19 7 6 3 22 2 14 89 27 35 99 42 12 71 11 5 23 67 heapify
BuildHeap size = 26 87 68 43 6 77 33 9 13 1 19 7 6 3 22 2 14 89 27 35 99 42 12 71 11 5 23 67 heapify
BuildHeap size = 26 87 68 43 6 77 33 2 13 1 19 7 6 3 22 9 14 89 27 35 99 42 12 71 11 5 23 67 heapify
BuildHeap size = 26 87 68 43 6 77 33 2 13 1 19 7 6 3 22 9 14 89 27 35 99 42 12 71 11 5 23 67 heapify
BuildHeap size = 26 87 68 43 6 77 3 2 13 1 19 7 6 33 22 9 14 89 27 35 99 42 12 71 11 5 23 67 heapify
BuildHeap size = 26 87 68 43 6 77 3 2 13 1 19 7 6 33 22 9 14 89 27 35 99 42 12 71 11 5 23 67 heapify
BuildHeap size = 26 87 68 43 6 77 3 2 13 1 19 7 6 23 22 9 14 89 27 35 99 42 12 71 11 5 33 67 heapify
BuildHeap size = 26 87 68 43 6 77 3 2 13 1 19 7 6 23 22 9 14 89 27 35 99 42 12 71 11 5 33 67 heapify
BuildHeap size = 26 87 68 43 6 6 3 2 13 1 19 7 77 23 22 9 14 89 27 35 99 42 12 71 11 5 33 67 heapify
BuildHeap size = 26 87 68 43 6 6 3 2 13 1 19 7 77 23 22 9 14 89 27 35 99 42 12 71 11 5 33 67 heapify
BuildHeap size = 26 87 68 43 6 6 3 2 13 1 19 7 12 23 22 9 14 89 27 35 99 42 77 71 11 5 33 67 heapify
BuildHeap size = 26 87 68 43 6 6 3 2 13 1 19 7 12 23 22 9 14 89 27 35 99 42 77 71 11 5 33 67 heapify
BuildHeap size = 26 87 68 43 1 6 3 2 13 6 19 7 12 23 22 9 14 89 27 35 99 42 77 71 11 5 33 67 heapify
BuildHeap size = 26 87 68 43 1 6 3 2 13 6 19 7 12 23 22 9 14 89 27 35 99 42 77 71 11 5 33 67 heapify
BuildHeap size = 26 87 68 43 1 6 3 2 13 5 19 7 12 23 22 9 14 89 27 35 99 42 77 71 11 6 33 67 heapify
BuildHeap size = 26 87 68 43 1 6 3 2 13 5 19 7 12 23 22 9 14 89 27 35 99 42 77 71 11 6 33 67 heapify
BuildHeap size = 26 87 2 43 1 6 3 68 13 5 19 7 12 23 22 9 14 89 27 35 99 42 77 71 11 6 33 67 heapify
BuildHeap size = 26 87 2 43 1 6 3 68 13 5 19 7 12 23 22 9 14 89 27 35 99 42 77 71 11 6 33 67 heapify
BuildHeap size = 26 87 2 43 1 6 3 9 13 5 19 7 12 23 22 68 14 89 27 35 99 42 77 71 11 6 33 67 heapify
BuildHeap size = 26 87 2 43 1 6 3 9 13 5 19 7 12 23 22 68 14 89 27 35 99 42 77 71 11 6 33 67 heapify
BuildHeap size = 26 87 2 1 43 6 3 9 13 5 19 7 12 23 22 68 14 89 27 35 99 42 77 71 11 6 33 67 heapify
BuildHeap size = 26 87 2 1 43 6 3 9 13 5 19 7 12 23 22 68 14 89 27 35 99 42 77 71 11 6 33 67 heapify
BuildHeap size = 26 87 2 1 5 6 3 9 13 43 19 7 12 23 22 68 14 89 27 35 99 42 77 71 11 6 33 67 heapify
BuildHeap size = 26 87 2 1 5 6 3 9 13 43 19 7 12 23 22 68 14 89 27 35 99 42 77 71 11 6 33 67 heapify
BuildHeap size = 26 87 2 1 5 6 3 9 13 6 19 7 12 23 22 68 14 89 27 35 99 42 77 71 11 43 33 67 heapify
BuildHeap size = 26 87 2 1 5 6 3 9 13 6 19 7 12 23 22 68 14 89 27 35 99 42 77 71 11 43 33 67 heapify
BuildHeap size = 26 1 2 87 5 6 3 9 13 6 19 7 12 23 22 68 14 89 27 35 99 42 77 71 11 43 33 67 heapify
BuildHeap size = 26 1 2 87 5 6 3 9 13 6 19 7 12 23 22 68 14 89 27 35 99 42 77 71 11 43 33 67 heapify
BuildHeap size = 26 1 2 5 87 6 3 9 13 6 19 7 12 23 22 68 14 89 27 35 99 42 77 71 11 43 33 67 heapify
BuildHeap size = 26 1 2 5 87 6 3 9 13 6 19 7 12 23 22 68 14 89 27 35 99 42 77 71 11 43 33 67 heapify
BuildHeap size = 26 1 2 5 6 6 3 9 13 87 19 7 12 23 22 68 14 89 27 35 99 42 77 71 11 43 33 67 heapify
BuildHeap size = 26 1 2 5 5 6 3 9 13 87 19 7 12 23 22 68 14 89 27 35 99 42 77 71 11 43 33 67 heapify
BuildHeap size = 26 1 2 5 5 6 3 9 13 11 19 7 12 23 22 68 14 89 27 35 99 42 77 71 87 43 33 67 heapify
BuildHeap size = 26 1 2 5 5 6 3 9 13 11 19 7 12 23 22 68 14 89 27 35 99 42 77 71 87 43 33 67 Questions?