-
-
Notifications
You must be signed in to change notification settings - Fork 136
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
Closed
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] { | ||
let reverseOrder = { i1, i2 in sort(i2, i1) } | ||
var h = Heap(array: a, sort: reverseOrder) | ||
return h.sort() | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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))" | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.