1010 you should insert new values in randomized order, not in sorted order.
1111*/
1212public class BinarySearchTree < T: Comparable > {
13- private ( set) public var value : T
14- private ( set) public var parent : BinarySearchTree ?
15- private ( set) public var left : BinarySearchTree ?
16- private ( set) public var right : BinarySearchTree ?
13+ fileprivate ( set) public var value : T
14+ fileprivate ( set) public var parent : BinarySearchTree ?
15+ fileprivate ( set) public var left : BinarySearchTree ?
16+ fileprivate ( set) public var right : BinarySearchTree ?
1717
1818 public init ( value: T ) {
1919 self . value = value
@@ -22,8 +22,8 @@ public class BinarySearchTree<T: Comparable> {
2222 public convenience init ( array: [ T ] ) {
2323 precondition ( array. count > 0 )
2424 self . init ( value: array. first!)
25- for v in array. dropFirst ( ) {
26- insert ( v, parent: self )
25+ for v in array. dropFirst ( ) {
26+ insert ( value : v, parent: self )
2727 }
2828 }
2929
@@ -74,20 +74,20 @@ extension BinarySearchTree {
7474 Performance: runs in O(h) time, where h is the height of the tree.
7575 */
7676 public func insert( value: T ) {
77- insert ( value, parent: self )
77+ insert ( value: value , parent: self )
7878 }
7979
80- private func insert( value: T , parent: BinarySearchTree ) {
80+ fileprivate func insert( value: T , parent: BinarySearchTree ) {
8181 if value < self . value {
8282 if let left = left {
83- left. insert ( value, parent: left)
83+ left. insert ( value: value , parent: left)
8484 } else {
8585 left = BinarySearchTree ( value: value)
8686 left? . parent = parent
8787 }
8888 } else {
8989 if let right = right {
90- right. insert ( value, parent: right)
90+ right. insert ( value: value , parent: right)
9191 } else {
9292 right = BinarySearchTree ( value: value)
9393 right? . parent = parent
@@ -108,7 +108,7 @@ extension BinarySearchTree {
108108
109109 Performance: runs in O(h) time, where h is the height of the tree.
110110 */
111- public func remove( ) -> BinarySearchTree ? {
111+ @ discardableResult public func remove( ) -> BinarySearchTree ? {
112112 let replacement : BinarySearchTree ?
113113
114114 if let left = left {
@@ -126,7 +126,7 @@ extension BinarySearchTree {
126126 replacement = nil
127127 }
128128
129- reconnectParentToNode ( replacement)
129+ reconnectParentTo ( node : replacement)
130130
131131 // The current node is no longer part of the tree, so clean it up.
132132 parent = nil
@@ -136,7 +136,7 @@ extension BinarySearchTree {
136136 return replacement
137137 }
138138
139- private func removeNodeWithTwoChildren( left: BinarySearchTree , _ right: BinarySearchTree ) -> BinarySearchTree {
139+ private func removeNodeWithTwoChildren( _ left: BinarySearchTree , _ right: BinarySearchTree ) -> BinarySearchTree {
140140 // This node has two children. It must be replaced by the smallest
141141 // child that is larger than this node's value, which is the leftmost
142142 // descendent of the right child.
@@ -164,7 +164,7 @@ extension BinarySearchTree {
164164 return successor
165165 }
166166
167- private func reconnectParentToNode ( node: BinarySearchTree ? ) {
167+ private func reconnectParentTo ( node: BinarySearchTree ? ) {
168168 if let parent = parent {
169169 if isLeftChild {
170170 parent. left = node
@@ -211,7 +211,7 @@ extension BinarySearchTree {
211211 */
212212
213213 public func contains( value: T ) -> Bool {
214- return search ( value) != nil
214+ return search ( value: value ) != nil
215215 }
216216
217217 /*
@@ -298,32 +298,32 @@ extension BinarySearchTree {
298298// MARK: - Traversal
299299
300300extension BinarySearchTree {
301- public func traverseInOrder( @ noescape process: T -> Void ) {
302- left? . traverseInOrder ( process)
301+ public func traverseInOrder( process: ( T ) -> Void ) {
302+ left? . traverseInOrder ( process: process )
303303 process ( value)
304- right? . traverseInOrder ( process)
304+ right? . traverseInOrder ( process: process )
305305 }
306306
307- public func traversePreOrder( @ noescape process: T -> Void ) {
307+ public func traversePreOrder( process: ( T ) -> Void ) {
308308 process ( value)
309- left? . traversePreOrder ( process)
310- right? . traversePreOrder ( process)
309+ left? . traversePreOrder ( process: process )
310+ right? . traversePreOrder ( process: process )
311311 }
312312
313- public func traversePostOrder( @ noescape process: T -> Void ) {
314- left? . traversePostOrder ( process)
315- right? . traversePostOrder ( process)
313+ public func traversePostOrder( process: ( T ) -> Void ) {
314+ left? . traversePostOrder ( process: process )
315+ right? . traversePostOrder ( process: process )
316316 process ( value)
317317 }
318318
319319 /*
320320 Performs an in-order traversal and collects the results in an array.
321321 */
322- public func map( @ noescape formula: T -> T ) -> [ T ] {
322+ public func map( formula: ( T ) -> T ) -> [ T ] {
323323 var a = [ T] ( )
324- if let left = left { a += left. map ( formula) }
324+ if let left = left { a += left. map ( formula: formula ) }
325325 a. append ( formula ( value) )
326- if let right = right { a += right. map ( formula) }
326+ if let right = right { a += right. map ( formula: formula ) }
327327 return a
328328 }
329329}
@@ -332,7 +332,7 @@ extension BinarySearchTree {
332332 Is this binary tree a valid binary search tree?
333333*/
334334extension BinarySearchTree {
335- public func isBST( minValue minValue : T , maxValue: T ) -> Bool {
335+ public func isBST( minValue: T , maxValue: T ) -> Bool {
336336 if value < minValue || value > maxValue { return false }
337337 let leftBST = left? . isBST ( minValue: minValue, maxValue: value) ?? true
338338 let rightBST = right? . isBST ( minValue: value, maxValue: maxValue) ?? true
0 commit comments