99.5k views
0 votes
Build the heap that would result from inserting the following elements in the order in which they are presented:

44,1,11,7,2,3,15,22,10,21,8.
Show the intermediate heaps resulting from each individual insertion

User Ethan G
by
8.1k points

1 Answer

0 votes

Final answer:

To build the heap, each value is inserted and then a heapify procedure is called when needed, ensuring the max-heap property is maintained. The intermediate heaps are shown after each insertion.

Step-by-step explanation:

To build a heap from the given sequence of insertions: 44, 1, 11, 7, 2, 3, 15, 22, 10, 21, 8, we will perform the insertions one by one and adjust the heap accordingly. The type of heap is not specified, so I will assume a max-heap for this example.

  1. Insert 44: [44]
  2. Insert 1: [44, 1]
  3. Insert 11: [44, 1, 11]
  4. Insert 7: Heapify after insertion: [44, 7, 11, 1]
  5. Insert 2: [44, 7, 11, 1, 2]
  6. Insert 3: [44, 7, 11, 1, 2, 3]
  7. Insert 15: Heapify after insertion: [44, 7, 15, 1, 2, 3, 11]
  8. Insert 22: Heapify after insertion: [44, 22, 15, 7, 2, 3, 11, 1]
  9. Insert 10: [44, 22, 15, 7, 10, 3, 11, 1, 2]
  10. Insert 21: Heapify after insertion: [44, 22, 21, 7, 10, 15, 11, 1, 2, 3]
  11. Insert 8: Heapify after insertion: [44, 22, 21, 7, 10, 15, 11, 1, 2, 3, 8]

After each insertion, a heapify procedure is called if necessary to maintain the heap property, for which the parent node's value must be greater than or equal to the values of its children for a max-heap.

User Tsutomu
by
8.1k points