Data Structures & AlgorithmsVery hardProduct-company placement

Keys 20, 24, 27, 35, 38 are searched with frequencies 4, 1, 3, 6, 11 respectively (only successful searches). The cost of a BST is Σ frequency × (depth + 1), root depth 0. What is the minimum possible cost?

  1. A. 52
  2. B. 62
  3. C. 49
  4. D. 48

Correct answer: C. 49

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic