blob: 51f729f7fbeacf77cf4c804d8b2a9b983f6abc32 (
plain)
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
|
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Queue;
public class Trie {
public static class TrieNode {
private TrieNode children[] = new TrieNode[26];
private boolean isWord = false;
private char character;
private TrieNode parent;
public TrieNode(){}
public TrieNode(char c, TrieNode parent) {
this.character = c;
this.parent = parent;
}
public TrieNode getChildren(char c) {
return children[(int)c - 97];
}
public ArrayList<TrieNode> getAllChildren() {
ArrayList<TrieNode> out = new ArrayList<>();
for (TrieNode child : children) {
if (child != null) {
out.add(child);
}
}
return out;
}
public TrieNode setChild(char c, TrieNode parent) {
children[(int)c - 97] = new TrieNode(c, parent);
return children[(int)c - 97];
}
public void setWord(boolean isWord) {
this.isWord = isWord;
}
public boolean isWord() {
return isWord;
}
public char getChar() {
return character;
}
public TrieNode getParent() {
return parent;
}
}
TrieNode root = new TrieNode();
public void add(String key) {
TrieNode temp = root;
for (char c : key.toLowerCase().toCharArray()) {
TrieNode next = temp.getChildren(c);
if (next == null) {
next = temp.setChild(c, temp);
}
temp = next;
}
temp.setWord(true);
}
public boolean contains(String str) {
TrieNode temp = root;
for (char c : str.toLowerCase().toCharArray()) {
TrieNode next = temp.getChildren(c);
if (next == null) {
return false;
}
temp = next;
}
return temp.isWord();
}
public ArrayList<String> getWords(String prefix) {
TrieNode temp = root;
ArrayList<String> words = new ArrayList<>();
for (char c : prefix.toLowerCase().toCharArray()) {
TrieNode next = temp.getChildren(c);
if (next == null) {
System.out.println("No words...");
return words;
}
temp = next;
}
TrieNode rel_root = temp;
Queue<TrieNode> q = new LinkedList<TrieNode>();
ArrayList<TrieNode> endingChars = new ArrayList<>();
q.add(rel_root);
while (q.isEmpty() == false) {
temp = q.remove();
if (temp.isWord()) {
endingChars.add(temp);
}
for (TrieNode child : temp.getAllChildren()) {
q.add(child);
}
}
// using a stack and a depth first algo would be better.
// instead we backtrack the parents until we get to the parent
for (TrieNode t : endingChars) {
temp = t;
String word = String.format("%c", t.getChar());
do {
temp = temp.getParent();
word = temp.getChar() + word;
} while (temp != rel_root);
words.add(prefix + word.substring(1));
}
return words;
}
public static void main(String[] args) {
Trie trie = new Trie();
String dict[] = {"cheers", "cheese", "chat", "bat"};
for (String w : dict) {
trie.add(w);
System.out.println(String.format("Added '%s' to trie...", w));
}
System.out.println("\nchips: " + trie.contains("chips"));
System.out.println("cheese: " + trie.contains("cheese"));
System.out.println("cat: " + trie.contains("cat"));
System.out.println("bat: " + trie.contains("bat"));
String prefixChecks[] = {"ch", "chee", "b"};
for (String prefix : prefixChecks) {
System.out.println("\nChecking prefixes for: '" + prefix + "':");
for (String word : trie.getWords(prefix)) {
System.out.println(word);
}
}
}
}
|