@@ -124,7 +124,7 @@ class RadixTree {
124
124
func insert( _ str: String ) -> Bool {
125
125
//Account for a blank input
126
126
if str == " " {
127
- return true
127
+ return false
128
128
}
129
129
//Account for an empty tree
130
130
if root. children. count == 0 {
@@ -135,7 +135,6 @@ class RadixTree {
135
135
var currEdge = root
136
136
while ( true ) {
137
137
var found = false
138
- var i = 0
139
138
if currEdge. children. count == 0 {
140
139
let newEdge = Edge ( searchStr)
141
140
currEdge. children. append ( newEdge)
@@ -190,7 +189,6 @@ class RadixTree {
190
189
return true
191
190
}
192
191
//They don't share a prefix (go to next child)
193
- i += 1
194
192
}
195
193
if ( !found) {
196
194
//No children share a prefix, so create a new child
@@ -202,6 +200,64 @@ class RadixTree {
202
200
}
203
201
}
204
202
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
+
205
261
func printTree( ) {
206
262
root. printRoot ( )
207
263
}
0 commit comments