-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrieTree.java
95 lines (90 loc) · 2.85 KB
/
TrieTree.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
package Basics;
public class TrieTree {
public class TrieNode{
public int path;
public int end;
public TrieNode[] map;
public TrieNode(){
path = 0;
end = 0;
map = new TrieNode[26];
}
}
public class Trie{
private TrieNode root;
public Trie(){
root = new TrieNode();
}
public void insert(String word){
if(word == null){
return;
}
char[] chars = word.toCharArray();
TrieNode node = root;
node.path++; //节点伸出的路径
int index = 0;
for (int i = 0;i < chars.length;i++){
index = chars[i] - 'a';
if(node.map[index] == null){ //如果指定的路径另一端为空
node.map[index] = new TrieNode();//就给它创建一个节点
}
node = node.map[index]; //指向下一个节点
node.path++; //节点伸出路径++
}
node.end++; //将末节点的end加一,表示以该节点为结尾的word
}
public boolean search(String word){
if(word == null){
return false;
}
char[] chars = word.toCharArray();
int index = 0;
TrieNode node = root;
for (int i = 0;i < chars.length;i++){
index = chars[i] - 'a';
if (node.map[index] == null){
return false;
}
node = node.map[index];
}
if (node.end != 0){
return true;
}else {
return false;
}
}
public void delete(String word){
if (word == null || search(word) == false){
return;
}
char[] chars = word.toCharArray();
TrieNode node = root;
int index = 0;
for (int i = 0;i < chars.length;i++){
index = chars[i] - 'a';
node.path--;
if (node.path <= 0 ){
node.map = null;
}
node = node.map[index];
}
node.end--;
}
public int prefixNumber(String pre){
if (pre == null){
return 0;
}
char[] chars = pre.toCharArray();
TrieNode node = root;
int index = 0;
for (int i = 0;i < chars.length;i++){
index = chars[i] - 'a';
if (node.map[index] == null){
return 0;
}
node = node.map[index];
}
return node.path;
}
}
}