When inserting a new element into a min-heap, the bubble up process is used to restore the heap property. Which of the following best describes what happens during bubble up?
Think about how the heap property is restored from the bottom up after insertion.
In a min-heap, after inserting the new element at the end, it is compared with its parent. If it is smaller, they swap places. This repeats until the element is no longer smaller than its parent or it reaches the root.
Given the min-heap represented as an array: [3, 5, 8, 10, 15], what is the array after inserting the element 4 and performing bubble up?
Insert 4 at the end, then swap with parent if smaller.
Insert 4 at the end: [3, 5, 8, 10, 15, 4]. Parent of 4 is 8. Since 4 < 8, swap them: [3, 5, 4, 10, 15, 8]. Now 4's parent is 3. Since 4 > 3, stop. Final heap: [3, 5, 4, 10, 15, 8].
What is the worst-case time complexity of the bubble up operation when inserting an element into a heap of size n?
Consider the height of a heap and how many swaps might be needed.
The heap is a complete binary tree with height about log n. In the worst case, the new element moves from the bottom to the root, requiring O(log n) swaps.
How does the bubble up process differ when inserting into a min-heap versus a max-heap?
Think about the heap property each type maintains.
Min-heaps keep the smallest element at the root, so bubble up swaps when the child is smaller. Max-heaps keep the largest element at the root, so bubble up swaps when the child is larger.
Consider inserting the elements [20, 15, 30, 5, 10] one by one into an empty min-heap using bubble up after each insertion. What is the final heap array representation?
Insert each element and perform bubble up step-by-step.
Insert 20: [20]
Insert 15: [15, 20]
Insert 30: [15, 20, 30]
Insert 5: [5, 15, 30, 20]
Insert 10: [5, 10, 30, 20, 15]