@@ -62,6 +62,7 @@ public class Node {
62
62
return self . children
63
63
}
64
64
65
+
65
66
func printNode( var indent: String , leaf: Bool ) -> Void {
66
67
67
68
print ( indent, terminator: " " )
@@ -96,17 +97,32 @@ public class Trie {
96
97
self . wordList = [ ]
97
98
}
98
99
99
- func find( key: String ) -> ( key: String , found: Bool ) {
100
+ init ( wordList: Set < String > ) {
101
+
102
+ self . root = Node ( c: " " , p: nil )
103
+ self . wordList = [ ]
104
+
105
+ for word in wordList {
106
+ self . insert ( word)
107
+ }
108
+ }
109
+
110
+ func merge( other: Trie ) -> Trie {
111
+ let newWordList = Set ( self . getWords ( ) + other. getWords ( ) )
112
+ return Trie ( wordList: newWordList)
113
+ }
114
+
115
+ func find( key: String ) -> ( node: Node ? , found: Bool ) {
100
116
var currentNode = self . root
101
117
102
118
for c in key. characters {
103
119
if currentNode. children [ String ( c) ] == nil {
104
- return ( key , false )
120
+ return ( nil , false )
105
121
}
106
122
currentNode = currentNode. children [ String ( c) ] !
107
123
}
108
124
109
- return ( key , currentNode. isValidWord ( ) )
125
+ return ( currentNode , currentNode. isValidWord ( ) )
110
126
}
111
127
112
128
func isEmpty( ) -> Bool {
@@ -125,21 +141,20 @@ public class Trie {
125
141
return find ( w. lowercaseString) . found
126
142
}
127
143
128
- func isPrefix( w : String ) -> ( node: Node ? , found: Bool ) {
129
- var currentNode = self . root
144
+ func isPrefix( p : String ) -> ( node: Node ? , found: Bool ) {
145
+ let prefixP = p . lowercaseString
130
146
131
- let word = w. lowercaseString
132
- if !self . contains ( w) {
133
- return ( nil , false )
134
- }
147
+ var currentNode = self . root
135
148
136
- for c in word . characters {
137
- if let child = currentNode. children [ String ( c) ] {
138
- currentNode = child
149
+ for c in prefixP . characters {
150
+ if currentNode. children [ String ( c) ] == nil {
151
+ return ( nil , false )
139
152
}
153
+
154
+ currentNode = currentNode. children [ String ( c) ] !
140
155
}
141
156
142
- if currentNode. getChildren ( ) . count > 0 {
157
+ if currentNode. numChildren ( ) > 0 {
143
158
return ( currentNode, true )
144
159
}
145
160
@@ -167,7 +182,6 @@ public class Trie {
167
182
}
168
183
169
184
let remainingChars = String ( word. characters. suffix ( length) )
170
- print ( remainingChars)
171
185
for c in remainingChars. characters {
172
186
currentNode. children [ String ( c) ] = Node ( c: String ( c) , p: currentNode)
173
187
currentNode = currentNode. children [ String ( c) ] !
@@ -180,7 +194,6 @@ public class Trie {
180
194
}
181
195
182
196
func remove( w: String ) -> ( word: String , removed: Bool ) {
183
-
184
197
let word = w. lowercaseString
185
198
186
199
if ( !self . contains ( w) ) {
@@ -196,13 +209,13 @@ public class Trie {
196
209
currentNode. isNotWord ( )
197
210
} else {
198
211
var character = currentNode. char ( )
199
- while ( currentNode. numChildren ( ) < 1 ) {
212
+ while ( currentNode. numChildren ( ) == 0 && !currentNode. isRoot ( ) ) {
213
+ print ( currentNode. getParent ( ) . char ( ) )
200
214
currentNode = currentNode. getParent ( )
201
215
currentNode. children [ character] !. setParent ( nil )
202
216
currentNode. children [ character] !. update ( nil )
203
217
currentNode. children [ character] = nil
204
218
character = currentNode. char ( )
205
-
206
219
}
207
220
}
208
221
@@ -221,46 +234,46 @@ public class Trie {
221
234
222
235
private func getChildrenWithPrefix( node: Node , var word: String , var words: [ String ] ) -> [ String ] {
223
236
224
- if node. isLeaf ( ) {
225
- word += node. char ( )
237
+ print ( word)
226
238
239
+ if node. isLeaf ( ) && node. isValidWord ( ) {
227
240
words. append ( word)
241
+ print ( words)
228
242
229
- }
243
+ } else {
230
244
231
- for (child, n) in node. getChildren ( ) {
232
- word += child
233
- getChildrenWithPrefix ( n, word: word, words: words)
245
+ for (child, n) in node. getChildren ( ) {
246
+ print ( child)
247
+ word += child
248
+ getChildrenWithPrefix ( n, word: word, words: words)
249
+ }
234
250
}
235
251
236
252
return words
237
253
}
238
254
239
255
func findPrefix( p: String ) -> [ String ] {
256
+ print ( " Entered " )
257
+
258
+
259
+ //var (node, pFound: Bool) = self.isPrefix(p)
240
260
if self . isPrefix ( p) . found {
241
- print ( " here " )
242
- return getChildrenWithPrefix ( self . isPrefix ( p) . node!, word: " " , words: [ ] )
261
+ print ( " I found the prefix! " )
262
+ return getChildrenWithPrefix ( self . isPrefix ( p) . node!, word: p . lowercaseString , words: [ ] )
243
263
}
244
264
245
- return [ ]
265
+ return [ " HE " ]
246
266
}
247
267
248
268
249
- private func removeAllHelper( node: Node , w: String ) {
250
-
251
- if ( node. getChildren ( ) . count == 0 ) {
252
-
269
+ func removeAll( ) -> Void {
270
+ for word in wordList {
271
+ self . remove ( word)
253
272
}
254
-
255
-
256
-
273
+ self . root. update ( nil )
257
274
}
258
275
259
276
260
- func removeAll( w: String ) {
261
-
262
- }
263
-
264
277
func printTrie( ) {
265
278
self . root. printNode ( " " , leaf: true )
266
279
}
@@ -277,6 +290,19 @@ print(x.isValidWord())*/
277
290
278
291
279
292
var T : Trie = Trie ( )
293
+ T . insert ( " Hello " )
294
+ T . insert ( " Hi " )
295
+ T . insert ( " Hey " )
296
+ T . insert ( " Hallo " )
297
+ T . insert ( " Henry " )
298
+ var U : Trie = Trie ( wordList: Set ( [ " Hey " , " HO " , " hello " , " yolo " ] ) )
299
+ var V : Trie = T . merge ( U)
300
+ //T.printTrie()
301
+ //U.printTrie()
302
+ //V.printTrie()
303
+ print ( V . getWords ( ) )
304
+ V . removeAll ( )
305
+ //V.printTrie()
280
306
/*T.insert("Hello")
281
307
T.insert("Hey")
282
308
T.insert("YOLO")
@@ -290,17 +316,6 @@ assert(T.count() == 3)
290
316
assert(T.contains("Him") == false, "Test failed")
291
317
assert(T.wordList.count == 3)*/
292
318
293
-
294
- //T.insert("Hello")
295
- T . insert ( " Hi " )
296
- T . insert ( " Hey " )
297
- //T.insert("Hallo")
298
- T . insert ( " Henry " )
299
- T . printTrie ( )
300
- assert ( T . contains ( " Henry " ) == true )
301
- T . remove ( " Henry " )
302
- assert ( T . contains ( " Henry " ) == false )
303
- //assert(T.count() == 4)
304
- assert ( T . isPrefix ( " Hen " ) . found == false )
305
- T . printTrie ( )
306
- print ( T . findPrefix ( " H " ) )
319
+ //T.printTrie()
320
+ //print(T.find(""))
321
+ //print(T.findPrefix("H"))
0 commit comments