10
10
you should insert new values in randomized order, not in sorted order.
11
11
*/
12
12
public 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 ?
17
17
18
18
public init ( value: T ) {
19
19
self . value = value
@@ -22,8 +22,8 @@ public class BinarySearchTree<T: Comparable> {
22
22
public convenience init ( array: [ T ] ) {
23
23
precondition ( array. count > 0 )
24
24
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 )
27
27
}
28
28
}
29
29
@@ -74,20 +74,20 @@ extension BinarySearchTree {
74
74
Performance: runs in O(h) time, where h is the height of the tree.
75
75
*/
76
76
public func insert( value: T ) {
77
- insert ( value, parent: self )
77
+ insert ( value: value , parent: self )
78
78
}
79
79
80
- private func insert( value: T , parent: BinarySearchTree ) {
80
+ fileprivate func insert( value: T , parent: BinarySearchTree ) {
81
81
if value < self . value {
82
82
if let left = left {
83
- left. insert ( value, parent: left)
83
+ left. insert ( value: value , parent: left)
84
84
} else {
85
85
left = BinarySearchTree ( value: value)
86
86
left? . parent = parent
87
87
}
88
88
} else {
89
89
if let right = right {
90
- right. insert ( value, parent: right)
90
+ right. insert ( value: value , parent: right)
91
91
} else {
92
92
right = BinarySearchTree ( value: value)
93
93
right? . parent = parent
@@ -108,7 +108,7 @@ extension BinarySearchTree {
108
108
109
109
Performance: runs in O(h) time, where h is the height of the tree.
110
110
*/
111
- public func remove( ) -> BinarySearchTree ? {
111
+ @ discardableResult public func remove( ) -> BinarySearchTree ? {
112
112
let replacement : BinarySearchTree ?
113
113
114
114
if let left = left {
@@ -126,7 +126,7 @@ extension BinarySearchTree {
126
126
replacement = nil
127
127
}
128
128
129
- reconnectParentToNode ( replacement)
129
+ reconnectParentTo ( node : replacement)
130
130
131
131
// The current node is no longer part of the tree, so clean it up.
132
132
parent = nil
@@ -136,7 +136,7 @@ extension BinarySearchTree {
136
136
return replacement
137
137
}
138
138
139
- private func removeNodeWithTwoChildren( left: BinarySearchTree , _ right: BinarySearchTree ) -> BinarySearchTree {
139
+ private func removeNodeWithTwoChildren( _ left: BinarySearchTree , _ right: BinarySearchTree ) -> BinarySearchTree {
140
140
// This node has two children. It must be replaced by the smallest
141
141
// child that is larger than this node's value, which is the leftmost
142
142
// descendent of the right child.
@@ -164,7 +164,7 @@ extension BinarySearchTree {
164
164
return successor
165
165
}
166
166
167
- private func reconnectParentToNode ( node: BinarySearchTree ? ) {
167
+ private func reconnectParentTo ( node: BinarySearchTree ? ) {
168
168
if let parent = parent {
169
169
if isLeftChild {
170
170
parent. left = node
@@ -211,7 +211,7 @@ extension BinarySearchTree {
211
211
*/
212
212
213
213
public func contains( value: T ) -> Bool {
214
- return search ( value) != nil
214
+ return search ( value: value ) != nil
215
215
}
216
216
217
217
/*
@@ -298,32 +298,32 @@ extension BinarySearchTree {
298
298
// MARK: - Traversal
299
299
300
300
extension 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 )
303
303
process ( value)
304
- right? . traverseInOrder ( process)
304
+ right? . traverseInOrder ( process: process )
305
305
}
306
306
307
- public func traversePreOrder( @ noescape process: T -> Void ) {
307
+ public func traversePreOrder( process: ( T ) -> Void ) {
308
308
process ( value)
309
- left? . traversePreOrder ( process)
310
- right? . traversePreOrder ( process)
309
+ left? . traversePreOrder ( process: process )
310
+ right? . traversePreOrder ( process: process )
311
311
}
312
312
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 )
316
316
process ( value)
317
317
}
318
318
319
319
/*
320
320
Performs an in-order traversal and collects the results in an array.
321
321
*/
322
- public func map( @ noescape formula: T -> T ) -> [ T ] {
322
+ public func map( formula: ( T ) -> T ) -> [ T ] {
323
323
var a = [ T] ( )
324
- if let left = left { a += left. map ( formula) }
324
+ if let left = left { a += left. map ( formula: formula ) }
325
325
a. append ( formula ( value) )
326
- if let right = right { a += right. map ( formula) }
326
+ if let right = right { a += right. map ( formula: formula ) }
327
327
return a
328
328
}
329
329
}
@@ -332,7 +332,7 @@ extension BinarySearchTree {
332
332
Is this binary tree a valid binary search tree?
333
333
*/
334
334
extension BinarySearchTree {
335
- public func isBST( minValue minValue : T , maxValue: T ) -> Bool {
335
+ public func isBST( minValue: T , maxValue: T ) -> Bool {
336
336
if value < minValue || value > maxValue { return false }
337
337
let leftBST = left? . isBST ( minValue: minValue, maxValue: value) ?? true
338
338
let rightBST = right? . isBST ( minValue: value, maxValue: maxValue) ?? true
0 commit comments