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?
- A. 52
- B. 62
- C. 49
- 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.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- DFS on the directed graph with adjacency lists A: CE; B: EFG; C: B; D: AG; E: C; F: G; G: – starts at A (scanning lists in the order…
- What is the minimum number of edges that must be deleted from the undirected graph with edges A–B, A–D, A–E, B–D, B–F, C–D, C–E, D–E, E–F…
- An undirected connected graph has edges A–C, B–D, B–F, B–H, C–D, C–F, D–G, D–I, E–H, F–G. How many articulation points (cut vertices) and…
- Coins of denominations 1, 9, 10, 11 are available in unlimited supply. What is the minimum number of coins needed to make 18, and how many…
- A BST holds integer keys between 1 and 100. Which sequence CANNOT be the sequence of keys examined while searching for 46?
- Dijkstra's algorithm runs from A on the directed graph A→B (6), A→E (1), A→F (11), B→C (9), B→D (2), B→F (1), C→D (10), C→F (11), D→A (5)…