Consider the following statements:
- The smallest element in a max-heap is always at a leaf node
- The second largest element in a max-heap is always a child of a root node
- A max-heap can be constructed from a binary search tree in time
- A binary search tree can be constructed from a max-heap in time
Which of the above statements are TRUE?
View worked solution
Answer: A
Let us analyze each statement individually:
- Statement I: The smallest element in a max-heap is always at a leaf node. (TRUE)
In a max-heap, the parent node is always greater than or equal to its children. If the smallest element were an internal node, its children would have to be smaller than or equal to it, which contradicts the assumption that it is the smallest element. Thus, the minimum element must reside in one of the leaf nodes. - Statement II: The second largest element in a max-heap is always a child of a root node. (TRUE)
The root node contains the largest element of the heap. Since any node in the heap is less than or equal to its parent, the second largest element cannot be further down the tree (as it would be smaller than its ancestors). Therefore, it must be one of the direct children of the root (either the left child or the right child). - Statement III: A max-heap can be constructed from a binary search tree in time. (TRUE)
We can perform an in-order (or any) traversal of the BST to copy its elements into an array in time. Then, we can apply the standard bottom-upBuild-Max-Heapalgorithm on this array, which runs in time. Thus, the overall construction takes time. - Statement IV: A binary search tree can be constructed from a max-heap in time. (FALSE)
If we could construct a BST from a max-heap in time, we could sort elements in comparison-based time as follows:- Build a max-heap from unsorted elements in time.
- Convert the max-heap to a BST in time.
- Perform an in-order traversal of the BST in time to get the sorted order.
Since statements I, II, and III are true, the correct option is A.
Free practice · Your answers on this page are not saved.
