Skip to content

Commit 7b1a045

Browse files
committed
Added find() to RadixTree class
1 parent cf7c087 commit 7b1a045

File tree

2 files changed

+70
-45
lines changed

2 files changed

+70
-45
lines changed

Radix-Tree/RadixTree.swift

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class RadixTree {
124124
func insert(_ str: String) -> Bool {
125125
//Account for a blank input
126126
if str == "" {
127-
return true
127+
return false
128128
}
129129
//Account for an empty tree
130130
if root.children.count == 0 {
@@ -135,7 +135,6 @@ class RadixTree {
135135
var currEdge = root
136136
while (true) {
137137
var found = false
138-
var i = 0
139138
if currEdge.children.count == 0 {
140139
let newEdge = Edge(searchStr)
141140
currEdge.children.append(newEdge)
@@ -190,7 +189,6 @@ class RadixTree {
190189
return true
191190
}
192191
//They don't share a prefix (go to next child)
193-
i += 1
194192
}
195193
if (!found) {
196194
//No children share a prefix, so create a new child
@@ -202,6 +200,64 @@ class RadixTree {
202200
}
203201
}
204202

203+
func find(_ str: String) -> Bool {
204+
//A radix tree always contains the empty string
205+
if str == "" {
206+
return true
207+
}
208+
//If there are no children then the string cannot be in the tree
209+
else if root.children.count == 0 {
210+
return false
211+
}
212+
var searchStr = str
213+
var currEdge = root
214+
while (true) {
215+
var found = false
216+
for c in currEdge.children {
217+
//First check if the search string and the child's label are equal
218+
// if so the string is in the tree, return true
219+
if searchStr == c.label {
220+
return true
221+
}
222+
//If that is not true, find the shared string b/t the search string
223+
// and the label
224+
var shared = sharedPrefix(searchStr, c.label)
225+
//If the shared string is equal to the label, update the curent node
226+
// and run it back
227+
if shared == c.label {
228+
currEdge = c
229+
var tempIndex = searchStr.startIndex
230+
for _ in 1...shared.characters.count {
231+
tempIndex = tempIndex.successor()
232+
}
233+
searchStr = searchStr.substringFromIndex(tempIndex)
234+
found = true
235+
break
236+
}
237+
//If the shared string is empty, go to the next children
238+
else if shared.characters.count == 0 {
239+
continue
240+
}
241+
//If the search string and the child's label only share some characters,
242+
// the string is not in the tree, return false
243+
else if shared[shared.startIndex] == c.label[c.label.startIndex] &&
244+
shared.characters.count < c.label.characters.count {
245+
return false
246+
}
247+
}
248+
if !found {
249+
return false
250+
}
251+
}
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
257+
258+
//If none of the children's labels are a prefix of the search string
259+
}
260+
205261
func printTree() {
206262
root.printRoot()
207263
}

Radix-Tree/main.swift

Lines changed: 11 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,13 @@
11
var radix = RadixTree()
2-
var radix2 = RadixTree()
32
var radixWiki = RadixTree()
43

5-
print("-------------------------------------------------")
6-
radix.printTree()
7-
print("--------------------------------------------------")
8-
radix.insert( "courting" )
9-
radix.printTree()
10-
print("-------------------------------------------------")
11-
radix.insert( "coral" )
12-
radix.printTree()
13-
print("--------------------------------------------------")
14-
radix.insert( "courted" )
15-
radix.printTree()
16-
print("---------------------------------------------------")
17-
print( radix.insert( "corals") )
18-
radix.printTree()
19-
20-
print( radixWiki.insert("romanus") )
21-
print( radixWiki.insert("rubicundus") )
22-
print( radixWiki.insert("rubicon") )
23-
print( radixWiki.insert("romane") )
24-
print( radixWiki.insert("ruber") )
25-
print( radixWiki.insert("rubens") )
26-
print( radixWiki.insert("romulus") )
4+
radixWiki.insert("romanus")
5+
radixWiki.insert("rubicundus")
6+
radixWiki.insert("rubicon")
7+
radixWiki.insert("romane")
8+
radixWiki.insert("ruber")
9+
radixWiki.insert("rubens")
10+
radixWiki.insert("romulus")
2711
radixWiki.insert("start")
2812
radixWiki.insert("shoot")
2913
radixWiki.insert("shit")
@@ -42,24 +26,9 @@ radixWiki.insert("courted")
4226

4327
radixWiki.insert("rubicunduses")
4428

45-
print("PRINT TEST-------------------------------")
46-
radixWiki.printTree()
47-
48-
let h = radix.height()
49-
let h2 = radix2.height()
50-
let hwiki = radixWiki.height()
51-
print("Height of tree: \(h)")
52-
print("height of second tree: \(h2)")
53-
print("Height of wikipedia tree: \(hwiki)")
54-
55-
print(radixWiki.root.level())
56-
print(radixWiki.root.children[0].level())
57-
print(radixWiki.root.children[0].children[0].level())
58-
print(radixWiki.root.children[0].children[0].children[0].level())
59-
60-
print("\t\t\t\t\t\ttest")
61-
print("PRINT TEST-------------------------------")
6229
radixWiki.printTree()
6330

64-
print("PRINT TEST-------------------------------")
65-
radix.printTree()
31+
print("FIND TESTS")
32+
print(radixWiki.find("courts"))
33+
print(radixWiki.find("rubicunduses"))
34+
print(radixWiki.find(""))

0 commit comments

Comments
 (0)