-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4150bf8
commit 8344aec
Showing
14 changed files
with
377 additions
and
157 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,85 @@ | ||
//// This module provides an implementation of the leftist heap data structure, | ||
//// a type of binary heap with efficient insert, find_min, and delete_min, and merge operations. | ||
|
||
// Based on "Purely Functional Data Structures" by Okasaki (1998) | ||
|
||
import gleam/order.{type Order, Gt} | ||
|
||
type T(a) { | ||
E | ||
T(Int, a, T(a), T(a)) | ||
type Tree(a) { | ||
Empty | ||
Tree(Int, a, Tree(a), Tree(a)) | ||
} | ||
|
||
pub opaque type Heap(a) { | ||
Heap(root: T(a), compare: fn(a, a) -> Order) | ||
Heap(root: Tree(a), compare: fn(a, a) -> Order) | ||
} | ||
|
||
/// Creates a new empty heap with the provided comparison function. | ||
pub fn new(compare: fn(a, a) -> Order) -> Heap(a) { | ||
Heap(E, compare) | ||
Heap(Empty, compare) | ||
} | ||
|
||
/// Inserts a new item into the heap, preserving the heap property. | ||
/// Time complexity: O(log n) | ||
pub fn insert(heap: Heap(a), item: a) -> Heap(a) { | ||
Heap(merge(T(1, item, E, E), heap.root, heap.compare), heap.compare) | ||
Heap( | ||
merge_trees(Tree(1, item, Empty, Empty), heap.root, heap.compare), | ||
heap.compare, | ||
) | ||
} | ||
|
||
/// Returns the minimum element in the heap, if the heap is not empty. | ||
/// Time complexity: O(1) | ||
pub fn find_min(heap: Heap(a)) -> Result(a, Nil) { | ||
case heap.root { | ||
T(_, x, _, _) -> Ok(x) | ||
E -> Error(Nil) | ||
Tree(_, x, _, _) -> Ok(x) | ||
Empty -> Error(Nil) | ||
} | ||
} | ||
|
||
/// Removes and returns the minimum element from the heap, along with the | ||
/// new heap after deletion, if the heap is not empty. | ||
/// Time complexity: O(log n) | ||
pub fn delete_min(heap: Heap(a)) -> Result(#(a, Heap(a)), Nil) { | ||
case heap.root { | ||
T(_, x, a, b) -> Ok(#(x, Heap(merge(a, b, heap.compare), heap.compare))) | ||
E -> Error(Nil) | ||
Tree(_, x, a, b) -> | ||
Ok(#(x, Heap(merge_trees(a, b, heap.compare), heap.compare))) | ||
Empty -> Error(Nil) | ||
} | ||
} | ||
|
||
fn merge(h1: T(a), h2: T(a), compare: fn(a, a) -> Order) -> T(a) { | ||
/// Merges two heaps into a new heap containing all elements from both heaps, | ||
/// preserving the heap property. | ||
/// The given heaps must have the same comparison function. | ||
/// Time complexity: O(log n) | ||
pub fn merge(heap1: Heap(a), heap2: Heap(a)) -> Heap(a) { | ||
let compare = heap1.compare | ||
Heap(merge_trees(heap1.root, heap2.root, compare), compare) | ||
} | ||
|
||
fn merge_trees(h1: Tree(a), h2: Tree(a), compare: fn(a, a) -> Order) -> Tree(a) { | ||
case h1, h2 { | ||
h, E -> h | ||
E, h -> h | ||
T(_, x, a1, b1), T(_, y, a2, b2) -> | ||
h, Empty -> h | ||
Empty, h -> h | ||
Tree(_, x, a1, b1), Tree(_, y, a2, b2) -> | ||
case compare(x, y) { | ||
Gt -> make(y, a2, merge(h1, b2, compare)) | ||
_ -> make(x, a1, merge(b1, h2, compare)) | ||
Gt -> make(y, a2, merge_trees(h1, b2, compare)) | ||
_ -> make(x, a1, merge_trees(b1, h2, compare)) | ||
} | ||
} | ||
} | ||
|
||
fn make(x, a, b) { | ||
let rank_a = case a { | ||
T(r, _, _, _) -> r | ||
E -> 0 | ||
Tree(r, _, _, _) -> r | ||
Empty -> 0 | ||
} | ||
let rank_b = case b { | ||
T(r, _, _, _) -> r | ||
E -> 0 | ||
Tree(r, _, _, _) -> r | ||
Empty -> 0 | ||
} | ||
case rank_a < rank_b { | ||
True -> T(rank_a + 1, x, b, a) | ||
_ -> T(rank_b + 1, x, a, b) | ||
True -> Tree(rank_a + 1, x, b, a) | ||
_ -> Tree(rank_b + 1, x, a, b) | ||
} | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,79 @@ | ||
//// This module provides an implementation of the pairing heap data structure, | ||
//// a type of self-adjusting heap with efficient insert, find_min, and delete_min, and merge operations. | ||
|
||
// Based on "Purely Functional Data Structures" by Okasaki (1998) | ||
|
||
import gleam/order.{type Order, Gt} | ||
|
||
type T(a) { | ||
E | ||
T(a, List(T(a))) | ||
type Tree(a) { | ||
Empty | ||
Tree(a, List(Tree(a))) | ||
} | ||
|
||
pub opaque type Heap(a) { | ||
Heap(root: T(a), compare: fn(a, a) -> Order) | ||
Heap(root: Tree(a), compare: fn(a, a) -> Order) | ||
} | ||
|
||
/// Creates a new empty heap with the provided comparison function. | ||
pub fn new(compare: fn(a, a) -> Order) -> Heap(a) { | ||
Heap(E, compare) | ||
Heap(Empty, compare) | ||
} | ||
|
||
/// Inserts a new item into the heap, preserving the heap property. | ||
/// Time complexity: O(1) | ||
pub fn insert(heap: Heap(a), key: a) -> Heap(a) { | ||
Heap(merge(T(key, []), heap.root, heap.compare), heap.compare) | ||
Heap(merge_trees(Tree(key, []), heap.root, heap.compare), heap.compare) | ||
} | ||
|
||
/// Returns the minimum element in the heap, if the heap is not empty. | ||
/// Time complexity: O(1) | ||
pub fn find_min(heap: Heap(a)) -> Result(a, Nil) { | ||
case heap.root { | ||
T(x, _) -> Ok(x) | ||
E -> Error(Nil) | ||
Tree(x, _) -> Ok(x) | ||
Empty -> Error(Nil) | ||
} | ||
} | ||
|
||
/// Removes and returns the minimum element from the heap along with the | ||
/// new heap after deletion, if the heap is not empty. | ||
/// Time complexity: O(log n) amortized | ||
pub fn delete_min(heap: Heap(a)) -> Result(#(a, Heap(a)), Nil) { | ||
case heap.root { | ||
T(x, xs) -> Ok(#(x, Heap(merge_pairs(xs, heap.compare), heap.compare))) | ||
E -> Error(Nil) | ||
Tree(x, xs) -> Ok(#(x, Heap(merge_pairs(xs, heap.compare), heap.compare))) | ||
Empty -> Error(Nil) | ||
} | ||
} | ||
|
||
fn merge(x: T(a), y: T(a), compare: fn(a, a) -> Order) -> T(a) { | ||
/// Merges two heaps into a new heap containing all elements from both heaps, | ||
/// preserving the heap property. | ||
/// The given heaps must have the same comparison function. | ||
/// Time complexity: O(1) | ||
pub fn merge(heap1: Heap(a), heap2: Heap(a)) -> Heap(a) { | ||
let compare = heap1.compare | ||
Heap(merge_trees(heap1.root, heap2.root, compare), compare) | ||
} | ||
|
||
fn merge_trees(x: Tree(a), y: Tree(a), compare: fn(a, a) -> Order) -> Tree(a) { | ||
case x, y { | ||
x, E -> x | ||
E, y -> y | ||
T(xk, xs), T(yk, ys) -> | ||
x, Empty -> x | ||
Empty, y -> y | ||
Tree(xk, xs), Tree(yk, ys) -> | ||
case compare(xk, yk) { | ||
Gt -> T(yk, [x, ..ys]) | ||
_ -> T(xk, [y, ..xs]) | ||
Gt -> Tree(yk, [x, ..ys]) | ||
_ -> Tree(xk, [y, ..xs]) | ||
} | ||
} | ||
} | ||
|
||
fn merge_pairs(l: List(T(a)), compare: fn(a, a) -> Order) -> T(a) { | ||
fn merge_pairs(l: List(Tree(a)), compare: fn(a, a) -> Order) -> Tree(a) { | ||
case l { | ||
[] -> E | ||
[] -> Empty | ||
[h] -> h | ||
[h1, h2, ..hs] -> | ||
merge(merge(h1, h2, compare), merge_pairs(hs, compare), compare) | ||
merge_trees( | ||
merge_trees(h1, h2, compare), | ||
merge_pairs(hs, compare), | ||
compare, | ||
) | ||
} | ||
} |
Oops, something went wrong.