1. TrieNode class #

Created Saturday 07 March 2020

Advantages of tries over hashMaps:

Attributes:

Operations:

If travel is possible for the whole length **and **that the last letter is a terminal, return true. Else false.

Traverse the initial part of the word, as much as possible in the trie. Three cases are possible:

  1. The word exists completely and the last letter is a terminal, do nothing.
  2. The word exists completely but the last letter is not a terminal, set the node’s isTerminal to true.
  3. The word exists partially(or not at all), create nodes for all the letters. For the last letter node, set isTerminal to true.

Two cases are possible:

  1. The word does not exist. Do nothing.
  2. The word exists. Delete the last node if it is a non terminal and a non-parent. Keep doing this for the parents, if they also are a non-terminal and a non-parent of children.

Doubt: How to delete parents? Answer:

  1. Make an array of length the same as the word_length which needs to be deleted.
  2. Traverse the word in the trie and note the parents.
  3. Once done, iterate through the list right to left. Keep deleting if the node is a non-parent and a non-terminal. Once this condition becomes false. Stop.