|
| 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 | + |
| 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* |
0 commit comments