-
Notifications
You must be signed in to change notification settings - Fork 5
/
Trie.java
executable file
·169 lines (150 loc) · 4.04 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
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
package com.vee.algorithms.datastructures;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
/**
* @author vee, bragboy
* @see <a href="http://www.technicalypto.com/2010/04/trie-data-structure-part-2-node-and.html">Blog</a>
*/
public class Trie {
private TrieNode root = new TrieNode('_', "");
public void insert(String s){
TrieNode current = root;
/* IMP: for empty character set isWord = true */
if(s.length() ==0)
current.isWord = true;
for(int i=0; i<s.length();i++){
char ch = s.charAt(i);
TrieNode node = current.find(ch);
if(node == null) {
node = new TrieNode(ch, current.wordTillNow + ch);
current.children.put(ch, node);
}
current = node;
if(i == s.length()-1)
current.isWord = true;
}
}
public boolean search(String s){
TrieNode current = findNode(s);
return current != null && current.isWord;
}
public List<String> autocomplete(String s, int count) {
TrieNode current = findNode(s);
List<String> list = new ArrayList<String>();
Deque<TrieNode> q = new ArrayDeque<TrieNode>();
q.offer(current);
while (count > 0 && !q.isEmpty()) {
TrieNode node = q.pop();
if (node.isWord) {
list.add(node.wordTillNow);
count--;
}
q.addAll(node.children.values());
}
return list;
}
private TrieNode findNode(String s) {
TrieNode current = root;
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
TrieNode node = current.find(ch);
if (node == null)
return node;
else
current = node;
}
return current;
}
private int findNodeIndex(String s) {
TrieNode current = root;
int i = 0;
for (i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
TrieNode node = current.find(ch);
if (node == null)
return i;
else
current = node;
}
return i;
}
public static void main(String args[]) {
testAutoComplete();
//testPalindromePairs();
}
static void testAutoComplete() {
Trie t = new Trie();
try(BufferedReader bf = new BufferedReader(new FileReader("/usr/share/dict/words"))) {
String word;
while ((word = bf.readLine()) != null) {
t.insert(word);
}
} catch (Exception e) {
e.printStackTrace();
}
long time = System.nanoTime();
List<String> results = t.autocomplete("quintes", 10);
System.out.println("Results in " + (System.nanoTime() - time) / 1000D + " micrsoseconds");
System.out.println(results);
}
static void testPalindromePairs() {
String strs[] = new String[] {"code", "edoc", "da", "d"};//[(0, 1), (1, 0), (2, 3)].
Trie t = new Trie();
for (String s : strs) {
t.insert(new StringBuilder(s).reverse().toString());
}
System.out.println(isPalindrome("aba"));
System.out.println(isPalindrome("abba"));
System.out.println(isPalindrome("abc"));
System.out.println(isPalindrome("abca"));
for (String s : strs) {
int n = t.findNodeIndex(s);
boolean isPairPal = s.length() == n || (isPalindrome(s.substring(n, s.length())));
System.out.println(s + " " + s.substring(0, n) + " " + isPairPal);
}
}
static boolean isPalindrome(String s) {
int l = 0;
int r = s.length()-1;
while (l <= r) {
if (s.charAt(l++) != s.charAt(r--)) {
return false;
}
}
return true;
}
}
class TrieNode implements Serializable {
private static final long serialVersionUID = 1L;
boolean isWord;
char content;
String wordTillNow;
Map<Character, TrieNode> children;
public TrieNode(char c, String s){
content = c;
isWord = false;
wordTillNow = s;
children = new HashMap<>();
}
/* Watch the return statements */
public TrieNode find(char c) {
return children.get(c);
}
public String toString() {
return String.format("%c, %s, %s", content, wordTillNow, isWord);
}
}