int[] heap = new int[10]; int size = 0; // Insert 10 heap[size++] = 10; // Insert 4 heap[size++] = 4; // Insert 15 heap[size++] = 15; // Insert 20 heap[size++] = 20; // Insert 0 heap[size++] = 0; // After each insertion, heapify up to maintain min-heap property // (Assume heapifyUp is correctly implemented) // What is the heap array after all insertions?
In a min-heap, the smallest element is at the root (index 0). After inserting 10, 4, 15, 20, and 0, the 0 bubbles up to the root. The final array is [0, 4, 15, 20, 10].
A max-heap requires that every parent node is greater than or equal to its children, ensuring the largest element is at the root.
Accessing an index outside the array bounds throws ArrayIndexOutOfBoundsException in Java.
Height = floor(log2(31)) = 4. This is the number of edges from root to the deepest leaf in a complete binary tree with 31 nodes.
int[] heap = {2, 5, 3, 10, 7, 8}; int size = 6; // Remove root (2), replace with last element (8) heap[0] = heap[size - 1]; size--; // Heapify down from root to maintain min-heap // (Assume heapifyDown is correctly implemented) // What is the heap array after removal and heapify?
After removing 2, replace root with 8: [8, 5, 3, 10, 7, _]. Heapify down swaps 8 with smaller child 3: [3, 5, 8, 10, 7]. Position 2 (now 8) has no children (indices 5 and 6 exceed size 5). Final array: [3, 5, 8, 10, 7].
