public void add ( String input ) { TrieNode currentNode = root ; for ( char c : input . toCharArray ( ) ) { Map < Character , TrieNode > children = currentNode . getChildren ( ) ; TrieNode matchingNode = children . get ( c ) ; if ( matchingNode != null ) { currentNode = matchingNode ; } else { TrieNode newNode = new TrieNode ( ) ; children . put ( c , newNode ) ; currentNode = newNode ; } } currentNode . end = true ; // this is the end of an input that was added, there may be more children }
Add a string to the trie .