-
Notifications
You must be signed in to change notification settings - Fork 0
/
Trie.test.ts
247 lines (181 loc) · 6.31 KB
/
Trie.test.ts
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import { Trie } from "./Trie.js";
import { Node } from "./Node.js";
type NodeTuple = [
element: string,
count: number,
isComplete: boolean,
];
/**
* Test helper for inspecting nodes along the traversal path.
* @param root - node to start the traversal at
* @param traversal - traversal instructions
* @returns - an array of tuples describing each node visited
*/
function getNodeArray(root: Node, traversal: string): NodeTuple[] {
return traversal.split("").map((char): NodeTuple => {
if (!root.children.hasOwnProperty(char)) {
return ["", 0, false];
}
root = root.children[char];
return [
root.element,
root.count.get(),
root.complete.get(),
];
})
}
describe("Trie", () => {
describe("add()", () => {
it("adds a single character word to an empty trie", () => {
const trie = new Trie();
trie.add("a");
const { root } = trie.__debug();
const a = getNodeArray(root, "a");
expect(a[0]).toEqual(["a", 0, true]);
});
it("adds a multi-character word to an empty trie", () => {
const trie = new Trie();
trie.add("ace");
const { root } = trie.__debug();
const ace = getNodeArray(root, "ace");
expect(ace[2]).toEqual(["e", 0, true]);
});
it("adds multiple words to a trie with no overlap", () => {
const trie = new Trie();
trie.add("ace");
trie.add("bev");
const { root } = trie.__debug();
const ace = getNodeArray(root, "ace");
expect(ace[2]).toEqual(["e", 0, true]);
const bev = getNodeArray(root, "bev");
expect(bev[2]).toEqual(["v", 0, true]);
});
it("adds a kangaroo word to a trie", () => {
const trie = new Trie();
trie.add("malign");
trie.add("malignant");
const { root } = trie.__debug();
const malignant = getNodeArray(root, "malignant");
expect(malignant[5]).toEqual(["n", 1, true]);
expect(malignant[8]).toEqual(["t", 0, true]);
});
it("adds a joey word to a trie", () => {
const trie = new Trie();
trie.add("malignant");
trie.add("malign");
const { root } = trie.__debug();
const malignant = getNodeArray(root, "malignant");
expect(malignant[5]).toEqual(["n", 1, true]);
expect(malignant[8]).toEqual(["t", 0, true]);
});
});
describe("remove()", () => {
it("removes all nodes from a one word Trie", () => {
const trie = new Trie();
trie.add("removable");
let { root } = trie.__debug();
const removable = getNodeArray(root, "removable");
expect(removable[8]).toEqual(["e", 0, true]);
trie.remove("removable");
({ root } = trie.__debug());
expect(root.children).toEqual({});
});
it("removes only the nodes not used by another word", () => {
const trie = new Trie();
trie.add("removable");
trie.add("remo");
let { root } = trie.__debug();
let removable = getNodeArray(root, "removable");
expect(removable[8]).toEqual(["e", 0, true]);
expect(removable[3]).toEqual(["o", 1, true]);
trie.remove("removable");
({ root } = trie.__debug());
removable = getNodeArray(root, "remo");
expect(removable[3]).toEqual(["o", 0, true]);
});
it("removes no nodes when all are used by another word", () => {
const trie = new Trie();
trie.add("removable");
trie.add("remo");
let { root } = trie.__debug();
let removable = getNodeArray(root, "removable");
expect(removable[3]).toEqual(["o", 1, true]);
expect(removable[8]).toEqual(["e", 0, true]);
trie.remove("remo");
({ root } = trie.__debug());
removable = getNodeArray(root, "removable");
expect(removable[3]).toEqual(["o", 1, false]);
});
it("throws an error if the word doesn't exist", () => {
const trie = new Trie();
expect(() => trie.remove("invisible")).toThrow("invisible does not exist");
});
});
describe("has()", () => {
it("returns true if the word exists", () =>{
const trie = new Trie();
trie.add("found");
expect(trie.has("found")).toBe(true);
});
it("returns false if the word does not exist", () =>{
const trie = new Trie();
expect(trie.has("missing")).toBe(false);
});
it("returns false if the word no longer exists", () =>{
const trie = new Trie();
trie.add("liberate");
expect(trie.has("liberate")).toBe(true);
trie.remove("liberate");
expect(trie.has("liberate")).toBe(false);
});
});
describe("search()", () => {
it("returns an empty array if the trie is empty", () => {
const trie = new Trie();
expect(trie.search("fruitless")).toEqual([]);
});
it("returns one matching word on an exact match", () => {
const trie = new Trie();
trie.add("emenate");
expect(trie.search("emenate")).toEqual(["emenate"]);
});
it("returns one matching word on a partial match", () => {
const trie = new Trie();
trie.add("emenate");
expect(trie.search("eme")).toEqual(["emenate"]);
});
it("returns matching words in a populated trie", () => {
const trie = new Trie();
trie.add("indistinguishable");
trie.add("indepth");
trie.add("interest");
trie.add("identification");
trie.add("unrelated");
expect(trie.search("ind")).toEqual(["indepth", "indistinguishable"]);
});
it("returns matching words in a populated trie", () => {
const trie = new Trie();
trie.add("indistinguishable", 0);
trie.add("indepth", 1);
trie.add("interest", 2);
trie.add("identification", 3);
trie.add("unrelated", 4);
expect(trie.search("ind")).toEqual(["indistinguishable", "indepth"]);
});
it("returns all words in a trie", () => {
const trie = new Trie();
trie.add("indistinguishable", 2);
trie.add("indepth", 1);
trie.add("interest", 0);
expect(trie.search("in")).toEqual(["interest", "indepth", "indistinguishable"]);
});
it("does not return a word removed from the trie", () => {
const trie = new Trie();
trie.add("indistinguishable", 2);
trie.add("indepth", 1);
trie.add("interest", 0);
trie.remove("indepth");
expect(trie.search("in")).toEqual(["interest", "indistinguishable"]);
});
});
});