@@ -71,6 +71,18 @@ class Edge: Root {
71
71
}
72
72
}
73
73
74
+ func erase( ) {
75
+ print ( " Testing erase on: \( label) " )
76
+ self . parent = nil
77
+ if children. count > 0 {
78
+ for _ in 0 ... children. count- 1 {
79
+ children [ 0 ] . erase ( )
80
+ children. remove ( at: 0 )
81
+ }
82
+ }
83
+ print ( " Removed: \( label) " )
84
+ }
85
+
74
86
func printEdge( ) {
75
87
if children. count > 1 {
76
88
for c in 0 ... children. count/ 2 - 1 {
@@ -238,6 +250,10 @@ class RadixTree {
238
250
else if shared. characters. count == 0 {
239
251
continue
240
252
}
253
+ //If the shared string matches the search string, return true
254
+ else if shared == searchStr {
255
+ return true
256
+ }
241
257
//If the search string and the child's label only share some characters,
242
258
// the string is not in the tree, return false
243
259
else if shared [ shared. startIndex] == c. label [ c. label. startIndex] &&
@@ -249,13 +265,58 @@ class RadixTree {
249
265
return false
250
266
}
251
267
}
252
- //Start at the root
253
- //If a child's label is a prefix of the search string
254
- // Cut the prefix off of the search string and
255
- // start looking through that child's children
256
- //If a child's label == searchStr, return true
268
+ }
257
269
258
- //If none of the children's labels are a prefix of the search string
270
+ func remove( _ str: String ) -> Bool {
271
+ print ( " Tryna remove: \( str) " )
272
+ //You cannot remove the empty string from the tree
273
+ if str == " " {
274
+ return false
275
+ }
276
+ //If the tree is empty, you cannot remove anything
277
+ else if root. children. count == 0 {
278
+ return false
279
+ }
280
+ var searchStr = str
281
+ var currEdge = root
282
+ while ( true ) {
283
+ var found = false
284
+ print ( " Search string: \( searchStr) " )
285
+ //If currEdge has no children, then the searchStr is not in the tree
286
+ if currEdge. children. count == 0 {
287
+ return false
288
+ }
289
+ for c in 0 ... currEdge. children. count- 1 {
290
+ //If the child's label matches the search string, remove that child
291
+ // and everything below it in the tree
292
+ print ( " Looking at: \( currEdge. children [ c] . label) " )
293
+ if currEdge. children [ c] . label == searchStr {
294
+ print ( " MATCH FOUND " )
295
+ currEdge. children [ c] . erase ( )
296
+ print ( " ERASED WORKED " )
297
+ currEdge. children. remove ( at: c)
298
+ print ( " EDGE LABEL MATCH REMOVE " )
299
+ return true
300
+ }
301
+ //Find the shared string
302
+ var shared = sharedPrefix ( searchStr, currEdge. children [ c] . label)
303
+ //If the shared string is equal to the child's string, go down a level
304
+ if shared == currEdge. children [ c] . label {
305
+ currEdge = currEdge. children [ c]
306
+ var tempIndex = searchStr. startIndex
307
+ for _ in 1 ... shared. characters. count {
308
+ tempIndex = tempIndex. successor ( )
309
+ }
310
+ searchStr = searchStr. substringFromIndex ( tempIndex)
311
+ found = true
312
+ break
313
+ }
314
+ }
315
+ //If there is no match, then the searchStr is not in the tree
316
+ if !found {
317
+ return false
318
+ }
319
+ }
259
320
}
260
321
261
322
func printTree( ) {
0 commit comments