Skip to content

Commit fc76baf

Browse files
committed
Cleanup of text and code
We're not using Swift 3 yet. ;-)
1 parent a2f4de8 commit fc76baf

File tree

9 files changed

+850
-424
lines changed

9 files changed

+850
-424
lines changed

README.markdown

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ Most of the time using just the built-in `Array`, `Dictionary`, and `Set` types
152152
- Fibonacci Heap
153153
- Trie
154154
- [B-Tree](B Tree/) :construction:
155-
- [Radix Tree](Radix-Tree/) :construction:
155+
- [Radix Tree](Radix Tree/) :construction:
156156

157157
### Hashing
158158

File renamed without changes.

Radix Tree/README.markdown

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Radix Tree
2+
3+
A radix tree is a [tree](../Tree/) where a node can have any number of children. Each edge leading from a node to a child has a label (usually a string). A radix tree is often used to store strings or IP addresses. By traversing from the root to any leaf in the tree, concatenating all the labels of edges along the way, you can find any string.
4+
5+
This is an example of a radix tree (image from wikipedia.org):
6+
7+
![](Images/radixtree.png)
8+
9+
## Implemenation
10+
11+
The radix tree is represented by a `RadixTree` object. This class has one member variable, `root`, of type `Root`. The `Root` is the object at the top of every `RadixTree`.
12+
13+
Every other node is of type `Edge` and is a child (or child of a child, etc.) of the root.
14+
15+
The difference between `Edge` and `Root` is that edges have a label (of type `String`) and a reference to a parent node.
16+
17+
This approach is a little different from the conventional way of creating tree data structures but is easier to wrap your head around (see the picture above).
18+
19+
## Operations
20+
21+
Typical operations on a radix tree are:
22+
23+
- lookup
24+
- insertion
25+
- deletion
26+
- find predecessor
27+
- find successor
28+
- find all strings with common prefix
29+
30+
The running time of lookup, insertion, and deletion is **O(k)** where **k** is the length of the key. This is different from most trees because these operations usually run in **O(log n)** time where **n** is the number of nodes in the tree.
31+
32+
### Lookup
33+
34+
The `find()` function returns true if the string you are searching for is in the tree and false if it is not.
35+
36+
Every `RadixTree` contains at the very least the empty string `""` so `find("")` will always return true.
37+
38+
A string is considered "in the tree" if the text you are searching for is a concatenation of `Edge` labels while traversing downwards, or a prefix of that concatenation.
39+
40+
For example, if you look at the example tree above, `find("roma")` will return true because it is a prefix of the traversal `"r" -> "om" -> "an"`.
41+
42+
On the other hand, `find("romans")` will return false.
43+
44+
### Insertion
45+
46+
The `insert()` function lets you add new strings to the radix tree.
47+
48+
This function returns true if the string you are trying to insert was successfully inserted into the `RadixTree` and false if the string is already in the tree.
49+
50+
### Deletion
51+
52+
The `remove()` removes a string from the tree. When a string is removed, any other strings that have a prefix of the removed string are removed as well.
53+
54+
For example, `remove("rom")` will also remove `"roman"`, `"rome"`, and `"romulus"` if those strings are in the tree as well. Calling `remove("")` will remove all strings in the tree.
55+
56+
### printTree()
57+
58+
You can use the `printTree()` function to visualize the tree. This function is a little buggy and not perfect yet but gets the job done thus far.
59+
60+
### Helper functions
61+
62+
The `sharedPrefix()` function is a non-member function in the **RadixTree.swift** file. It takes in two `String` objects and returns the shared prefix between them.
63+
64+
For example, `sharedPrefix("apples", "oranges")` will return `""`, and `sharedPrefix("court", "coral")` will return `"co"`.
65+
66+
This function is used in the implementation of the `find()`, `insert()`, and `remove()` functions.
67+
68+
## See also
69+
70+
[Radix Tree - Wikipedia](https://en.wikipedia.org/wiki/Radix_tree)
71+
72+
*Written for Swift Algorithm Club by Steven Scally*

Radix-Tree/main.swift renamed to Radix Tree/RadixTree.playground/Contents.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ radixWiki.insert("rubicunduses")
3030

3131
radixWiki.printTree()
3232

33-
print("FIND TESTS")
33+
print("\n\nFIND TESTS")
3434
print(radixWiki.find("courts")) // false
3535
print(radixWiki.find("r")) // true
3636
print(radixWiki.find("ro")) // true
@@ -41,7 +41,7 @@ print(radixWiki.find("romane")) // true
4141
print(radixWiki.find("romans")) // false
4242
print(radixWiki.find("steve")) // true
4343

44-
print("REMOVE TESTS")
44+
print("\n\nREMOVE TESTS")
4545

4646
print(radixWiki.remove("c"))
4747
radixWiki.printTree()
@@ -52,4 +52,4 @@ radixWiki.printTree()
5252
print(radixWiki.remove("stevenson"))
5353

5454
print(radixWiki.remove(""))
55-
radixWiki.printTree()
55+
radixWiki.printTree()

0 commit comments

Comments
 (0)