Skip to content

Instantly share code, notes, and snippets.

@KristopherGBaker
KristopherGBaker / heap2.js
Last active July 16, 2020 08:13
Alternative Javascript Heap implementation (works with CoderPad)
// 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)
//
@KristopherGBaker
KristopherGBaker / Heap.swift
Last active May 26, 2020 05:06
Simple Swift Heap implementation
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:
@KristopherGBaker
KristopherGBaker / heap.js
Last active December 14, 2023 16:49
Simple Javascript Heap implementation
// 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)
//
@KristopherGBaker
KristopherGBaker / 2014-09-03-potd-swift
Last active August 29, 2015 14:06
2014-09-03 Problem of the Day solution in Swift
// 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>?