Skip to content

Commit a2f4de8

Browse files
committed
Merge pull request kodecocodes#105 from scalls/master
Radix Tree
2 parents 3a7f20d + 2f209a3 commit a2f4de8

File tree

5 files changed

+476
-0
lines changed

5 files changed

+476
-0
lines changed

README.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +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:
155156

156157
### Hashing
157158

Radix-Tree/Images/radixtree.png

23.5 KB
Loading

Radix-Tree/README.markdown

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Radix Tree
2+
3+
A radix tree is a 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 and by traversing from the root to any leaf in the tree, (by concatenating all the labels of edges along the way) you can find any string. This is a radix tree:
4+
![](/Radix-Tree/Images/radixtree.png)
5+
(Image from en.wikipedia.org)
6+
7+
## Implemenation
8+
9+
The RadixTree class itself has one member variable, root, of type Root. The Root is the top of every RadixTree. Every node that is added, removed, looked up, etc. is a child, a child of a child, etc. of that root. The rest of the tree is represented as an Edge, which inherits Root. The different between Edge and Root is that Edges have a label (of type String) and a parent (of type Root?) member variables. 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).
10+
11+
## Operations
12+
13+
Typical operations on a radix tree include lookup, insertion, deletion, find predecessor, find successor, and find all strings with common prefix. 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(logn) time where n is the number of nodes in the tree.
14+
15+
### RadixTree.find(_ str: String) -> Bool
16+
17+
The find function returns true if the String you are searching for is in the tree and false if it is not. Every RadixTree contains at the very least one String ("") so find("") will always return true. A String is considered "in the tree" if the String you are searching for is a concatenation of Edge labels while traversing downwards or a prefix of that concatenation. 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". On the other hand, find("romans") will return false.
18+
19+
### RadixTree.insert(_ str: String) -> Bool
20+
21+
The insert 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. Similarly to find(""), insert("") will always return false because an empty RadixTree contains "" by default.
22+
23+
### RadixTree.remove(_ str: String) -> Bool
24+
25+
The remove function returns true if the String is removed and false if the String is not in the tree. When a string is removed, any other Strings that have a prefix of the removed String are removed as well. 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.
26+
27+
### Root.level() -> Int
28+
29+
The level function returns how far down in the tree the Root or Edge in question is. A Root object will always return 0. A child of the Root will return 1, a child of a child of the root will return 2, etc.
30+
31+
### RadixTree.height() -> Int
32+
33+
The height function returns an Int equal to the highest level in the Tree. An empty tree will return 0.
34+
35+
### RadixTree.printTree()
36+
37+
The printTree function was used to visualize the tree itself. This function is a little buggy and not perfect yet but gets the job done thus far.
38+
39+
### sharedPrefix(_ str1: String, _ str2: String) -> String
40+
41+
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. For example,
42+
sharedPrefix("apples", "oranges") will return "" and sharedPrefix("court", "coral") will return "co". This function is used in the implementation of the find, insert, and remove functions.
43+
44+
## See Also
45+
46+
[Radix Tree - Wikipedia](https://en.wikipedia.org/wiki/Radix_tree)

0 commit comments

Comments
 (0)