-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTrie.java
More file actions
154 lines (131 loc) · 3.93 KB
/
Trie.java
File metadata and controls
154 lines (131 loc) · 3.93 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
/*
* Trie data structure for string symbol table
*/
import java.util.NoSuchElementException;
public class Trie<Value> {
private static final int R = 26; // Extended ASCII char set
private Node root; // root of trie
private int n; // number of keys in trie
// R-way Node object
private static class Node {
private Object value;
Node[] next;
public Node() {
next = new Node[R];
value = null;
}
}
public Trie() {
root = null;
n = 0;
}
public Value search(String key) {
if (key == null) {
throw new IllegalArgumentException();
}
if (root == null) {
throw new NoSuchElementException();
}
Node temp = root;
for (int i = 0; i < key.length(); ++i) {
int index = key.charAt(i) - 'a';
if (temp.next[index] == null) {
return null;
}
temp = temp.next[index];
}
return (Value)temp.value;
}
public boolean contains(String key) {
return search(key) != null;
}
public void insert(String key, Value value) {
if (key == null || value == null) {
throw new IllegalArgumentException();
}
if (root == null) {
root = new Node();
}
Node temp = root;
int length = key.length();
for (int i = 0; i < length; ++i) {
int index = key.charAt(i) - 'a';
if (temp.next[index] == null) {
temp.next[index] = new Node();
}
temp = temp.next[index];
}
temp.value = value;
n++;
}
private boolean empty(Node node) {
for (int i = 0; i < R; ++i) {
if (node.next[i] != null) {
return false;
}
}
return true;
}
private Node remove(Node node, String key, int depth) {
if (node == null) {
return null;
}
if (depth == key.length()) {
if (node.value != null) {
n--;
node.value = null;
}
} else {
int index = key.charAt(depth) - 'a';
node.next[index] = remove(node.next[index], key, depth + 1);
}
if (node.value != null || !empty(node)) {
return node;
}
return null;
}
public void remove(String key) {
if (key == null) {
throw new IllegalArgumentException();
}
if (root == null) {
throw new NoSuchElementException();
}
root = remove(root, key, 0);
}
public int size() {
return n;
}
public boolean isEmpty() {
return size() == 0;
}
public static void main(String[] args) {
String[] keys = {"the", "a", "there", "answer", "any",
"by", "bye", "their", "z"};
Trie<Integer> trie = new Trie<>();
for (int i = 0; i < keys.length; ++i) {
trie.insert(keys[i], i);
}
System.out.println("== Trie size : " + trie.size());
for (int i = 0; i < keys.length; ++i) {
if (trie.contains(keys[i])) {
System.out.println(keys[i] + ": " + trie.search(keys[i]));
} else {
System.out.println(keys[i] + ": " + null);
}
}
String[] remove = {"z", "the", "their"};
for (int i = 0; i < remove.length; ++i) {
System.out.println("Deleting " + remove[i]);
trie.remove(remove[i]);
}
System.out.println("== Trie size : " + trie.size());
for (int i = 0; i < keys.length; ++i) {
if (trie.contains(keys[i])) {
System.out.println(keys[i] + ": " + trie.search(keys[i]));
} else {
System.out.println(keys[i] + ": " + null);
}
}
}
}