Choose the best description of what the heapify operation does in a binary heap.
Think about how a heap maintains its special order after changes.
The heapify operation adjusts the subtree rooted at a given node to maintain the heap property, ensuring the parent node is ordered correctly relative to its children.
Identify the time complexity of the heapify operation when applied to a subtree with n nodes.
Consider the height of the subtree and how many swaps heapify might perform.
Heapify works by moving down the tree levels, and since the height of a heap is log n, the time complexity is O(log n).
Given the array representing a binary heap: [4, 10, 3, 5, 1], apply heapify at index 1 and select the resulting array.
array = [4, 10, 3, 5, 1] heapify at index 1
Check if the subtree rooted at index 1 already satisfies the heap property.
At index 1, the value is 10 with children 5 and 1. Since 10 is greater than both children, the subtree already satisfies the max-heap property, so the array remains unchanged.
Select the statement that best explains how heapify and build-heap differ.
Think about the scope of each operation.
Heapify adjusts the heap property at a single node, while build-heap runs heapify on all non-leaf nodes from bottom to top to create a valid heap.
Determine the total time complexity of building a heap by applying heapify from the last non-leaf node up to the root in an array of size n.
Consider that heapify takes less time on nodes lower in the tree and more time near the root.
Although heapify takes O(log n) time per node, most nodes are near the leaves and require less work. Summing all heapify calls results in O(n) total time.