This file contains 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
// usage: | |
// create a min heap | |
// const heap = new Heap((a, b) => a.val < b.val) | |
// | |
// create a max heap | |
// const heap = new Heap((a, b) => a.val > b.val) | |
// | |
// insert an item into the heap - O(lgn) | |
// heap.insert(item) | |
// |
This file contains 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
struct Heap<Element: Comparable> { | |
enum HeapType { | |
case min | |
case max | |
func compare(_ lhs: Element, _ rhs: Element) -> Bool { | |
switch self { | |
case .min: | |
return lhs < rhs | |
case .max: |
This file contains 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
// usage: | |
// create a min heap | |
// const heap = new Heap((a, b) => a.val < b.val) | |
// | |
// create a max heap | |
// const heap = new Heap((a, b) => a.val > b.val) | |
// | |
// insert an item into the heap - O(lgn) | |
// heap.insert(item) | |
// |
This file contains 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
// http://www.problemotd.com/problem/level-order-traversal/ | |
class BinaryTree<T> { | |
init(value: T) { | |
self.value = value | |
} | |
var value: T | |
var left: BinaryTree<T>? | |
var right: BinaryTree<T>? |