-
Notifications
You must be signed in to change notification settings - Fork 85
/
Trie.java
217 lines (199 loc) · 3.94 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import java.util.TreeMap;
/**
* Trie树(前缀树、字典树)
*
* @author ronglexie
* @version 2018/9/1
*/
public class Trie {
private Node root;
private int size;
public Trie() {
root = new Node();
size = 0;
}
public int getSize(){
return size;
}
/**
* 添加一个单词
*
* @param word
* @return void
* @author ronglexie
* @version 2018/9/1
*/
public void add(String word){
Node cur = root;
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if(cur.next.get(c) == null){
cur.next.put(c,new Node());
}
cur = cur.next.get(c);
}
if(!cur.isWord){
cur.isWord = true;
size ++;
}
}
/**
* 递归添加一个单词
*
* @param word
* @return void
* @author ronglexie
* @version 2018/9/1
*/
public void addRecursion(String word){
root = addRecursion(root,word);
}
/**
* 在node节点添加一个单词
*
* @param node
* @param word
* @return Trie.Node
* @author ronglexie
* @version 2018/9/1
*/
private Node addRecursion(Node node, String word){
if(node == null){
return null;
}
if(word.length() > 0){
if(node.next.get(word.charAt(0)) == null) {
node.next.put(word.charAt(0), new Node());
}
node.next.put(word.charAt(0),addRecursion(node.next.get(word.charAt(0)), word.substring(1)));
if(word.length() == 1 && !node.next.get(word.charAt(0)).isWord){
node.next.get(word.charAt(0)).isWord = true;
size ++;
}
}
return node;
}
/**
* 是否包含某个单词
*
* @param word
* @return boolean
* @author ronglexie
* @version 2018/9/1
*/
public boolean contains(String word){
Node cur = root;
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if(cur.next.get(c) == null){
return false;
}
cur = cur.next.get(c);
}
return cur.isWord;
}
/**
* 递归查询是否包含某个单词
*
* @param word
* @return boolean
* @author ronglexie
* @version 2018/9/1
*/
public boolean containsRecurison(String word){
return containsRecurison(root,word);
}
/**
* 递归查询某个节点中是否包含某个单词
*
* @param node
* @param word
* @return boolean
* @author ronglexie
* @version 2018/9/1
*/
private boolean containsRecurison(Node node, String word) {
if(node == null){
return false;
}
if(word.length() > 0){
if(node.next.get(word.charAt(0)) == null) {
return false;
}else if (word.length() == 1 && node.next.get(word.charAt(0)).isWord){
return true;
}
return containsRecurison(node.next.get(word.charAt(0)), word.substring(1));
}
return false;
}
/**
* 是否包含此前缀的单词
*
* @param prefix
* @return boolean
* @author ronglexie
* @version 2018/9/1
*/
private boolean isPrefix(String prefix){
Node cur = root;
for (int i = 0; i < prefix.length(); i++) {
char c = prefix.charAt(i);
if(cur.next.get(c) == null){
return false;
}
cur = cur.next.get(c);
}
return true;
}
/**
* 递归查询是否包含此前缀的单词
*
* @param word
* @return boolean
* @author ronglexie
* @version 2018/9/1
*/
public boolean isPrefixRecurison(String word){
return isPrefixRecurison(root,word);
}
/**
* 递归查询是否包含此前缀的单词
*
* @param node
* @param word
* @return boolean
* @author ronglexie
* @version 2018/9/1
*/
private boolean isPrefixRecurison(Node node, String word) {
if(node == null){
return false;
}
if(word.length() > 0){
if(node.next.get(word.charAt(0)) == null) {
return false;
}else if (word.length() == 1){
return true;
}
return isPrefixRecurison(node.next.get(word.charAt(0)), word.substring(1));
}
return false;
}
/**
* 节点类
*
* @author ronglexie
* @version 2018/9/1
*/
private class Node{
public boolean isWord;
public TreeMap<Character,Node> next;
public Node(boolean isWord) {
this.isWord = isWord;
next = new TreeMap<>();
}
public Node() {
this(false);
}
}
}