forked from kodecocodes/swift-algorithm-club
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrie.swift
512 lines (429 loc) · 11.7 KB
/
trie.swift
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/*
Queue implementation (taken from repository, needed for findPrefix())
*/
public struct Queue<T> {
private var array = [T?]()
private var head = 0
public var isEmpty: Bool {
return count == 0
}
public var count: Int {
return array.count - head
}
public mutating func enqueue(element: T) {
array.append(element)
}
public mutating func dequeue() -> T? {
guard head < array.count, let element = array[head] else { return nil }
array[head] = nil
head += 1
let percentage = Double(head)/Double(array.count)
if array.count > 50 && percentage > 0.25 {
array.removeFirst(head)
head = 0
}
return element
}
}
/*
A Trie (Pre-fix Tree)
Some of the functionality of the trie makes use of the Queue implementation for this project.
Every node in the Trie stores a bit of information pertainining to what it references:
-Character (letter of an inserted word)
-Parent (Letter that comes before the current letter in some word)
-Children (Words that have more letters than available in the prefix)
-isAWord (Does the current letter mark the end of a known inserted word?)
-visited (Mainly for the findPrefix() function)
*/
public class Node {
private var character: String?
private var parent: Node?
private var children: [String:Node]
private var isAWord: Bool
private var visited: Bool //only for findPrefix
init(c: String?, p: Node?){
self.character = c
self.children = [String:Node]()
self.isAWord = false
self.parent = p
self.visited = false
}
/*
Function Name: char()
Input: N/A
Output: String
Functionality: Returns the associated value of the node
*/
func char() -> String {
return self.character!
}
/*
Function Name: update
Input: String
Output: N/A
Functionality: Updates the associated value of the node
*/
func update(c: String?) {
self.character = c
}
/*
Function Name: isLeaf
Input: N/A
Output: Bool
Functionality: Returns true if the node is a leaf node, false otherwise
*/
func isLeaf() -> Bool{
return self.children.count == 0
}
/*
Function Name: getParent
Input: N/A
Output: Node
Functionality: Returns the parent node of the current node
*/
func getParent() -> Node {
return parent!
}
/*
Function Name: setParent
Input: Node
Output: N/A
Functionality: Changes the parent of the current node to the passed node
*/
func setParent(node: Node?) {
self.parent = node
}
/*
Function Name: getChildAt
Input: String
Output: Node
Functionality: returns the child node that holds the specific passed letter
*/
func getChildAt(s: String) -> Node {
return self.children[s]!
}
/*
Function Name: isValidWord
Input: N/A
Output: Bool
Functionality: Returns whether or not the current node marks the end of a valid word
*/
func isValidWord() -> Bool{
return self.isAWord
}
/*
Function Name: isWord
Input: N/A
Output: N/A
Functionality: the current node is indeed a word
*/
func isWord() {
self.isAWord = true
}
/*
Function Name: isNotWord
Input: N/A
Output: N/A
Functionality: marks the current node as not a word
*/
func isNotWord() {
self.isAWord = false
}
/*
Function Name: isRoot
Input: N/A
Output: Bool
Functionality: Returns whether or not the current node is the root of the trie
*/
func isRoot() -> Bool {
return self.character == ""
}
/*
Function Name: numChildren
Input: N/A
Output: Int
Functionality: Returns the number of immediate letters that follow the current node
*/
func numChildren() -> Int {
return self.children.count
}
/*
Function Name: getChildren
Input: N/A
Output: [String: Node]
Functionality: Returns the letters that immediately follow the current node's value for possible word segments that follow
*/
func getChildren() -> [String: Node] {
return self.children
}
/*
Function Name: printNode
Input: String, Bool
Output: N/A
Functionality: prints to the console a string representation of the current node in the trie
*/
func printNode(var indent: String, leaf: Bool) {
print(indent, terminator: "")
if leaf {
print("\\-", terminator: "")
indent += " "
}
else {
print("|-", terminator: "")
indent += "| "
}
print(self.char())
var i = 0
for (_, node) in self.children {
node.printNode(indent, leaf: i == self.getChildren().count-1)
i+=1
}
}
}
/*
The Trie class has the following attributes:
-root (the root of the trie)
-wordList (the words that currently exist in the trie)
-wordCount (the number of words in the trie)
*/
public class Trie {
private var root: Node
private var wordList: [String]
private var wordCount = 0
init() {
self.root = Node(c: "", p: nil)
self.wordList = []
}
init(wordList: Set<String>) {
self.root = Node(c: "", p: nil)
self.wordList = []
for word in wordList {
self.insert(word)
}
}
/*
Function Name: merge
Input: Trie
Output: Trie
Functionality: Merges two tries into one and returns the merged trie
*/
func merge(other: Trie) -> Trie{
let newWordList = Set(self.getWords() + other.getWords())
return Trie(wordList: newWordList)
}
/*
Function Name: find
Input: String
Output: (Node?, Bool)
Functionality: Looks for a specific key and returns a tuple that has a reference to the node(if found) and true/false depending on if it was found
*/
func find(key: String) -> (node: Node?, found: Bool) {
var currentNode = self.root
for c in key.characters {
if currentNode.children[String(c)] == nil {
return(nil, false)
}
currentNode = currentNode.children[String(c)]!
}
return(currentNode, currentNode.isValidWord())
}
/*
Function Name: isEmpty
Input: N/A
Output: Bool
Functionality: returns true if the trie is empty, false otherwise
*/
func isEmpty() -> Bool {
return wordCount == 0
}
/*
Function Name: count
Input: N/A
Output: Int
Functionality: returns the number of words in the trie
*/
func count() -> Int {
return wordCount
}
/*
Function Name: getWords
Input: N/A
Output: [String]
Functionality: returns the list of words that exist in the trie
*/
func getWords() -> [String] {
return wordList
}
/*
Function Name: contains
Input: String
Output: Bool
Functionality: returns true if the tries has the word passed, false otherwise
*/
func contains(w: String) -> Bool {
return find(w.lowercaseString).found
}
/*
Function Name: isPrefix
Input: String
Output: (Node?, Bool)
Functionality: returns a tuple containing a reference to the final node in the prefix (if it exists) and true/false depending on whether or not the prefix exists in the trie
*/
func isPrefix(p: String) -> (node: Node?, found: Bool) {
let prefixP = p.lowercaseString
var currentNode = self.root
for c in prefixP.characters {
if currentNode.children[String(c)] == nil{
return (nil, false)
}
currentNode = currentNode.children[String(c)]!
}
if currentNode.numChildren() > 0 {
return (currentNode, true)
}
return (nil, false)
}
/*
Function Name: insert
Input: String
Output: (String, Bool)
Functionality: Inserts a word int othe trie. Returns a tuple containing the word attempted to be added, and true/false depending on whether or not the insertion was successful
*/
func insert(w: String) -> (word: String, inserted: Bool) {
let word = w.lowercaseString
var currentNode = self.root
var length = word.characters.count
if self.contains(word) {
return (w, false)
}
var index = 0
var c = Array(word.characters)[index]
while let child = currentNode.children[String(c)] {
currentNode = child
length -= 1
index += 1
if length == 0 {
currentNode.isWord()
wordList.append(w)
wordCount += 1
return (w, true)
}
c = Array(word.characters)[index]
}
let remainingChars = String(word.characters.suffix(length))
for c in remainingChars.characters {
currentNode.children[String(c)] = Node(c: String(c), p: currentNode)
currentNode = currentNode.children[String(c)]!
}
currentNode.isWord()
wordList.append(w)
wordCount += 1
return (w, true)
}
/*
Function Name: insertWords
Input: [String]
Output: ([String], Bool)
Functionality: attempts to insert all words from input array. returns a tuple containing the input array and true if some of the words were succesffuly added, false if none were added
*/
func insertWords(wordList: [String]) -> (wordList: [String], inserted: Bool){
var succesful: Bool = false
for word in wordList {
succesful = self.insert(word).inserted || succesful
}
return(wordList, succesful)
}
/*
Function Name: remove
Input: String
Output: (String, Bool)
Functionality: Removes the specified key from the trie if it exists, returns tuple containing the word attempted to be removed and true/false if the removal was succesful
*/
func remove(w: String) -> (word: String, removed: Bool){
let word = w.lowercaseString
if !self.contains(w) {
return (w, false)
}
var currentNode = self.root
for c in word.characters {
currentNode = currentNode.getChildAt(String(c))
}
if currentNode.numChildren() > 0 {
currentNode.isNotWord()
} else {
var character = currentNode.char()
while(currentNode.numChildren() == 0 && !currentNode.isRoot()) {
currentNode = currentNode.getParent()
currentNode.children[character]!.setParent(nil)
currentNode.children[character]!.update(nil)
currentNode.children[character] = nil
character = currentNode.char()
}
}
wordCount -= 1
var index = 0
for item in wordList{
if item == w {
wordList.removeAtIndex(index)
}
index += 1
}
return (w, true)
}
/*
Function Name: findPrefix
Input: String
Output: [String]
Functionality: returns a list containing all words in the trie that have the specified prefix
*/
func findPrefix(p: String) -> [String] {
if !self.isPrefix(p).found {
return []
}
var q: Queue = Queue<Node>()
var n: Node = self.isPrefix(p).node!
var wordsWithPrefix: [String] = []
var word = p
var tmp = ""
q.enqueue(n)
while let current = q.dequeue() {
for (char, child) in current.getChildren() {
if !child.visited {
q.enqueue(child)
child.visited = true
if child.isValidWord() {
var currentNode = child
while currentNode !== n {
tmp += currentNode.char()
currentNode = currentNode.getParent()
}
tmp = String(tmp.characters.reverse())
wordsWithPrefix.append(word + tmp)
tmp = ""
}
}
}
}
return wordsWithPrefix
}
/*
Function Name: removeAll
Input: N/A
Output: N/A
Functionality: removes all nodes in the trie using remove as a subroutine
*/
func removeAll() {
for word in wordList {
self.remove(word)
}
}
/*
Function Name: printTrie
Input: N/A
Output: N/A
Functionality: prints all the nodes of the trie to console in a nice and easy to understand format
*/
func printTrie() {
self.root.printNode("", leaf: true)
}
}