Data Structures & AlgorithmsModerateICPC regional · GATE CS top band
Words ['b', 'abbb', 'b', 'ca', 'b', 'bac'] are inserted in this order into an empty trie with one node per character, shared prefixes sharing nodes, and a root. (A repeated word adds no nodes.) How many nodes does the trie have, not counting the root?
- A. 9
- B. 12
- C. 6
- D. 4
Correct answer: A. 9
Every distinct non-empty prefix is exactly one node, so there are 9 nodes. Counting each word's length, or each word, double-counts shared prefixes.
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
- Directed weighted graph on vertices A, B, C, D, E, F with edges listed in this exact order: F→D (3); F→B (3); D→E (1); D→B (2); A→C (7)…
- Tree with vertices 0 to 7, root 0, edges 0–1, 1–2, 0–3, 3–4, 2–5, 5–6, 1–7 (parent–child). Vertex weights (vertex:weight) 0:10, 1:14…
- Distinct keys [10, 21, 12, 26, 19] are inserted, in the order given by the sequence [12, 21, 19, 10, 26], into an empty binary search…
- A chain of 5 matrices M1 to M5 has dimensions given by the sequence p = [20, 17, 5, 11, 19, 20], where Mi has size p[i−1] × p[i]…
- Connected undirected weighted graph on vertices A, B, C, D, E, F with edges A–E (3), A–F (5), B–D (1), B–E (2), C–F (20), D–E (17), D–F…
- Directed weighted graph on vertices A, B, C, D with edges (u→v, w): D→B (3); B→C (4); A→C (6); D→A (2); A→D (2); D→C (6); B→A (8); B→D…