@@ -251,6 +251,7 @@ class RadixTree {
251
251
var currEdge = root
252
252
while ( true ) {
253
253
var found = false
254
+ //Loop through currEdge's children
254
255
for c in currEdge. children {
255
256
//First check if the search string and the child's label are equal
256
257
// if so the string is in the tree, return true
@@ -260,8 +261,8 @@ class RadixTree {
260
261
//If that is not true, find the shared string b/t the search string
261
262
// and the label
262
263
var shared = sharedPrefix ( searchStr, c. label)
263
- //If the shared string is equal to the label, update the curent node
264
- // and run it back
264
+ //If the shared string is equal to the label, update the curent node,
265
+ // break the loop, and run it back
265
266
if shared == c. label {
266
267
currEdge = c
267
268
var tempIndex = searchStr. startIndex
@@ -272,7 +273,7 @@ class RadixTree {
272
273
found = true
273
274
break
274
275
}
275
- //If the shared string is empty, go to the next children
276
+ //If the shared string is empty, go to the next child
276
277
else if shared. characters. count == 0 {
277
278
continue
278
279
}
@@ -287,14 +288,16 @@ class RadixTree {
287
288
return false
288
289
}
289
290
}
291
+ //If nothing was found, return false
290
292
if !found {
291
293
return false
292
294
}
293
295
}
294
296
}
295
297
298
+ //Removes a string from the tree
296
299
func remove( _ str: String ) -> Bool {
297
- //You cannot remove the empty string from the tree
300
+ //Removing the empty string removes everything in the tree
298
301
if str == " " {
299
302
for c in root. children {
300
303
c. erase ( )
@@ -306,6 +309,7 @@ class RadixTree {
306
309
else if root. children. count == 0 {
307
310
return false
308
311
}
312
+ //searchStr and currEdge have the same functionality as in insert() and find()
309
313
var searchStr = str
310
314
var currEdge = root
311
315
while ( true ) {
@@ -314,6 +318,7 @@ class RadixTree {
314
318
if currEdge. children. count == 0 {
315
319
return false
316
320
}
321
+ //Loop through the children
317
322
for c in 0 ... currEdge. children. count- 1 {
318
323
//If the child's label matches the search string, remove that child
319
324
// and everything below it in the tree
@@ -343,6 +348,7 @@ class RadixTree {
343
348
}
344
349
}
345
350
351
+ //Prints the tree by calling the root's print function
346
352
func printTree( ) {
347
353
root. printRoot ( )
348
354
}
0 commit comments