Skip to content

Commit 993812b

Browse files
committed
Added Documentation.
1 parent 4965c9c commit 993812b

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

Ternary Search Tree/TST.playground/Contents.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ for _ in (1...testCount) {
6262

6363
for aTest in testNums {
6464
let data = treeOfInts.find(aTest.key)
65-
6665
if data == nil {
6766
print("TEST FAILED. Key: \(aTest.key) Data: \(aTest.data)")
6867
}

Ternary Search Tree/TernarySearchTree.swift

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,45 @@
88

99
import Foundation
1010

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+
*/
1216
public class TernarySearchTree<Element> {
1317

18+
/// A reference to the root node of this TST
1419
var root: TSTNode<Element>?
1520

21+
/**
22+
Standard initializer
23+
*/
1624
public init() {}
1725

1826
//MARK: - Insertion
1927

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+
*/
2036
public func insert(data:Element, withKey key: String) -> Bool{
2137
return insertNode(&root, withData: data, andKey: key, atIndex: 0)
22-
2338
}
2439

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+
*/
2550
private func insertNode(inout aNode: TSTNode<Element>?, withData data: Element, andKey key: String, atIndex charIndex: Int) -> Bool {
2651

2752
//sanity check.
@@ -62,12 +87,26 @@ public class TernarySearchTree<Element> {
6287

6388
//MARK: - Finding
6489

90+
/**
91+
Public find method.
92+
93+
- parameter key: Search for an object associated with this key.
6594

95+
- returns: The element, if found. Otherwise, nil.
96+
*/
6697
public func find(key:String) -> Element? {
6798
return findNode(root, withKey: key, atIndex: 0)
6899
}
69100

101+
/**
102+
Helper method that performs actual legwork of find operation. Implemented recursively.
70103

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+
*/
71110
private func findNode(aNode: TSTNode<Element>?, withKey key: String, atIndex charIndex: Int) -> Element? {
72111

73112
//Given key does not exist in tree.

0 commit comments

Comments
 (0)