![]() |
| This Trie has Three words, BUDDHA, SUN and SUPER. |
public classtrieNode { trieNode[] childrenArr; Boolean isEndOfWord; trieNode() { childrenArr = new trieNode[26]; } //methods ... }
Operations:
- String[] autoComplete(String str)
- Boolean contains(String str)
- void insert(String str)
- void delete(String str)
Boolean contains(String str)
eg:
Considering Trie represented in Figure.
contains("sun")
root if (null != childrenArr[indexOf('s')]) True
|
s if (null != childrenArr[indexOf('u')]) True
|
u if (null != childrenArr[indexOf('n')]) True
|
n if (True == isEndOfWord) True
return Truecontains("song")
root if (null != childrenArr[indexOf('s')]) True
|
s if (null != childrenArr[indexOf('o')]) False
return False void insert(String str)
This function add child nodes corresponding to the chars in string only if they are present already, if already present keep traversing and if not present add child node corresponding to chars and keep on adding.
eg: Considering Trie represented in Figure.insert("buddy")
root if (null != childrenArr[indexOf('b')]) True
/ \
b s at b node: if (null != childrenArr[indexOf('u')]) True
| .
u . if (null != childrenArr[indexOf('d')]) True
|
d if (null != childrenArr[indexOf('d')]) True
|
d if (null != childrenArr[indexOf('y')]) False
|\
| \ y child is not present so add it.
h y ---added a y child at indexOf('y') with isEndOfWord True
|
a
insert("bud")
root if (null != childrenArr[indexOf('b')]) True
/ \
b s at b node: if (null != childrenArr[indexOf('u')]) True
| .
u . if (null != childrenArr[indexOf('d')]) True
|
d set isEndOfWord TrueString[] autoComplete(Stringstr)
autoComplete(“bu”) output: [“bud”, “buddy”, “buddha”] autoComplete(“su”) output: [“sun”, “super”]Its simple first reach node representing last char of prefix, and then start traversing
all children recursively in other words traverse all paths till leaf nodes. During traversal
isEndOfWord can be used to find all possible words.
void delete(String str)
root
/ \
b s
| |
u u
| |\
end of word ---d n p
| |
d e
|\ |
h y r
|
a
Lets understand expected behavior of delete().
contains("buddy") True contains("bud") True
delete("bud") This will set isEndOfWord False in in b->u->(d).
contains("bud") False
contains("buddy") True
delete("buddy") This will remove y node and free memory of node.
contains("buddy") False
contains("buddy") False
contains("buddha") True
delete("buddha") It will remove all nodes from b->u->d->d->h->a.
contains("buddha") FalseDelete operation is tricky one and there are Three scenarios:
First Scenario: Deleting a word which is a prefix of other existing longer words.
eg: deletion of bud.
In this case we need to set isEndOfWord to Flase only.
Second Scenario: Deleting a word which shares a part of it with other words and one part is not shared by any other words.
eg: deletion of buddy, “y” is not shared with anyone so node “y” will be deleted.
Third Scenario: Deleting a word which does not share any char with other words in trie.
eg: we have deleted bud and buddy now buddha is not shared with any word.
delete(“buddha”) This will delete all nodes b to a.
Applications of Trie:
- Lexicographical sorting
- Auto Complete feature.
- Spell Check.
- Longest Prefix Matching
- Replacement for Binary Search Tree and Hash Lookup.
code:
Please visit github link given below for full implementation.
https://github.com/abhi1kush/coding/blob/master/Trie_Java/src/Trie.java
c++, c, python, javascript, go, scalla, clojure implementation coming soon.
