-2

Can any one help me how to add word in a Trie really new to data structure

/**
 * This method adds a word to the Trie
 * 
 * @param s - word to add to the Trie
 * @param data - Data associated with word s
 */
public void addWord(String s, E data) {

}
CLearner
  • 63
  • 1
  • 2
  • 8

1 Answers1

0

It is a bit unclear what the purpose of data is; do you want to associate some information with each stored string? Basically, adding a new string would mean that you start at the root of the trie, then read s character-wise and traverse the arcs of the trie correspondingly as long as this is possible. If all characters of s are consumed in this process, you are done; if there is some suffix of s left, you have to add new nodes and arcs to accomodate for the suffix. In either case, you arrive at a node where you can store data if this is desired.

Codor
  • 17,447
  • 9
  • 29
  • 56