-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrie.java
127 lines (110 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
package com.ds_algo.o_trie;
import com.ds_algo.k_hashTable.hashMap.HashMap;
public class Trie<V> {
private int size; // 单词的数量
private Node<V> root;
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
public void clear() {
size = 0;
root = null;
}
public V get(String key) {
Node<V> node = node(key);
return node == null ? null : node.value;
}
public boolean contains(String key) {
Node<V> node = node(key);
return node == null ? false : node.word;
}
public V add(String key, V value) {
keyCheck(key);
// 创建根节点
if (root == null){
root = new Node<>(null);
}
Node<V> node = root;
char[] chars = key.toCharArray();
for (char c : chars) {
boolean emptyChildren = node.children == null;
Node<V> childNode = emptyChildren ? null : node.children.get(c);
if (childNode == null){
childNode = new Node<>(node);
childNode.character = c;
if (emptyChildren){
node.children = new HashMap<>();
}
node.children.put(c,childNode);
}
node = childNode;
}
if (node.word){
// 已经存在��个单词
V oldValue = node.value;
node.value = value;
return oldValue;
}
// 新增一个单词
node.word = true;
node.value = value;
size++;
return null;
}
public V remove(String key) {
keyCheck(key);
// 找到最后一个节点
Node<V> node = node(key);
// 如果不是单词结尾,不用作任何处理
if (node == null || !node.word) return null;
size--;
V oldValue = node.value;
// 如果还有子节点
if (node.children != null && !node.children.isEmpty()){
node.word = false;
node.value = null;
return oldValue;
}
// 如果没有子节点
Node<V> parent = null;
while (node.parent != null){
parent = node.parent;
parent.children.remove(node.character);
if (parent.word || !parent.children.isEmpty()) break;
node = parent;
}
return oldValue;
}
public boolean startsWith(String prefix) {
Node<V> node = node(prefix);
return node != null;
}
private Node<V> node(String key) {
keyCheck(key);
Node<V> node = root;
char[] chars = key.toCharArray();
for (char c : chars) {
if (node == null || node.children == null || node.children.isEmpty()) return null;
node = node.children.get(c);
}
return node;
}
private void keyCheck(String key) {
if (key == null || key.length() == 0) {
throw new IllegalArgumentException("key must not be empty");
}
}
private static class Node<V> {
Character character;
boolean word; // 是否为单词的结尾(是否为一个完整的单词)
HashMap<Character,Node<V>> children;
Node<V> parent;
V value;
public Node(Node<V> parent) {
this.parent = parent;
}
}
}