|
1 | 1 | # Trie
|
2 | 2 |
|
3 |
| -Under Construction. |
| 3 | +##What is a Trie? |
| 4 | +A trie (also known as a prefix tree, or radix tree in some other (but different) implementations) is a special type of tree used to store associative data structures where the key item is normally of type String. Each node in the trie is typically not associated with a value containing strictly itself, but more so is linked to some common prefix that precedes it in levels above it. Oftentimes, true key-value pairs are associated with the leaves of the trie, but they are not limited to this. |
| 5 | + |
| 6 | +##Why a Trie? |
| 7 | +Tries are very useful simply for the fact that it has some advantages over other data structures, like the binary tree or a hash map. These advantages include: |
| 8 | +* Looking up keys is typically faster in the worst case when compared to other data structures. |
| 9 | +* Unlike a hash map, a trie need not worry about key collisions |
| 10 | +* No need for hasing, as each key will have a unique path in the trie |
| 11 | +* Tries, by implementation, can be by default alphabetically ordered. |
| 12 | + |
| 13 | + |
| 14 | +##Common Algorithms |
| 15 | + |
| 16 | +###Find (or any general lookup function) |
| 17 | +Tries make looking up keys a trivial task, as all one has to do is walk over the nodes until we either hit a null reference or we find the key in question. |
| 18 | + |
| 19 | +The algorithm would be as follows: |
| 20 | +``` |
| 21 | + let node be the root of the trie |
| 22 | + |
| 23 | + for each character in the key |
| 24 | + if the child of node with value character is null |
| 25 | + return false (key doesn't exist in trie) |
| 26 | + else |
| 27 | + node = child of node with value character (move to the next node) |
| 28 | + return true (key exists in trie and was found |
| 29 | +``` |
| 30 | + |
| 31 | +And in swift: |
| 32 | +```swift |
| 33 | +func find(key: String) -> (node: Node?, found: Bool) { |
| 34 | + var currentNode = self.root |
| 35 | + |
| 36 | + for c in key.characters { |
| 37 | + if currentNode.children[String(c)] == nil { |
| 38 | + return(nil, false) |
| 39 | + } |
| 40 | + currentNode = currentNode.children[String(c)]! |
| 41 | + } |
| 42 | + |
| 43 | + return(currentNode, currentNode.isValidWord()) |
| 44 | + } |
| 45 | +``` |
| 46 | + |
| 47 | +###Insertion |
| 48 | +Insertion is also a trivial task with a Trie, as all one needs to do is walk over the nodes until we either halt on a node that we must mark as a key, or we reach a point where we need to add extra nodes to represent it. |
| 49 | + |
| 50 | +Let's walk through the algorithm: |
| 51 | + |
| 52 | +``` |
| 53 | + let S be the root node of our tree |
| 54 | + let word be an array of characters of the input key |
| 55 | + let length be the length of the key |
| 56 | + |
| 57 | + |
| 58 | + find(key) |
| 59 | + if the key was found |
| 60 | + return false |
| 61 | + else |
| 62 | + |
| 63 | + for each character in key |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | +``` |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | +` |
4 | 74 |
|
5 | 75 | See also [Wikipedia](https://en.wikipedia.org/wiki/Trie).
|
6 | 76 |
|
|
0 commit comments