|
8 | 8 |
|
9 | 9 | import Foundation
|
10 | 10 |
|
11 |
| - |
| 11 | +/** |
| 12 | +The Ternary Search Tree (TST) Data structure. |
| 13 | +Data structure uses key-value mappings. Keys are strings used to map to any data element. |
| 14 | +See README for more information. |
| 15 | +*/ |
12 | 16 | public class TernarySearchTree<Element> {
|
13 | 17 |
|
| 18 | + /// A reference to the root node of this TST |
14 | 19 | var root: TSTNode<Element>?
|
15 | 20 |
|
| 21 | + /** |
| 22 | + Standard initializer |
| 23 | + */ |
16 | 24 | public init() {}
|
17 | 25 |
|
18 | 26 | //MARK: - Insertion
|
19 | 27 |
|
| 28 | + /** |
| 29 | + Public insertion method. |
| 30 | + |
| 31 | + - parameter data: The value to store in this TST. |
| 32 | + - parameter key: The key to associate with this value. |
| 33 | + |
| 34 | + - returns: Value indicating insertion success/failure. |
| 35 | + */ |
20 | 36 | public func insert(data:Element, withKey key: String) -> Bool{
|
21 | 37 | return insertNode(&root, withData: data, andKey: key, atIndex: 0)
|
22 |
| - |
23 | 38 | }
|
24 | 39 |
|
| 40 | + /** |
| 41 | + Helper method for insertion that does the actual legwork. Insertion is performed recursively. |
| 42 | + |
| 43 | + - parameter aNode: The current node to insert below. |
| 44 | + - parameter data: The data being inserted. |
| 45 | + - parameter key: The key being used to find an insertion ___location for the given data |
| 46 | + - parameter charIndex: The index of the character in the key string to use to for the next node. |
| 47 | + |
| 48 | + - returns: Value indicating insertion success/failure. |
| 49 | + */ |
25 | 50 | private func insertNode(inout aNode: TSTNode<Element>?, withData data: Element, andKey key: String, atIndex charIndex: Int) -> Bool {
|
26 | 51 |
|
27 | 52 | //sanity check.
|
@@ -62,12 +87,26 @@ public class TernarySearchTree<Element> {
|
62 | 87 |
|
63 | 88 | //MARK: - Finding
|
64 | 89 |
|
| 90 | + /** |
| 91 | + Public find method. |
| 92 | + |
| 93 | + - parameter key: Search for an object associated with this key. |
65 | 94 |
|
| 95 | + - returns: The element, if found. Otherwise, nil. |
| 96 | + */ |
66 | 97 | public func find(key:String) -> Element? {
|
67 | 98 | return findNode(root, withKey: key, atIndex: 0)
|
68 | 99 | }
|
69 | 100 |
|
| 101 | + /** |
| 102 | + Helper method that performs actual legwork of find operation. Implemented recursively. |
70 | 103 |
|
| 104 | + - parameter aNode: The current node being evaluated. |
| 105 | + - parameter key: The key being used for the search. |
| 106 | + - parameter charIndex: The index of the current char in the search key |
| 107 | + |
| 108 | + - returns: The element, if found. Nil otherwise. |
| 109 | + */ |
71 | 110 | private func findNode(aNode: TSTNode<Element>?, withKey key: String, atIndex charIndex: Int) -> Element? {
|
72 | 111 |
|
73 | 112 | //Given key does not exist in tree.
|
|
0 commit comments