GATE Computer ScienceHardGATE CS

Ten words are inserted into an initially empty trie (one node per character, plus a root): cat, car, cart, care, dog, dot, do, carbon, cattle, cab. How many nodes does the trie have, counting the root?

  1. A. 20
  2. B. 24
  3. C. 18
  4. D. 17

Correct answer: C. 18

Count the distinct non-empty prefixes: c, ca, cat, car, cab, cart, care, carb, carbo, carbon, catt, cattl, cattle, d, do, dog, dot, which is 17, plus the root gives 18. Each shared prefix is stored only once.

More Algorithms questions

Exams that ask this topic