سوال ۶۸ کنکور ارشد علوم کامپیوتر ۱۳۹۹
در یک درخت جستجوی باینری، الگوریتم یافتن median دارای چه مرتبه زمانی است؟
پاسخ صحیح: گزینهٔ A —
The median of the keys stored in a binary search tree (BST) is the element whose rank is in sorted order. An in-order traversal of a BST visits the keys in increasing order, so the median is simply the -th key produced by that traversal.
Counting to the middle element. A plain BST stores no rank or subtree-size information at its nodes. To reach the element of rank we must perform an in-order walk and count nodes until we have emitted of them. That stops after visiting about nodes:
In Big- notation constant factors are discarded, so and denote the same growth class — option (1) is the canonical, simplified form.
Why not ? The height of a balanced BST is , but a single root-to-leaf path does not locate the median: there is no way to know in advance which key has rank without counting. To select by rank in you would need an order-statistic tree (each node augmented with the size of its subtree). The question speaks of an ordinary binary search tree, so that augmentation is not available and the in-order count dominates.
| Approach | Extra info stored | Time to find median |
|---|---|---|
| Plain BST + in-order count | none | |
| Order-statistic (size-augmented) BST | subtree sizes |
Hence the time order is linear, .
Official key (دو گزینهای): this question is officially dual-keyed — both option (1) and option (2) are accepted, because . The more defensible choice is option (1), since Big- drops constant factors and is the standard simplified form.
Pitfall: Do not confuse the height of a balanced BST with the cost of selecting by rank. Without subtree-size augmentation, finding the median (or any -th order statistic) requires an in-order count and is .
کارتهای مرتبط
A plain BST has no balance guarantee, so its worst-case height is Θ(n) , making search/min/max O(n) .
A plain BST is not balanced, so its height ranges from Θ(log n) down to n-1 and its leaf count from 1 to ⌈ n/2⌉ .