| 16 | |
| 17 | //This function insert the given word into our Trie |
| 18 | public void insert(String word){ |
| 19 | TrieNode curr= root;//get the root and create curr node to perform the operation |
| 20 | |
| 21 | for(int i=0; i<word.length(); i++){ |
| 22 | char ch= word.charAt(i); |
| 23 | |
| 24 | if(curr.children[ch - 'a'] == null){ |
| 25 | curr.children[ch - 'a']= new TrieNode();//if it is empty put a new Trienode in the curr postion |
| 26 | } |
| 27 | |
| 28 | curr= curr.children[ch -'a']; |
| 29 | } |
| 30 | curr.isEnd= true; //marks the end of the current word in our Trie |
| 31 | } |
| 32 | |
| 33 | |
| 34 | //Search function which searches for the word in the Trie. It returns true if the provided word exists in Trie else returns false |