Skip to content

Commit

Permalink
Updated playground to Swift 3
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvinlauKL committed Sep 8, 2016
1 parent 3945383 commit 6ee8119
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
//: Playground - noun: a place where people can play

let tree = BinarySearchTree<Int>(value: 7)
tree.insert(2)
tree.insert(5)
tree.insert(10)
tree.insert(9)
tree.insert(1)
tree.insert(value: 2)
tree.insert(value: 5)
tree.insert(value: 10)
tree.insert(value: 9)
tree.insert(value: 1)

tree
tree.debugDescription

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

tree.search(5)
tree.search(2)
tree.search(7)
tree.search(6)
tree.search(value: 5)
tree.search(value: 2)
tree.search(value: 7)
tree.search(value: 6)

tree.traverseInOrder { value in print(value) }

Expand All @@ -24,7 +24,7 @@ tree.toArray()
tree.minimum()
tree.maximum()

if let node2 = tree.search(2) {
if let node2 = tree.search(value: 2) {
node2.remove()
node2
print(tree)
Expand All @@ -34,25 +34,25 @@ tree.height()
tree.predecessor()
tree.successor()

if let node10 = tree.search(10) {
if let node10 = tree.search(value: 10) {
node10.depth() // 1
node10.height() // 1
node10.predecessor()
node10.successor() // nil
}

if let node9 = tree.search(9) {
if let node9 = tree.search(value: 9) {
node9.depth() // 2
node9.height() // 0
node9.predecessor()
node9.successor()
}

if let node1 = tree.search(1) {
if let node1 = tree.search(value: 1) {
// This makes it an invalid binary search tree because 100 is greater
// than the root, 7, and so must be in the right branch not in the left.
tree.isBST(minValue: Int.min, maxValue: Int.max) // true
node1.insert(100)
tree.search(100) // nil
node1.insert(value: 100)
tree.search(value: 100) // nil
tree.isBST(minValue: Int.min, maxValue: Int.max) // false
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
you should insert new values in randomized order, not in sorted order.
*/
public class BinarySearchTree<T: Comparable> {
private(set) public var value: T
private(set) public var parent: BinarySearchTree?
private(set) public var left: BinarySearchTree?
private(set) public var right: BinarySearchTree?
fileprivate(set) public var value: T
fileprivate(set) public var parent: BinarySearchTree?
fileprivate(set) public var left: BinarySearchTree?
fileprivate(set) public var right: BinarySearchTree?

public init(value: T) {
self.value = value
Expand All @@ -22,8 +22,8 @@ public class BinarySearchTree<T: Comparable> {
public convenience init(array: [T]) {
precondition(array.count > 0)
self.init(value: array.first!)
for v in array.dropFirst() {
insert(v, parent: self)
for v in array.dropFirst() {
insert(value: v, parent: self)
}
}

Expand Down Expand Up @@ -74,20 +74,20 @@ extension BinarySearchTree {
Performance: runs in O(h) time, where h is the height of the tree.
*/
public func insert(value: T) {
insert(value, parent: self)
insert(value: value, parent: self)
}

private func insert(value: T, parent: BinarySearchTree) {
fileprivate func insert(value: T, parent: BinarySearchTree) {
if value < self.value {
if let left = left {
left.insert(value, parent: left)
left.insert(value: value, parent: left)
} else {
left = BinarySearchTree(value: value)
left?.parent = parent
}
} else {
if let right = right {
right.insert(value, parent: right)
right.insert(value: value, parent: right)
} else {
right = BinarySearchTree(value: value)
right?.parent = parent
Expand All @@ -108,7 +108,7 @@ extension BinarySearchTree {
Performance: runs in O(h) time, where h is the height of the tree.
*/
public func remove() -> BinarySearchTree? {
@discardableResult public func remove() -> BinarySearchTree? {
let replacement: BinarySearchTree?

if let left = left {
Expand All @@ -126,7 +126,7 @@ extension BinarySearchTree {
replacement = nil
}

reconnectParentToNode(replacement)
reconnectParentTo(node: replacement)

// The current node is no longer part of the tree, so clean it up.
parent = nil
Expand All @@ -136,7 +136,7 @@ extension BinarySearchTree {
return replacement
}

private func removeNodeWithTwoChildren(left: BinarySearchTree, _ right: BinarySearchTree) -> BinarySearchTree {
private func removeNodeWithTwoChildren(_ left: BinarySearchTree, _ right: BinarySearchTree) -> BinarySearchTree {
// This node has two children. It must be replaced by the smallest
// child that is larger than this node's value, which is the leftmost
// descendent of the right child.
Expand Down Expand Up @@ -164,7 +164,7 @@ extension BinarySearchTree {
return successor
}

private func reconnectParentToNode(node: BinarySearchTree?) {
private func reconnectParentTo(node: BinarySearchTree?) {
if let parent = parent {
if isLeftChild {
parent.left = node
Expand Down Expand Up @@ -211,7 +211,7 @@ extension BinarySearchTree {
*/

public func contains(value: T) -> Bool {
return search(value) != nil
return search(value: value) != nil
}

/*
Expand Down Expand Up @@ -298,32 +298,32 @@ extension BinarySearchTree {
// MARK: - Traversal

extension BinarySearchTree {
public func traverseInOrder(@noescape process: T -> Void) {
left?.traverseInOrder(process)
public func traverseInOrder(process: (T) -> Void) {
left?.traverseInOrder(process: process)
process(value)
right?.traverseInOrder(process)
right?.traverseInOrder(process: process)
}

public func traversePreOrder(@noescape process: T -> Void) {
public func traversePreOrder(process: (T) -> Void) {
process(value)
left?.traversePreOrder(process)
right?.traversePreOrder(process)
left?.traversePreOrder(process: process)
right?.traversePreOrder(process: process)
}

public func traversePostOrder(@noescape process: T -> Void) {
left?.traversePostOrder(process)
right?.traversePostOrder(process)
public func traversePostOrder(process: (T) -> Void) {
left?.traversePostOrder(process: process)
right?.traversePostOrder(process: process)
process(value)
}

/*
Performs an in-order traversal and collects the results in an array.
*/
public func map(@noescape formula: T -> T) -> [T] {
public func map(formula: (T) -> T) -> [T] {
var a = [T]()
if let left = left { a += left.map(formula) }
if let left = left { a += left.map(formula: formula) }
a.append(formula(value))
if let right = right { a += right.map(formula) }
if let right = right { a += right.map(formula: formula) }
return a
}
}
Expand All @@ -332,7 +332,7 @@ extension BinarySearchTree {
Is this binary tree a valid binary search tree?
*/
extension BinarySearchTree {
public func isBST(minValue minValue: T, maxValue: T) -> Bool {
public func isBST(minValue: T, maxValue: T) -> Bool {
if value < minValue || value > maxValue { return false }
let leftBST = left?.isBST(minValue: minValue, maxValue: value) ?? true
let rightBST = right?.isBST(minValue: value, maxValue: maxValue) ?? true
Expand Down

0 comments on commit 6ee8119

Please sign in to comment.