-
Notifications
You must be signed in to change notification settings - Fork 0
/
Trie.java
129 lines (102 loc) · 3.34 KB
/
Trie.java
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package by.bsu.mmf.datastructure.trie;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Trie {
private TrieNode root;
public Trie() {
this.root = new TrieNode();
}
public void addValue(String value) {
if (value == null || value.isEmpty()) {
return;
}
TrieNode currNode = root;
currNode.incrementNumOfCompleteWords();
for (int i = 0; i < value.length(); i++) {
char ch = value.charAt(i);
if (!currNode.hasChildNode(ch)) {
currNode.addChildNode(ch);
}
currNode = currNode.getChildNode(ch);
currNode.incrementNumOfCompleteWords();
}
currNode.setCompleteWord(true);
}
public List<String> findValuesByPrefix(String prefix) {
List<String> result = new ArrayList<>();
TrieNode currNode = root;
boolean isPrefixExist = true;
for (int i = 0; i < prefix.length(); i++) {
char ch = prefix.charAt(i);
if (!currNode.hasChildNode(ch)) {
isPrefixExist = false;
break;
}
currNode = currNode.getChildNode(ch);
}
if (isPrefixExist) {
if (currNode.isCompleteWord) {
result.add(prefix);
}
getAllWords(currNode, result, prefix);
}
return result;
}
private void getAllWords(TrieNode currNode, List<String> result, String currWord) {
if (currNode != null) {
Map<Character, TrieNode> childrenNodes = currNode.getChildrenNodes();
for (Map.Entry<Character, TrieNode> entry : childrenNodes.entrySet()) {
String tempWord = currWord + entry.getKey();
if (entry.getValue().isCompleteWord) {
result.add(tempWord);
}
getAllWords(entry.getValue(), result, tempWord);
}
}
}
public int getNumOfWordsByPrefix(String prefix) {
TrieNode currNode = root;
boolean isPrefixExist = true;
for (int i = 0; i < prefix.length(); i++) {
char ch = prefix.charAt(i);
if (!currNode.hasChildNode(ch)) {
isPrefixExist = false;
break;
}
currNode = currNode.getChildNode(ch);
}
int count = isPrefixExist ? currNode.getNumOfCompleteWords() : 0;
return count;
}
}
class TrieNode {
private int numOfCompleteWords = 0;
private Map<Character, TrieNode> childrenNodes = new HashMap<>();
boolean isCompleteWord;
public Map<Character, TrieNode> getChildrenNodes() {
return childrenNodes;
}
public boolean hasChildNode(char ch) {
return childrenNodes.containsKey(ch);
}
public TrieNode getChildNode(char ch) {
return childrenNodes.get(ch);
}
public void addChildNode(char ch) {
childrenNodes.put(ch, new TrieNode());
}
public void setCompleteWord(boolean completeWord) {
isCompleteWord = completeWord;
}
public boolean isCompleteWord() {
return isCompleteWord;
}
public void incrementNumOfCompleteWords() {
numOfCompleteWords++;
}
public int getNumOfCompleteWords() {
return numOfCompleteWords;
}
}