Data Structures & AlgorithmsVery hardGATE CS

Keys 12, 16, 26, 38, 48 are searched with frequencies 10, 1, 8, 8, 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. 77
  2. B. 91
  3. C. 89
  4. D. 76

Correct answer: A. 77

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic