Data Structures & AlgorithmsVery hardGATE CS

Keys 28, 36, 45, 51, 56 are searched with frequencies 2, 3, 8, 7, 8 respectively (only successful searches). The cost of a BST is Σ frequency × (depth + 1), root depth 0. What is the minimum possible cost?

  1. A. 57
  2. B. 59
  3. C. 56
  4. D. 28

Correct answer: C. 56

Optimal BST DP: C(i, j) = Σf(i..j) + min over root r of C(i, r−1) + C(r+1, j) gives 56. Putting the most frequent key at the root of every subtree costs 57, a height-balanced tree rooted at 45 costs 59, and 28 is just Σf (everything at depth 0).

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic