60 likes | 68 Views
45. Build a heap with 27, 35, 23, 22, 4, 45, 21, 5, 42 and 19. With a series of insertions, here’s the result. 42. 35. 27. 19. 23. 21. 5. 22. 4. 20. Build a heap with 20, 35, 23, 22, 4, 45, 21, 5, 42 and 19. 35. 23. 22. 4. 45. 21. 5. 42. 19.
E N D
45 • Build a heap with 27, 35, 23, 22, 4, 45, 21, 5, 42 and 19. • With a series of insertions, here’s the result. 42 35 27 19 23 21 5 22 4
20 • Build a heap with 20, 35, 23, 22, 4, 45, 21, 5, 42 and 19. 35 23 22 4 45 21 5 42 19 First, we build an arbitrary complete binary tree. Then, starting from the bottom level, we fix the tree such that parent_value > child_value. Swap with the larger child.
20 • Build a heap with 20, 35, 23, 22, 4, 45, 21, 5, 42 and 19. 35 23 42 19 45 21 5 22 4 22 and 42 are swapped. 4 and 19 are swapped.
20 • Build a heap with 20, 35, 23, 22, 4, 45, 21, 5, 42 and 19. 42 45 35 19 23 21 5 22 4 35 and 42 are swapped. 23 and 45 are swapped.
45 • Build a heap with 20, 35, 23, 22, 4, 45, 21, 5, 42 and 19. 42 20 35 19 23 21 5 22 4 45 and 20 are swapped. We need to continue this comparison until we reach the bottom level.
45 • Build a heap with 20, 35, 23, 22, 4, 45, 21, 5, 42 and 19. 42 23 35 19 20 21 5 22 4 20 and 23 are swapped. Now it’s a heap.