Skip to content

Commit bcf9ade

Browse files
committed
Added more inline comments
1 parent efe4d94 commit bcf9ade

File tree

2 files changed

+52
-19
lines changed

2 files changed

+52
-19
lines changed

Radix-Tree/RadixTree.swift

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Foundation
22

3+
//The root is the top of the Radix Tree
34
class Root {
45

56
var children: [Edge]
@@ -8,10 +9,14 @@ class Root {
89
children = [Edge]()
910
}
1011

12+
//The height functions returns the length (in number of edges) of the longest
13+
// traversal down the tree
1114
func height() -> Int {
15+
//Base case: no children: the tree has a height of 1
1216
if children.count == 0 {
1317
return 1
1418
}
19+
//Recursion: find the max height of a root's child and return 1 + that max
1520
else {
1621
var max = 1
1722
for c in children {
@@ -23,11 +28,16 @@ class Root {
2328
}
2429
}
2530

31+
//The level function returns how far down in the tree a Root/Edge is
32+
//A root's level is always 0
2633
func level() -> Int {
2734
return 0
2835
}
2936

37+
//printRoot is a recursive function that prints the tree for debugging/
38+
// visualization purposes
3039
func printRoot() {
40+
//Print the first half of the children
3141
if (children.count > 1) {
3242
for c in 0...children.count/2-1 {
3343
children[c].printEdge()
@@ -37,7 +47,9 @@ class Root {
3747
else if children.count > 0 {
3848
children[0].printEdge()
3949
}
50+
//Then print the root
4051
print("ROOT")
52+
//Print the second half of the children
4153
if children.count > 1 {
4254
for c in children.count/2...children.count-1 {
4355
children[c].printEdge()
@@ -48,6 +60,7 @@ class Root {
4860
}
4961
}
5062

63+
//Edges are what actually store the Strings in the tree
5164
class Edge: Root {
5265

5366
var parent: Root?
@@ -58,27 +71,36 @@ class Edge: Root {
5871
super.init()
5972
}
6073

74+
//The Edge class overrides root's level function
6175
override
6276
func level() -> Int {
77+
//Recurse up the tree incrementing level by one until the root is reached
6378
if parent != nil {
6479
return 1 + parent!.level()
6580
}
81+
//If an edge has no parent, it's level is one
82+
// NOTE: THIS SHOULD NEVER HAPPEN AS THE ROOT IS ALWAYS THE TOP OF THE TREE
6683
else {
6784
return 1
6885
}
6986
}
7087

88+
//Erase erases a specific edge (and all edges below it in the tree)
7189
func erase() {
7290
self.parent = nil
7391
if children.count > 0 {
92+
//For each child, erase it, then remove it from the children array
7493
for _ in 0...children.count-1 {
7594
children[0].erase()
7695
children.remove(at: 0)
7796
}
7897
}
7998
}
8099

100+
//printEdge is used in printRoot to print the tree
101+
// It follows a similar structure to printRoot
81102
func printEdge() {
103+
//Print the first half of the edge's children
82104
if children.count > 1 {
83105
for c in 0...children.count/2-1 {
84106
children[c].printEdge()
@@ -87,21 +109,17 @@ class Edge: Root {
87109
else if children.count > 0 {
88110
children[0].printEdge()
89111
}
112+
//Tab over once up to the edge's level
90113
for x in 1...level() {
91114
if x == level() {
92115
print("|------>", terminator: "")
93116
}
94-
else if x == 1 {
95-
print("| ", terminator: "")
96-
}
97-
else if x == level()-1 {
98-
print("| ", terminator: "")
99-
}
100117
else {
101118
print("| ", terminator: "")
102119
}
103120
}
104121
print(label)
122+
//Print the second half of the edge's children
105123
if children.count == 0 {
106124
for _ in 1...level() {
107125
print("| ", terminator: "")
@@ -124,38 +142,47 @@ class RadixTree {
124142
root = Root()
125143
}
126144

145+
//Returns the height of the tree by calling the root's height function
127146
func height() -> Int {
128147
return root.height() - 1
129148
}
130149

150+
//Inserts a string into the tree
131151
func insert(_ str: String) -> Bool {
132152
//Account for a blank input
153+
// The empty string is already in the tree
133154
if str == "" {
134155
return false
135156
}
136-
//Account for an empty tree
137-
if root.children.count == 0 {
138-
root.children.append( Edge(str) )
139-
return true
140-
}
157+
//searchStr is the parameter of the function
158+
// it will be substringed as the function traverses down the tree
141159
var searchStr = str
160+
//currEdge is the current Edge (or Root) in question
142161
var currEdge = root
143162
while (true) {
144163
var found = false
164+
//If the current Edge has no children then the remaining searchStr is
165+
// created as a child
145166
if currEdge.children.count == 0 {
146167
let newEdge = Edge(searchStr)
147168
currEdge.children.append(newEdge)
148169
newEdge.parent = currEdge
170+
return true
149171
}
172+
//Loop through all of the children
150173
for e in currEdge.children {
151-
//Get the shared
174+
//Get the shared prefix betweeen the child in question and the
175+
// search string
152176
var shared = sharedPrefix(searchStr, e.label)
153177
var index = shared.startIndex
154-
//The search string is equal to the shared string
155-
//so the string already exists in the tree
178+
//If the search string is equal to the shared string,
179+
// the string already exists in the tree
156180
if searchStr == shared {
157181
return false
158182
}
183+
//If the child's label is equal to the shared string, you have to
184+
// traverse another level down the tree, so substring the search
185+
// string, break the loop, and run it back
159186
else if shared == e.label {
160187
currEdge = e
161188
var tempIndex = searchStr.startIndex
@@ -166,16 +193,18 @@ class RadixTree {
166193
found = true
167194
break
168195
}
169-
//The child's label and the search string share a prefix
196+
//If the child's label and the search string share a partial prefix,
197+
// then both the label and the search string need to be substringed
198+
// and a new branch needs to be created
170199
else if shared.characters.count > 0 {
171-
//Cut the prefix off from both the search string and label
172200
var labelIndex = e.label.characters.startIndex
173201
//Create index objects and move them to after the shared prefix
174202
for _ in 1...shared.characters.count {
175203
index = index.successor()
176204
labelIndex = labelIndex.successor()
177205
}
178-
//Substring both the search string and the label from the shared prefix
206+
//Substring both the search string and the label from the
207+
// shared prefix
179208
searchStr = searchStr.substringFromIndex(index)
180209
e.label = e.label.substringFromIndex(labelIndex)
181210
//Create 2 new edges and update parent/children values
@@ -195,10 +224,10 @@ class RadixTree {
195224
e.children.append(newEdge2)
196225
return true
197226
}
198-
//They don't share a prefix (go to next child)
227+
//If they don't share a prefix, go to next child
199228
}
229+
//If none of the children share a prefix, you have to create a new child
200230
if (!found) {
201-
//No children share a prefix, so create a new child
202231
let newEdge = Edge(searchStr)
203232
currEdge.children.append(newEdge)
204233
newEdge.parent = currEdge
@@ -207,6 +236,7 @@ class RadixTree {
207236
}
208237
}
209238

239+
//Tells you if a string is in the tree
210240
func find(_ str: String) -> Bool {
211241
//A radix tree always contains the empty string
212242
if str == "" {
@@ -216,6 +246,7 @@ class RadixTree {
216246
else if root.children.count == 0 {
217247
return false
218248
}
249+
//searchStr and currEdge have the same functionality as insert()
219250
var searchStr = str
220251
var currEdge = root
221252
while (true) {

Radix-Tree/main.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ radixWiki.insert("shoot")
1313
radixWiki.insert("shit")
1414
radixWiki.insert("starch")
1515
radixWiki.insert("steven")
16+
radixWiki.insert("shart")
17+
radixWiki.insert("shard")
1618

1719
radixWiki.insert("compute")
1820
radixWiki.insert("compatible")

0 commit comments

Comments
 (0)