Data Structures & AlgorithmsModerateWipro · Capgemini pattern

The words bat, bath, bad, ban, band are inserted into an empty trie (one node per character, plus the root). How many nodes does the trie have, including the root?

  1. A. 8
  2. B. 7
  3. C. 17
  4. D. 9

Correct answer: A. 8

Root → b → a is shared by all five words (2 nodes). Under 'a' hang t, d and n (3 nodes). 'h' extends bat to bath and a second 'd' extends ban to band (2 nodes). 2 + 3 + 2 = 7 character nodes + the root = 8. '7' forgets the root; '17' is the total number of characters with no prefix sharing.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic