-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathTrie.test.js
More file actions
223 lines (181 loc) · 5.83 KB
/
Trie.test.js
File metadata and controls
223 lines (181 loc) · 5.83 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
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
import { Trie } from '../Trie.js'
describe('Trie', () => {
describe('Constructor', () => {
it('should create an empty trie', () => {
const trie = new Trie()
expect(trie.root.key).toBe(null)
expect(trie.root.parent).toBe(null)
expect(trie.root.count).toBe(0)
expect(Object.keys(trie.root.children)).toHaveLength(0)
})
})
describe('insert', () => {
it('should insert words correctly', () => {
const trie = new Trie()
trie.insert('hello')
trie.insert('world')
trie.insert('hello')
expect(trie.contains('hello')).toBe(true)
expect(trie.contains('world')).toBe(true)
expect(trie.findOccurrences('hello')).toBe(2)
})
it('should handle empty string insertion', () => {
const trie = new Trie()
trie.insert('')
expect(trie.contains('')).toBe(true)
expect(trie.findOccurrences('')).toBe(1)
})
it('should not insert non-string values', () => {
const trie = new Trie()
trie.insert(123)
trie.insert(null)
trie.insert(undefined)
trie.insert({})
trie.insert('valid')
expect(trie.contains('valid')).toBe(true)
})
})
describe('contains', () => {
it('should return true for existing words', () => {
const trie = new Trie()
trie.insert('test')
trie.insert('testing')
expect(trie.contains('test')).toBe(true)
expect(trie.contains('testing')).toBe(true)
})
it('should return false for non-existing words', () => {
const trie = new Trie()
trie.insert('test')
expect(trie.contains('tes')).toBe(false)
expect(trie.contains('testing')).toBe(false)
expect(trie.contains('')).toBe(false)
})
it('should handle prefix that is not a complete word', () => {
const trie = new Trie()
trie.insert('programming')
expect(trie.contains('program')).toBe(false)
expect(trie.contains('programming')).toBe(true)
})
})
describe('findOccurrences', () => {
it('should return correct occurrence count', () => {
const trie = new Trie()
trie.insert('apple')
trie.insert('apple')
trie.insert('app')
expect(trie.findOccurrences('apple')).toBe(2)
expect(trie.findOccurrences('app')).toBe(1)
expect(trie.findOccurrences('ap')).toBe(0)
})
it('should return 0 for non-existing words', () => {
const trie = new Trie()
expect(trie.findOccurrences('nonexistent')).toBe(0)
})
})
describe('remove', () => {
it('should remove words correctly', () => {
const trie = new Trie()
trie.insert('hello')
trie.insert('hello')
trie.insert('hello')
trie.remove('hello', 2)
expect(trie.findOccurrences('hello')).toBe(1)
trie.remove('hello')
expect(trie.findOccurrences('hello')).toBe(0)
})
it('should handle removing more occurrences than exist', () => {
const trie = new Trie()
trie.insert('test')
trie.remove('test', 5)
expect(trie.findOccurrences('test')).toBe(0)
})
it('should not remove if count is invalid', () => {
const trie = new Trie()
trie.insert('test')
trie.remove('test', 0)
expect(trie.findOccurrences('test')).toBe(1)
trie.remove('test', -1)
expect(trie.findOccurrences('test')).toBe(1)
})
it('should handle empty string removal', () => {
const trie = new Trie()
trie.insert('')
trie.insert('')
trie.remove('', 1)
expect(trie.findOccurrences('')).toBe(1)
})
})
describe('findAllWords', () => {
it('should find all words with given prefix', () => {
const trie = new Trie()
trie.insert('cat')
trie.insert('category')
trie.insert('caterpillar')
trie.insert('dog')
trie.insert('cattle')
const words = trie.findAllWords('cat')
expect(words).toHaveLength(4)
expect(words).toEqual(
expect.arrayContaining([
{ word: 'cat', count: 1 },
{ word: 'category', count: 1 },
{ word: 'caterpillar', count: 1 },
{ word: 'cattle', count: 1 }
])
)
})
it('should return empty array for non-existing prefix', () => {
const trie = new Trie()
trie.insert('hello')
expect(trie.findAllWords('world')).toEqual([])
})
it('should return words with correct counts', () => {
const trie = new Trie()
trie.insert('test')
trie.insert('test')
trie.insert('testing')
const words = trie.findAllWords('test')
expect(words).toEqual(
expect.arrayContaining([
{ word: 'test', count: 2 },
{ word: 'testing', count: 1 }
])
)
})
})
describe('findPrefix', () => {
it('should return node for existing prefix', () => {
const trie = new Trie()
trie.insert('hello')
const node = trie.findPrefix('hell')
expect(node).not.toBeNull()
expect(node.key).toBe('l')
})
it('should return null for non-existing prefix', () => {
const trie = new Trie()
trie.insert('hello')
expect(trie.findPrefix('world')).toBeNull()
})
it('should return null for non-string input', () => {
const trie = new Trie()
expect(trie.findPrefix(123)).toBeNull()
})
})
describe('integration tests', () => {
it('should handle complex operations', () => {
const trie = new Trie()
const words = ['apple', 'application', 'app', 'banana', 'band']
words.forEach((word) => trie.insert(word))
words.forEach((word) => {
expect(trie.contains(word)).toBe(true)
})
const appWords = trie.findAllWords('app')
expect(appWords).toHaveLength(3)
trie.remove('app')
expect(trie.contains('app')).toBe(false)
expect(trie.contains('apple')).toBe(true)
trie.insert('apple')
expect(trie.findOccurrences('apple')).toBe(2)
})
})
})