Skip to content

Commit 42e259f

Browse files
committed
Added remove() to the RadixTree class
1 parent a6d58ec commit 42e259f

File tree

2 files changed

+86
-9
lines changed

2 files changed

+86
-9
lines changed

Radix-Tree/RadixTree.swift

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ class Edge: Root {
7171
}
7272
}
7373

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+
7486
func printEdge() {
7587
if children.count > 1 {
7688
for c in 0...children.count/2-1 {
@@ -238,6 +250,10 @@ class RadixTree {
238250
else if shared.characters.count == 0 {
239251
continue
240252
}
253+
//If the shared string matches the search string, return true
254+
else if shared == searchStr {
255+
return true
256+
}
241257
//If the search string and the child's label only share some characters,
242258
// the string is not in the tree, return false
243259
else if shared[shared.startIndex] == c.label[c.label.startIndex] &&
@@ -249,13 +265,58 @@ class RadixTree {
249265
return false
250266
}
251267
}
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+
}
257269

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+
}
259320
}
260321

261322
func printTree() {

Radix-Tree/main.swift

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@ radixWiki.insert("rubicunduses")
2929
radixWiki.printTree()
3030

3131
print("FIND TESTS")
32-
print(radixWiki.find("courts"))
33-
print(radixWiki.find("rubicunduses"))
34-
print(radixWiki.find(""))
32+
print(radixWiki.find("courts")) // false
33+
print(radixWiki.find("r")) // true
34+
print(radixWiki.find("ro")) // true
35+
print(radixWiki.find("rom")) // true
36+
print(radixWiki.find("roma")) // true
37+
print(radixWiki.find("roman")) // true
38+
print(radixWiki.find("romane")) // true
39+
print(radixWiki.find("romans")) // false
40+
print(radixWiki.find("steve")) // true
41+
42+
print("REMOVE TESTS")
43+
44+
print(radixWiki.remove("c"))
45+
radixWiki.printTree()
46+
47+
print(radixWiki.remove("rub"))
48+
radixWiki.printTree()
49+
50+
print(radixWiki.remove("stevenson"))

0 commit comments

Comments
 (0)