Skip to content

Add - AVL Tree, Binary Search Tree & Heap Sort. #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions sorts/HeapSort.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
extension Heap {
public mutating func sort() -> [T] {
for i in stride(from: (nodes.count - 1), through: 1, by: -1) {
nodes.swapAt(0, i)
shiftDown(from: 0, until: i)
}
return nodes
}
}

/*
Sorts an array using a heap.
Heapsort can be performed in-place, but it is not a stable sort.
*/
public func heapsort<T>(_ a: [T], _ sort: @escaping (T, T) -> Bool) -> [T] {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test for the implementation is missing. Add test cases for demonstration.

let reverseOrder = { i1, i2 in sort(i2, i1) }
var h = Heap(array: a, sort: reverseOrder)
return h.sort()
}
120 changes: 120 additions & 0 deletions trees/BinarySearchTree.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
Binary search tree using value types
The tree is immutable. Any insertions or deletions will create a new tree.
*/
public enum BinarySearchTree<T: Comparable> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test for the implementation is missing. Add test cases for demonstration.

case empty
case leaf(T)
indirect case node(BinarySearchTree, T, BinarySearchTree)

/* How many nodes are in this subtree. Performance: O(n). */
public var count: Int {
switch self {
case .empty: return 0
case .leaf: return 1
case let .node(left, _, right): return left.count + 1 + right.count
}
}

/* Distance of this node to its lowest leaf. Performance: O(n). */
public var height: Int {
switch self {
case .empty: return -1
case .leaf: return 0
case let .node(left, _, right): return 1 + max(left.height, right.height)
}
}

/*
Inserts a new element into the tree.
Performance: runs in O(h) time, where h is the height of the tree.
*/
public func insert(newValue: T) -> BinarySearchTree {
switch self {
case .empty:
return .leaf(newValue)

case .leaf(let value):
if newValue < value {
return .node(.leaf(newValue), value, .empty)
} else {
return .node(.empty, value, .leaf(newValue))
}

case .node(let left, let value, let right):
if newValue < value {
return .node(left.insert(newValue), value, right)
} else {
return .node(left, value, right.insert(newValue))
}
}
}

/*
Finds the "highest" node with the specified value.
Performance: runs in O(h) time, where h is the height of the tree.
*/
public func search(x: T) -> BinarySearchTree? {
switch self {
case .empty:
return nil
case .leaf(let y):
return (x == y) ? self : nil
case let .node(left, y, right):
if x < y {
return left.search(x)
} else if y < x {
return right.search(x)
} else {
return self
}
}
}

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

/*
Returns the leftmost descendent. O(h) time.
*/
public func minimum() -> BinarySearchTree {
var node = self
var prev = node
while case let .node(next, _, _) = node {
prev = node
node = next
}
if case .leaf = node {
return node
}
return prev
}

/*
Returns the rightmost descendent. O(h) time.
*/
public func maximum() -> BinarySearchTree {
var node = self
var prev = node
while case let .node(_, _, next) = node {
prev = node
node = next
}
if case .leaf = node {
return node
}
return prev
}
}

extension BinarySearchTree: CustomDebugStringConvertible {
public var debugDescription: String {
switch self {
case .empty: return "."
case .leaf(let value): return "\(value)"
case .node(let left, let value, let right):
return "(\(left.debugDescription) <- \(value) -> \(right.debugDescription))"
}
}
}