-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTries.java
More file actions
188 lines (167 loc) · 5.4 KB
/
Tries.java
File metadata and controls
188 lines (167 loc) · 5.4 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
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
public class Tries {
public static class Node {
Node children[] = new Node[26];
boolean eow = false;
int freq;
Node() {
for (int i = 0; i < children.length; i++) {
children[i] = null;
}
freq = 1;
}
}
public static Node root = new Node();
public static void insert(String word) {
Node crnt = root;
for (int i = 0; i < word.length(); i++) {
int idx = word.charAt(i) - 'a';
if (crnt.children[idx] == null) {
crnt.children[idx] = new Node();
} else {
crnt.children[idx].freq++;
}
crnt = crnt.children[idx];
}
crnt.eow = true;
}
public static String search(String key) {
Node crnt = root;
String ans = "";
for (int i = 0; i < key.length(); i++) {
int idx = key.charAt(i) - 'a';
if (crnt.children[idx].freq == 1) {
ans += (char) (idx + 'a');
break;
} else if (crnt.children[idx] != null) {
ans += (char) (idx + 'a');
}
crnt = crnt.children[idx];
}
return ans;
}
public static class Info {
Node node;
int idx;
Info(Node node, int idx) {
this.idx = idx;
this.node = node;
}
}
static Queue<Info> q = new LinkedList<>();
public static void printTrie(Node root) {
q.clear();
q.add(new Info(root, -1));
q.add(null);
while (!q.isEmpty()) {
Info cc = q.remove();
if (cc == null) {
System.out.println();
if (q.isEmpty()) {
break;
} else {
q.add(null);
}
} else {
System.out.print(cc.idx == -1 ? "@" : ((char) (cc.idx + 'a') + " "));
Node childrens[] = cc.node.children;
for (int i = 0; i < childrens.length; i++) {
if (childrens[i] != null) {
q.add(new Info(childrens[i], i));
}
}
}
}
}
public static void prefix(Node root, String words[]) {
ArrayList<String> ans = new ArrayList<>();
for (String word : words) {
ans.add(search(word));
}
System.out.println(ans);
}
public static void prefixAP(Node root, String ans) {
if (root == null) {
return;
}
if (root.freq == 1) {
System.out.print(ans + " ");
return;
}
for (int idx = 0; idx < root.children.length; idx++) {
if (root.children != null) {
prefixAP(root.children[idx], ans + (char) (idx + 'a'));
}
}
}
public static boolean prefixKey(String key) {
Node crnt = root;
for (int i = 0; i < key.length(); i++) {
int idx = key.charAt(i) - 'a';
if (crnt.children[idx] == null) {
return false;
}
crnt = crnt.children[idx];
}
return true;
}
public static int countNode(Node root) {
if (root == null) {
return 0;
}
int count = 0;
for (Node children : root.children) {
if (children != null) {
count += countNode(children);
}
}
return count + 1;
}
static String ans = " ";
public static void longestWord(Node root, StringBuilder temp) {
if (root == null)
return;
for (int i = 0; i < 26; i++) { // this provide lexiographically smaller word , if we reverse then it return
// large word {for(int i=25 ; i>0 ; i--)}
if (root.children[i] != null && root.children[i].eow == true) {
temp.append((char) ('a' + i));
if (temp.length() > ans.length())
ans = temp.toString();
longestWord(root.children[i], temp);
temp.deleteCharAt(temp.length() - 1);
}
}
}
public static void main(String args[]) {
// String words[] = {
// "zebra", "dog", "duck", "dove", "hadshi", "shanks"
// };
// for (String word : words)
// insert(word);
// // Prefix of the words my way
// prefix(root, words);
// root.freq = -1;
// // prefix of the words in AP way
// prefixAP(root, "");
// System.out.println();
// String key = "ze";
// System.out.println("Prefix of word " + key + " present in the TRIES : " +
// prefixKey(key));
// System.out.println("\nTrie in form of level order ");
// printTrie(root);
// String queString = "ababa";
// for (int i = 0; i < queString.length(); i++) {
// insert(queString.substring(i));
// }
// printTrie(root);
// System.out.println("The Number of unique prefixes are : " + countNode(root));
String[] stringAry = { "a", "ap", "app", "appl", "apple", "apply" };
for (String s : stringAry)
insert(s);
printTrie(root);
longestWord(root, new StringBuilder());
System.out.println(ans);
}
}