-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTrie.java
More file actions
40 lines (31 loc) · 1012 Bytes
/
Trie.java
File metadata and controls
40 lines (31 loc) · 1012 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package Implementations;
public class Trie<V> {
//Todo: WIP
private TrieNode<V> root = new TrieNode<>();
public Trie(){
}
public void add(V value[]){
TrieNode<V> currentNode = root;
for(int i = 0; i < value.length; i++) {
if (currentNode.children.contains(value[i]))
currentNode = currentNode.children.get(value[i]);
else {
TrieNode<V> newNode = new TrieNode();
currentNode.children.set(value[i], newNode);
currentNode = newNode;
}
}
}
public String getSuggestions(V value[]){
TrieNode<V> currentNode = root;
for(int i = 0; i < value.length; i++) {
if (currentNode.children.contains(value[i]))
currentNode = currentNode.children.get(value[i]);
else break;
}
return currentNode.children.toString();
}
}
class TrieNode<V>{
HashMap<V, TrieNode> children = new HashMap<>();
}