Question
A complete binary min-heap is made by including each integer in exactly once. The depth of a node in the heap is the length of the path from the root of the heap to that node. Thus, the root is at depth 0. The maximum depth at which integer 9 can appear is ________.
Official answer and solution
- Official answer
- 8
- From
- Official GATE 2016 answer key
Solution
Find the maximum possible depth in a min-heap by maximizing the number of ancestors smaller than the target key.
A complete binary min-heap stores the integers with the root at depth 0.
In a min-heap, the key of every parent is strictly less than the keys of its children. Along any simple path from the root to a node at depth , the values must strictly increase:
If node 9 is located at depth , there must be exactly ancestor nodes on the path from the root to node 9, and every ancestor must have a value strictly less than 9.
The set of available integers in the heap strictly less than 9 is , which contains exactly 8 values. Thus, the path can have at most 8 ancestors:
This places 9 at depth 8.
Since , the tree is completely full up to depth 9 (containing leaves at depth 9 and nodes at depth 8). Therefore, depth 8 is well within the tree bounds and can be realized by placing the remaining 1014 larger elements in other branches.
Thus, the maximum depth at which integer 9 can appear is 8.
8
Confusing the maximum depth of the tree (9) with the maximum depth of key 9, or forgetting that the root is at depth 0.
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2016 Computer Science and Information Technology (CS) Session 2
- Question
- 44 of 65, printed as CS Q.34
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.