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?
- A. 57
- B. 59
- C. 56
- 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).
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
- The weighted undirected graph A–B (1), A–D (6), A–E (4), B–C (6), B–D (4), C–D (1), C–E (4) has repeated weights. How many different…
- A 4-ary max-heap is stored in a 0-indexed array (children of i at 4i + 1 … 4i + 4): [56, 50, 20, 30, 4, 3, 9, 27, 5, 1]. After inserting…
- What does f(4) print? c void f(int n) { if (n <= 0) return; printf("%d", n); f(n - 2); printf("%d", n); f(n - 1); }
- What is the length of the longest BITONIC subsequence (strictly increasing then strictly decreasing; either part may be empty) of [12, 2…
- DFS on the directed graph with adjacency lists A: –; B: ACF; C: FG; D: E; E: BC; F: A; G: A starts at A (scanning lists in the order…
- All suffixes of the string 'radar' are inserted into a character trie (a suffix trie). How many nodes does it have, including the root?