# Details

## Details

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. 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"

2. 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

3. 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)

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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

11. 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

12. 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

13. 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

14. 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

15. 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

16. 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

17. 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

18. 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

19. 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

20. 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

21. 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

22. 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

23. 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

24. 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

25. 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

26. 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

27. 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

28. 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

29. 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

30. 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

31. 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

32. 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

33. 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

34. 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

35. 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

36. 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

37. 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

38. 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

39. 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

40. 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

41. 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

42. 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

43. 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

44. 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

45. 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

46. 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

47. 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

48. 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?