Data Structures & AlgorithmsVery hardGATE CS

Keys 13, 30, 35, 39, 54 are searched with frequencies 8, 1, 9, 9, 12 respectively (only successful searches). The cost of a BST is Σ frequency × (depth + 1), root depth 0. What is the minimum possible cost?

  1. A. 85
  2. B. 89
  3. C. 78
  4. D. 79

Correct answer: D. 79

Optimal BST DP: C(i, j) = Σf(i..j) + min over root r of C(i, r−1) + C(r+1, j), giving 79. Putting the most frequent key at the root of every subtree (greedy) costs 85.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic