Skip to content

Commit f215ea1

Browse files
authored
Merge pull request kodecocodes#205 from raywenderlich/BinarySearchTreeSwift3
Binary search tree swift3
2 parents 86c4314 + b502621 commit f215ea1

File tree

4 files changed

+58
-56
lines changed

4 files changed

+58
-56
lines changed
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
//: Playground - noun: a place where people can play
22

33
let tree = BinarySearchTree<Int>(value: 7)
4-
tree.insert(2)
5-
tree.insert(5)
6-
tree.insert(10)
7-
tree.insert(9)
8-
tree.insert(1)
4+
tree.insert(value: 2)
5+
tree.insert(value: 5)
6+
tree.insert(value: 10)
7+
tree.insert(value: 9)
8+
tree.insert(value: 1)
99

1010
tree
1111
tree.debugDescription
1212

1313
let tree2 = BinarySearchTree<Int>(array: [7, 2, 5, 10, 9, 1])
1414

15-
tree.search(5)
16-
tree.search(2)
17-
tree.search(7)
18-
tree.search(6)
15+
tree.search(value: 5)
16+
tree.search(value: 2)
17+
tree.search(value: 7)
18+
tree.search(value: 6)
1919

2020
tree.traverseInOrder { value in print(value) }
2121

@@ -24,7 +24,7 @@ tree.toArray()
2424
tree.minimum()
2525
tree.maximum()
2626

27-
if let node2 = tree.search(2) {
27+
if let node2 = tree.search(value: 2) {
2828
node2.remove()
2929
node2
3030
print(tree)
@@ -34,25 +34,25 @@ tree.height()
3434
tree.predecessor()
3535
tree.successor()
3636

37-
if let node10 = tree.search(10) {
37+
if let node10 = tree.search(value: 10) {
3838
node10.depth() // 1
3939
node10.height() // 1
4040
node10.predecessor()
4141
node10.successor() // nil
4242
}
4343

44-
if let node9 = tree.search(9) {
44+
if let node9 = tree.search(value: 9) {
4545
node9.depth() // 2
4646
node9.height() // 0
4747
node9.predecessor()
4848
node9.successor()
4949
}
5050

51-
if let node1 = tree.search(1) {
51+
if let node1 = tree.search(value: 1) {
5252
// This makes it an invalid binary search tree because 100 is greater
5353
// than the root, 7, and so must be in the right branch not in the left.
5454
tree.isBST(minValue: Int.min, maxValue: Int.max) // true
55-
node1.insert(100)
56-
tree.search(100) // nil
55+
node1.insert(value: 100)
56+
tree.search(value: 100) // nil
5757
tree.isBST(minValue: Int.min, maxValue: Int.max) // false
5858
}

Binary Search Tree/Solution 1/BinarySearchTree.playground/Sources/BinarySearchTree.swift

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
you should insert new values in randomized order, not in sorted order.
1111
*/
1212
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?
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

300300
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)
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
*/
334334
extension 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

Binary Search Tree/Solution 2/BinarySearchTree.playground/Contents.swift

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
// Each time you insert something, you get back a completely new tree.
44
var tree = BinarySearchTree.Leaf(7)
5-
tree = tree.insert(2)
6-
tree = tree.insert(5)
7-
tree = tree.insert(10)
8-
tree = tree.insert(9)
9-
tree = tree.insert(1)
5+
tree = tree.insert(newValue: 2)
6+
tree = tree.insert(newValue: 5)
7+
tree = tree.insert(newValue: 10)
8+
tree = tree.insert(newValue: 9)
9+
tree = tree.insert(newValue: 1)
1010
print(tree)
1111

12-
tree.search(10)
13-
tree.search(1)
14-
tree.search(11)
12+
tree.search(x: 10)
13+
tree.search(x: 1)
14+
tree.search(x: 11)
15+
16+

Binary Search Tree/Solution 2/BinarySearchTree.playground/Sources/BinarySearchTree.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ public enum BinarySearchTree<T: Comparable> {
4444

4545
case .Node(let left, let value, let right):
4646
if newValue < value {
47-
return .Node(left.insert(newValue), value, right)
47+
return .Node(left.insert(newValue: newValue), value, right)
4848
} else {
49-
return .Node(left, value, right.insert(newValue))
49+
return .Node(left, value, right.insert(newValue: newValue))
5050
}
5151
}
5252
}
@@ -63,17 +63,17 @@ public enum BinarySearchTree<T: Comparable> {
6363
return (x == y) ? self : nil
6464
case let .Node(left, y, right):
6565
if x < y {
66-
return left.search(x)
66+
return left.search(x: x)
6767
} else if y < x {
68-
return right.search(x)
68+
return right.search(x: x)
6969
} else {
7070
return self
7171
}
7272
}
7373
}
7474

7575
public func contains(x: T) -> Bool {
76-
return search(x) != nil
76+
return search(x: x) != nil
7777
}
7878

7979
/*

0 commit comments

Comments
 (0)