-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrieTree.java
More file actions
186 lines (168 loc) · 5.39 KB
/
TrieTree.java
File metadata and controls
186 lines (168 loc) · 5.39 KB
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
package class01;
import java.util.HashMap;
/**
* @author pacai
* @version 1.0
*/
@SuppressWarnings("all")
public class TrieTree {
private static class Node {
private int pass;
private int end;
private Node[] next;
public Node() {
pass = 0;
end = 0;
next = new Node[26];
}
}
public static class Trie1 {
private Node root;
public Trie1() {
root = new Node();
}
public void insert(String word) {
if (word == null) {
return;
}
Node cur = root;
cur.pass++;
int path;
for (int i = 0; i < word.length(); i++) { //从左往右遍历
path = word.charAt(i) - 'a';//找到路径
if (cur.next[path] == null) {
cur.next[path] = new Node();
}
cur = cur.next[path];
cur.pass++;
}
cur.end++;
}
//word加入几次
public int search(String word) {
if (word == null) {
return 0;
}
Node cur = root;
int path;
for (int i = 0; i < word.length(); i++) {
path = word.charAt(i) - 'a';
if (cur.next[path] == null) {
return 0;
}
cur = cur.next[path];
}
return cur.end;
}
//所有加入的字符串,有多少以pre作为前缀
public int prefixSearch(String pre) {
if (pre == null) {
return 0;
}
Node cur = root;
int path;
for (int i = 0; i < pre.length(); i++) {
path = pre.charAt(i) - 'a';
if (cur.next[path] == null) {
return 0;
}
cur = cur.next[path];
}
return cur.pass;
}
//删除
public void delete(String word) {
if (word == null || search(word) == 0) { // 确保有加入该字符串才可以执行删除
return;
}
Node cur = root;
cur.pass--;
int path;
for (int i = 0; i < word.length(); i++) {
path = word.charAt(i) - 'a';
if (--cur.next[path].pass == 0) {
cur.next[path] = null;
return;
}
cur = cur.next[path];
}
cur.end--;
}
//使用HashMap来记录字符,则可以动态的控制输入字符的数量
static class TrieTree2 {
private class Node {
private int pass;
private int end;
private HashMap<Integer, Node> next;
}
private Node root;
public TrieTree2() {
this.root = new Node();
}
//添加字符串
public void insert(String word) {
if (word == null) {
return;
}
Node cur = root;
cur.pass++;
for (int i = 0; i < word.length(); i++) {
int path = word.charAt(i) - '0';
if (!cur.next.containsKey(path)) {
cur.next.put(path, new Node());
}
cur = cur.next.get(path);
cur.pass++;
}
cur.end++;
}
//查找字符串
public int search(String word) {
if (word == null) {
return 0;
}
Node cur = root;
for (int i = 0; i < word.length(); i++) {
int path = word.charAt(i) - '0';
if (!cur.next.containsKey(path)) {
return 0;
}
cur = cur.next.get(path);
}
return cur.end;
}
//查询以传入字符串为前缀的数量
public int prefixSearch(String pre) {
if (pre == null) {
return 0;
}
Node cur = root;
for (int i = 0; i < pre.length(); i++) {
int path = pre.charAt(i) - '0';
if (!cur.next.containsKey(path)) {
return 0;
}
cur = cur.next.get(path);
}
return cur.pass;
}
//删除字符串
public void delete(String word) {
if (word == null || search(word) < 1) {
return;
}
Node cur = root;
cur.pass--;
for (int i = 0; i < word.length(); i++) {
int path = word.charAt(i) - '0';
cur = cur.next.get(path);
cur.pass--;
if(cur.pass == 0){
cur.next.remove(path);
}
}
cur.end--;
}
}
}
}