We can search the trie in O(n)
time with n
being the length of the word.
Finally, we can implement remove
.

To know whether we should remove something, we should first check if the word is in the trie. So first, we'll need to do a search for it and see if we get a depth
value returned.
1void remove(String val) {
2 int depth = this.search(val);
3 if(depth != 0) {
4 removeHelper(this.head, val, depth);
5 }
6}
7
8TrieNode remove(TrieNode root, String key, int depth) {
9 TrieNode pCrawl = root;
10
11 if (pCrawl == null)
12 return null;
13
14 if (depth == key.length()) {
15 if (pCrawl.isEndOfWord)
16 pCrawl.isEndOfWord = false;
17
18 if (isEmpty(pCrawl))
19 pCrawl = null;
20
21 return pCrawl;
22 }
23}