Skip to content

Commit 19789d9

Browse files
committed
trie implementation
1 parent 70dea5f commit 19789d9

1 file changed

Lines changed: 205 additions & 0 deletions

File tree

PuzzleCoding/src/Trie.java

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
import java.util.ArrayList;
2+
import java.util.LinkedList;
3+
import java.util.Queue;
4+
5+
/**
6+
* Implements a trie. We store the input list of words in tries so
7+
* that we can efficiently find words with a given prefix.
8+
*/
9+
public class Trie {
10+
11+
// Trie Node
12+
public static class Node {
13+
14+
//the character is the value of node
15+
public char character;
16+
public ArrayList<Node> children;
17+
18+
// end of a word
19+
public boolean terminated = false;
20+
21+
public Node() {
22+
children = new ArrayList<Node>();
23+
24+
}
25+
26+
public Node(char c) {
27+
this();
28+
character = c;
29+
}
30+
31+
public boolean addWord(String w) {
32+
if (w == null || w.length() == 0) {
33+
return false;
34+
}
35+
36+
Node child;
37+
char firstChar = w.charAt(0);
38+
Node tmp = getChild(firstChar);
39+
if (tmp == null) {
40+
child = new Node(firstChar);
41+
children.add(child);
42+
} else {
43+
child = tmp;
44+
45+
}
46+
47+
if (w.length() > 1) {
48+
child.addWord(w.substring(1));
49+
} else {
50+
child.terminated = true;
51+
}
52+
return true;
53+
}
54+
55+
public Node getChild(char c) {
56+
for (Node n : children) {
57+
if (n.character == c)
58+
return n;
59+
}
60+
return null;
61+
}
62+
63+
public Node getLeafNode(char c) {
64+
for (Node n : children) {
65+
if (n.children.size() != 0) {
66+
n.getLeafNode(c);
67+
} else {
68+
if (n.terminated) {
69+
return n;
70+
} else {
71+
return null;
72+
}
73+
}
74+
}
75+
return null;
76+
}
77+
78+
public Node getParentLeafNode(char c) {
79+
for (Node n : children) {
80+
if (n.children.size() != 0) {
81+
n.getLeafNode(c);
82+
} else {
83+
if (n.terminated) {
84+
return this;
85+
} else {
86+
return null;
87+
}
88+
}
89+
}
90+
return null;
91+
}
92+
93+
public boolean removeChild(Node child) {
94+
if (children.contains(child)) {
95+
children.remove(child);
96+
if (children.size() == 0) {
97+
this.terminated = true;
98+
}
99+
return true;
100+
} else {
101+
return false;
102+
}
103+
}
104+
105+
public boolean removeChild(char c) {
106+
Node child = new Node(c);
107+
return removeChild(child);
108+
}
109+
110+
public void getAllWords(StringBuilder path, ArrayList<String> words) {
111+
for (Node c : children) {
112+
StringBuilder word = new StringBuilder(path);
113+
word.append(c.character);
114+
if (c.terminated) {
115+
words.add(word.toString());
116+
117+
} else {
118+
c.getAllWords(word, words);
119+
}
120+
}
121+
122+
}
123+
124+
}
125+
126+
private Node root;
127+
128+
public Trie(ArrayList<String> words) {
129+
root = new Node();
130+
for (String word : words) {
131+
root.addWord(word);
132+
}
133+
}
134+
135+
public Trie(String[] words) {
136+
root = new Node();
137+
for (String word : words) {
138+
root.addWord(word);
139+
}
140+
}
141+
142+
public boolean contains(String word, boolean exactMatch) {
143+
Node current = root;
144+
for (int i = 0; i < word.length(); i++) {
145+
current = current.getChild(word.charAt(i));
146+
if (current == null)
147+
return false;
148+
}
149+
150+
return !exactMatch || current.terminated;
151+
}
152+
153+
public boolean removeWord(String w) {
154+
if (contains(w, true)) {
155+
for (int i = w.length() - 1; i >= 0; i--) {
156+
char c = w.charAt(i);
157+
Node parent = root.getParentLeafNode(c);
158+
parent.removeChild(c);
159+
if (parent.children.size() != 0) {
160+
break;
161+
}
162+
}
163+
164+
} else {
165+
return false;
166+
}
167+
return true;
168+
}
169+
170+
public ArrayList<String> getAllWords() {
171+
ArrayList<String> words = new ArrayList<String>();
172+
StringBuilder word = new StringBuilder();
173+
root.getAllWords(word, words);
174+
return words;
175+
}
176+
177+
public void printTrie(){
178+
Queue<Node> queue = new LinkedList<Node>(root.children);
179+
Queue<Node> nextQueue = new LinkedList<Node>();
180+
181+
while( !queue.isEmpty()){
182+
183+
Node n = queue.poll();
184+
nextQueue.addAll(n.children);
185+
System.out.print(n.character +" ");
186+
187+
if(queue.isEmpty()){
188+
queue.addAll(nextQueue);
189+
nextQueue.clear();
190+
System.out.println();
191+
}
192+
193+
}
194+
}
195+
196+
public static void main(String[] args) {
197+
String[] words = {"apple", "arm", "grape", "apc"};
198+
Trie root = new Trie(words);
199+
ArrayList<String> list = root.getAllWords();
200+
201+
root.printTrie();
202+
System.out.println(list.toString());
203+
}
204+
205+
}

0 commit comments

Comments
 (0)