GATE / CS ARCHIVE

GATE CSE 2004 / QUESTION 85

Total smaller-subtree cost in a balanced tree

Algorithms · Asymptotic Analysis & Recurrences · Full GATE CSE 2004 paper

GATE 2004, Q85 2 marks · MCQ
Report issue

Asymptotic Analysis & Recurrences

Question 85, GATE 2004: A program visits every node x of a balanced binary search tree with n leaves, computing g(x). The cost at x is min(number of leaves in x’s left subtree,number in x’s right subtree). What is worst-case total time?
Options:
(A) Θ(n)
(B) Θ(n log n)
(C) Θ(n²)
(D) Θ(n² log n)
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

1. In a perfect balanced tree with n leaves, the root costs n/2, the two children together cost n/2, and every internal level similarly costs n/2. There are log2 n internal levels, so this supplies a Θ(n log n) worst-case lower bound.

2. For any balanced tree, at each depth the subtrees are disjoint and the sum of their leaf counts is at most n; summing min(left leaves,right leaves) at that depth is at most n/2. Height O(log n) gives O(n log n), with traversal overhead O(n).

3. Thus worst-case Θ(n log n), B, assuming the usual balanced binary-tree model.

4. Teaching check: For n=8 in a perfect tree, the root cost is 4, the next level totals 4 and the next totals 4, giving 12 before traversal bookkeeping. Each internal level contributes n/2, and there are log2 n such levels. Counting only the root misses the repeated per-level work.

Original paper, page 12