Skip to content

Commit

Permalink
Add deleteMin() and deleteMax()
Browse files Browse the repository at this point in the history
  • Loading branch information
ByronBecker committed Feb 6, 2023
1 parent f4844ff commit 3a0bcb4
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/BTree.mo
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,28 @@ module {
}
};

/// Deletes the minimum key in a BTree and returns its associated value. If the BTree is empty, returns null
public func deleteMin<K, V>(tree: BTree<K, V>, compare: (K, K) -> O.Order): ?(K, V) {
switch(min(tree)) {
case (?(k, v)) {
ignore delete<K, V>(tree, compare, k);
?(k, v);
};
case null { null }
}
};

/// Deletes the maximum key in a BTree and returns its associated value. If the BTree is empty, returns null
public func deleteMax<K, V>(tree: BTree<K, V>, compare: (K, K) -> O.Order): ?(K, V) {
switch(max(tree)) {
case (?(k, v)) {
ignore delete<K, V>(tree, compare, k);
?(k, v);
};
case null { null }
}
};

/// Returns an ascending order BTree iterator
public func entries<K, V>(t: BTree<K, V>): Iter.Iter<(K, V)> {
switch(t.root) {
Expand Down

0 comments on commit 3a0bcb4

Please sign in to comment.