-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTernarySearchTreeAutocomplete.java
160 lines (145 loc) · 5.29 KB
/
TernarySearchTreeAutocomplete.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
package autocomplete;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
/**
* Ternary search tree (TST) implementation of the {@link Autocomplete} interface.
*
* @see Autocomplete
*/
public class TernarySearchTreeAutocomplete implements Autocomplete {
/**
* The overall root of the tree: the first character of the first autocompletion term added to this tree.
*/
private Node overallRoot;
private int size;
/**
* Constructs an empty instance.
*/
public TernarySearchTreeAutocomplete() {
overallRoot = null;
}
/**
* Returns true if and only if this TST contains the given key.
*/
public boolean contains(CharSequence key) {
if (key == null) {
throw new NullPointerException("calls contains() with null argument");
} else if (key.length() == 0) {
throw new IllegalArgumentException("key must have length >= 1");
}
Node node = get(overallRoot, key, 0);
return node != null && node.isTerm;
}
// Private helper method for returning the node (subtree) corresponding to given key
private Node get(Node node, CharSequence key, int depth) {
if (node == null) {
return null;
}
char curr = key.charAt(depth);
if (curr < node.data) {
// if curr comes before node.data, traverse down the left subtree
return get(node.left, key, depth);
} else if (curr > node.data) {
// if curr comes after node.data, traverse down the right subtree
return get(node.right, key, depth);
} else if (depth < key.length() - 1) {
// if curr is the correct letter, and not at the end of the tree, traverse down the middle subtree
return get(node.mid, key, depth + 1);
} else {
// if at the end of the tree path, return the current letter
return node;
}
}
// Private helper method for adding a node if it doesn't already exist in the TST.
private Node add(Node node, CharSequence key, int depth) {
char curr = key.charAt(depth);
if (node == null) {
node = new Node(curr, false);
}
if (curr < node.data) {
// if curr comes alphabetically before node.data, traverse down the left subtree
node.left = add(node.left, key, depth);
} else if (curr > node.data) {
// if curr comes alphabetically after node.data, traverse down the right subtree
node.right = add(node.right, key, depth);
} else if (depth < key.length() - 1) {
// if curr is the correct letter, and not at the end of the tree, traverse down the middle subtree
node.mid = add(node.mid, key, depth + 1);
} else {
// if at the end of the tree path, ensure isTerm is true
node.isTerm = true;
}
return node;
}
@Override
public void addAll(Collection<? extends CharSequence> terms) {
for (CharSequence key : terms) {
if (key == null) {
throw new NullPointerException("calls add() with null key");
}
if (!contains(key)) {
overallRoot = add(overallRoot, key, 0);
size += 1;
}
}
}
// Private helper method for collecting all keys in the node (subtree) with the given prefix
private void collect(Node node, StringBuilder prefix, List<CharSequence> list) {
if (node == null) {
return;
}
collect(node.left, prefix, list);
if (node.isTerm) {
list.add(prefix.toString() + node.data);
}
prefix.append(node.data);
collect(node.mid, prefix, list);
prefix.deleteCharAt(prefix.length() - 1);
collect(node.right, prefix, list);
}
/** Returns all strings in the TST as a list. */
public List<CharSequence> keys() {
List<CharSequence> list = new LinkedList<>();
collect(overallRoot, new StringBuilder(), list);
return list;
}
public List<CharSequence> keysWithPrefix(CharSequence prefix) {
if (prefix == null) {
throw new NullPointerException("calls keysWithPrefix() with null argument");
} else if (prefix.length() == 0) {
throw new IllegalArgumentException("prefix must have length >= 1");
}
List<CharSequence> list = new LinkedList<>();
Node node = get(overallRoot, prefix, 0);
if (node == null) {
return list;
}
if (node.isTerm) {
list.add(prefix);
}
collect(node.mid, new StringBuilder(prefix), list);
return list;
}
@Override
public List<CharSequence> allMatches(CharSequence prefix) {
return keysWithPrefix(prefix);
}
/**
* A search tree node representing a single character in an autocompletion term.
*/
private static class Node {
private char data;
private boolean isTerm;
private Node left;
private Node mid;
private Node right;
public Node(char data, boolean isTerm) {
this.data = data;
this.isTerm = isTerm;
this.left = null;
this.mid = null;
this.right = null;
}
}
}