forked from muddassirzfr/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrie.java
More file actions
209 lines (172 loc) · 5.38 KB
/
Trie.java
File metadata and controls
209 lines (172 loc) · 5.38 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
/**
* Implements a trie. We store the input list of words in tries so
* that we can efficiently find words with a given prefix.
*/
public class Trie {
// Trie Node
public static class Node {
//the character is the value of node
public char character;
public ArrayList<Node> children;
// end of a word
public boolean terminated = false;
public Node() {
children = new ArrayList<Node>();
}
public Node(char c) {
this();
character = c;
}
public boolean addWord(String w) {
if (w == null || w.length() == 0) {
return false;
}
Node child;
char firstChar = w.charAt(0);
Node tmp = getChild(firstChar);
if (tmp == null) {
child = new Node(firstChar);
children.add(child);
} else {
child = tmp;
}
if (w.length() > 1) {
child.addWord(w.substring(1));
} else {
child.terminated = true;
}
return true;
}
public Node getChild(char c) {
for (Node n : children) {
if (n.character == c)
return n;
}
return null;
}
public Node getLeafNode(char c) {
for (Node n : children) {
if (n.children.size() != 0) {
n.getLeafNode(c);
} else {
if (n.terminated) {
return n;
} else {
return null;
}
}
}
return null;
}
public Node getParentLeafNode(char c) {
for (Node n : children) {
if (n.children.size() != 0) {
n.getLeafNode(c);
} else {
if (n.terminated) {
return this;
} else {
return null;
}
}
}
return null;
}
public boolean removeChild(Node child) {
if (children.contains(child)) {
children.remove(child);
if (children.size() == 0) {
this.terminated = true;
}
return true;
} else {
return false;
}
}
public boolean removeChild(char c) {
Node child = new Node(c);
return removeChild(child);
}
public void getAllWords(StringBuilder path, ArrayList<String> words) {
for (Node c : children) {
StringBuilder word = new StringBuilder(path);
word.append(c.character);
if (c.terminated) {
words.add(word.toString());
} else {
c.getAllWords(word, words);
}
}
}
} // end of TrieNode class
private Node root;
public Trie(ArrayList<String> words) {
root = new Node();
for (String word : words) {
root.addWord(word);
}
}
public Trie(String[] words) {
root = new Node();
for (String word : words) {
root.addWord(word);
}
}
public boolean contains(String word, boolean exactMatch) {
Node current = root;
for (int i = 0; i < word.length(); i++) {
current = current.getChild(word.charAt(i));
if (current == null)
return false;
}
return !exactMatch || current.terminated;
}
public boolean removeWord(String w) {
if (contains(w, true)) {
for (int i = w.length() - 1; i >= 0; i--) {
char c = w.charAt(i);
Node parent = root.getParentLeafNode(c);
parent.removeChild(c);
if (parent.children.size() != 0) {
break;
}
}
} else {
return false;
}
return true;
}
public ArrayList<String> getAllWords() {
ArrayList<String> words = new ArrayList<String>();
StringBuilder word = new StringBuilder();
root.getAllWords(word, words);
return words;
}
public void printTrie() {
Queue<Node> queue = new LinkedList<Node>(root.children);
Queue<Node> nextQueue = new LinkedList<Node>();
while (!queue.isEmpty()) {
Node n = queue.poll();
nextQueue.addAll(n.children);
System.out.print(n.character + " ");
if (queue.isEmpty()) {
queue.addAll(nextQueue);
nextQueue.clear();
System.out.println();
}
}
}
public static void main(String[] args) {
String[] words = {"apple", "arm", "grape", "apc"};
Trie root = new Trie(words);
ArrayList<String> list = root.getAllWords();
root.printTrie();
System.out.println(list.toString());
System.out.println(root.contains("ppl", false));
System.out.println(root.contains("arm", true));
System.out.println(root.contains("appl", false));
}
}