@@ -33,52 +33,52 @@ public class TernarySearchTree<Element> {
3333
3434 - returns: Value indicating insertion success/failure.
3535 */
36- public func insert( data: Element , withKey key: String ) -> Bool {
37- return insertNode ( & root, withData: data, andKey: key, atIndex: 0 )
36+ @ discardableResult public func insert( data: Element , withKey key: String ) -> Bool {
37+ return insert ( node : & root, withData: data, andKey: key, atIndex: 0 )
3838 }
3939
4040 /**
4141 Helper method for insertion that does the actual legwork. Insertion is performed recursively.
4242
43- - parameter aNode: The current node to insert below.
43+ - parameter node: The current node to insert below.
4444 - parameter data: The data being inserted.
4545 - parameter key: The key being used to find an insertion location for the given data
4646 - parameter charIndex: The index of the character in the key string to use to for the next node.
4747
4848 - returns: Value indicating insertion success/failure.
4949 */
50- private func insertNode ( inout aNode : TSTNode < Element > ? , withData data: Element , andKey key: String , atIndex charIndex: Int ) -> Bool {
50+ private func insert ( node : inout TSTNode < Element > ? , withData data: Element , andKey key: String , atIndex charIndex: Int ) -> Bool {
5151
5252 //sanity check.
53- if key. characters. count == 0 {
53+ guard key. characters. count > 0 else {
5454 return false
5555 }
5656
5757 //create a new node if necessary.
58- if aNode == nil {
59- let index = key. startIndex . advancedBy ( charIndex)
60- aNode = TSTNode < Element > ( key: key [ index] )
58+ if node == nil {
59+ let index = key. index ( key . startIndex , offsetBy : charIndex)
60+ node = TSTNode < Element > ( key: key [ index] )
6161 }
6262
6363 //if current char is less than the current node's char, go left
64- let index = key. startIndex . advancedBy ( charIndex)
65- if key [ index] < aNode !. key {
66- return insertNode ( & aNode !. left, withData: data, andKey: key, atIndex: charIndex)
64+ let index = key. index ( key . startIndex , offsetBy : charIndex)
65+ if key [ index] < node !. key {
66+ return insert ( node : & node !. left, withData: data, andKey: key, atIndex: charIndex)
6767 }
6868 //if it's greater, go right.
69- else if key [ index] > aNode !. key {
70- return insertNode ( & aNode !. right, withData: data, andKey: key, atIndex: charIndex)
69+ else if key [ index] > node !. key {
70+ return insert ( node : & node !. right, withData: data, andKey: key, atIndex: charIndex)
7171 }
7272 //current char is equal to the current nodes, go middle
7373 else {
7474 //continue down the middle.
7575 if charIndex + 1 < key. characters. count {
76- return insertNode ( & aNode !. middle, withData: data, andKey: key, atIndex: charIndex + 1 )
76+ return insert ( node : & node !. middle, withData: data, andKey: key, atIndex: charIndex + 1 )
7777 }
7878 //otherwise, all done.
7979 else {
80- aNode !. data = data
81- aNode ? . hasData = true
80+ node !. data = data
81+ node ? . hasData = true
8282 return true
8383 }
8484 }
@@ -95,40 +95,40 @@ public class TernarySearchTree<Element> {
9595 - returns: The element, if found. Otherwise, nil.
9696 */
9797 public func find( key: String ) -> Element ? {
98- return findNode ( root, withKey: key, atIndex: 0 )
98+ return find ( node : root, withKey: key, atIndex: 0 )
9999 }
100100
101101 /**
102102 Helper method that performs actual legwork of find operation. Implemented recursively.
103103
104- - parameter aNode: The current node being evaluated.
104+ - parameter node: The current node being evaluated.
105105 - parameter key: The key being used for the search.
106106 - parameter charIndex: The index of the current char in the search key
107107
108108 - returns: The element, if found. Nil otherwise.
109109 */
110- private func findNode ( aNode : TSTNode < Element > ? , withKey key: String , atIndex charIndex: Int ) -> Element ? {
110+ private func find ( node : TSTNode < Element > ? , withKey key: String , atIndex charIndex: Int ) -> Element ? {
111111
112112 //Given key does not exist in tree.
113- if aNode == nil {
113+ guard let node = node else {
114114 return nil
115115 }
116116
117- let index = key. startIndex . advancedBy ( charIndex)
117+ let index = key. index ( key . startIndex , offsetBy : charIndex)
118118 //go left
119- if key [ index] < aNode! . key {
120- return findNode ( aNode! . left, withKey: key, atIndex: charIndex)
119+ if key [ index] < node . key {
120+ return find ( node : node . left, withKey: key, atIndex: charIndex)
121121 }
122122 //go right
123- else if key [ index] > aNode! . key {
124- return findNode ( aNode! . right, withKey: key, atIndex: charIndex)
123+ else if key [ index] > node . key {
124+ return find ( node : node . right, withKey: key, atIndex: charIndex)
125125 }
126126 //go middle
127127 else {
128128 if charIndex + 1 < key. characters. count {
129- return findNode ( aNode! . middle, withKey: key, atIndex: charIndex + 1 )
129+ return find ( node : node . middle, withKey: key, atIndex: charIndex + 1 )
130130 } else {
131- return aNode! . data
131+ return node . data
132132 }
133133 }
134134 }
0 commit comments