A scheme for storing binary trees in an array is as follows. Indexing of starts at instead of . the root is stored at . For a node stored at , the left child, if any, is stored in and the right child, if any, in . To be able to store any binary tree on n vertices the minimum size of should be
View worked solution
Answer: D
To store any arbitrary binary tree of vertices in the given array representation, we must size the array to accommodate the worst-case tree structure—the one that reaches the maximum possible index.
The maximum index is reached when the binary tree is completely right-skewed (i.e., every node has only a right child):
- The root is at index .
- The right child of the root is at index .
- The right child of that node is at index .
In general, the index of the -th node in a right-skewed path satisfies the recurrence relation:
Solving this recurrence:
Thus, for a right-skewed binary tree with vertices, the last node is stored at index . To ensure we can store any such tree, the minimum size of the array must be at least .
Free practice · Your answers on this page are not saved.
