Let's assume that a binary heap is represented using a binary tree such that each node may have a left child node and a right child node. For this type of representation, we can still label the nodes of the tree in the same way as we label the nodes for an array representation. That is, the root node has a label 1. In general, for a node with label i, its left child node will have a label 2i and the right child has a label 2i+1. For any i with 1 <= I <= n , Terry says that the following easy algorithm will walk you from the root node to the node with label i: First find the binary representation P of i. Start with the rightmost bit (least significant bit) of P, walk down from the root as follow: For a 0 bit, walk to the left child, for a 1 bit walk to the right child. At the end, you’ll reach the node with label i. Which of the following is the most appropriate? A. Terry’s algorithm is wrong and not fixable. B. Terry’s algorithm is right. C. Terry's algorithm can be fixed as follows: Walk from leftmost bit (most significant bit) of P. D. Terry's algorithm can be fixed as follows: Ignore the leftmost bit (the most significant bit) of P and Walk from the second leftmost (second most significant) bit of P.
Let's assume that a binary heap is represented using a binary tree such that each node may have a left child node and a right child node. For this type of representation, we can still label the nodes of the tree in the same way as we label the nodes for an array representation. That is, the root node has a label 1. In general, for a node with label i, its left child node will have a label 2i and the right child has a label 2i+1. For any i with 1 <= I <= n , Terry says that the following easy
- First find the binary representation P of i.
- Start with the rightmost bit (least significant bit) of P, walk down from the root as follow:
- For a 0 bit, walk to the left child, for a 1 bit walk to the right child.
- At the end, you’ll reach the node with label i.
Which of the following is the most appropriate?
A. Terry’s algorithm is wrong and not fixable.
B. Terry’s algorithm is right.
C. Terry's algorithm can be fixed as follows: Walk from leftmost bit (most significant bit) of P.
D. Terry's algorithm can be fixed as follows: Ignore the leftmost bit (the most significant bit) of P and Walk from the second leftmost (second most significant) bit of P.
Step by step
Solved in 2 steps