| Crates.io | d-ary-heap |
| lib.rs | d-ary-heap |
| version | 2.3.0 |
| created_at | 2025-12-28 09:23:22.746946+00 |
| updated_at | 2025-12-28 09:23:22.746946+00 |
| description | Generic d-ary heap priority queue supporting min/max via comparator, with O(1) item lookup for updates. Features configurable arity, efficient priority updates, and cross-language API compatibility. |
| homepage | https://github.com/PCfVW/priority-queues |
| repository | https://github.com/PCfVW/priority-queues |
| max_upload_size | |
| id | 2008490 |
| size | 64,144 |
Wikipedia-standard d-ary heap implementation with O(1) item lookup and configurable arity.
d (number of children per node)front(), peek(), len(), is_empty(), contains()insert(), increase_priority()pop(), decrease_priority()Display trait alongside to_string() for flexibilityAdd to your Cargo.toml:
[dependencies]
d-ary-heap = "2.3.0"
use d_ary_heap::{DHeap, MinBy, MaxBy};
#[derive(Clone, Eq, PartialEq, Hash)]
struct Task {
id: u32,
priority: u32,
}
// Min-heap by priority (lower value = higher priority)
let mut heap = DHeap::new(4, MinBy(|t: &Task| t.priority));
// Insert items
heap.insert(Task { id: 1, priority: 10 });
heap.insert(Task { id: 2, priority: 5 });
// Get highest priority item
let top = heap.front().unwrap();
assert_eq!(top.priority, 5);
// Update priority (make more important)
heap.increase_priority(&Task { id: 1, priority: 1 });
let top = heap.front().unwrap();
assert_eq!(top.priority, 1);
// Remove items in priority order
while let Some(task) = heap.pop() {
println!("Processing task {} with priority {}", task.id, task.priority);
}
use d_ary_heap::{DHeap, MinBy};
let mut heap = DHeap::new(3, MinBy(|x: &i32| *x));
// Insert items
heap.insert(10);
heap.insert(5);
heap.insert(15);
// Check properties
assert_eq!(heap.len(), 3);
assert!(!heap.is_empty());
assert_eq!(heap.d(), 3);
// Access highest priority
assert_eq!(heap.front(), Some(&5));
assert_eq!(heap.peek(), Some(&5));
// Remove items
assert_eq!(heap.pop(), Some(5));
assert_eq!(heap.pop(), Some(10));
assert_eq!(heap.pop(), Some(15));
assert_eq!(heap.pop(), None);
use d_ary_heap::{DHeap, MaxBy};
let mut heap = DHeap::new(2, MaxBy(|x: &i32| *x));
heap.insert(10);
heap.insert(5);
heap.insert(15);
// Max-heap: highest value has highest priority
assert_eq!(heap.front(), Some(&15));
use d_ary_heap::DHeap;
// Custom comparator for complex types
let heap = DHeap::new(4, |a: &str, b: &str| a.len() < b.len());
heap.insert("short");
heap.insert("medium length");
heap.insert("longest string here");
// Shortest string has highest priority
assert_eq!(heap.front(), Some(&"short"));
DHeap<T, C>: The main heap typeMinBy<F>: Comparator wrapper for min-heap behaviorMaxBy<F>: Comparator wrapper for max-heap behaviorPosition: Type alias for position indices (cross-language consistency)| Method | Complexity | Description |
|---|---|---|
new(d, comparator) |
O(1) | Create new heap with arity d |
len() |
O(1) | Number of items |
is_empty() |
O(1) | Check if empty |
d() |
O(1) | Get arity |
contains(item) |
O(1) | Check membership |
front() |
O(1) | Highest priority item (None if empty) |
peek() |
O(1) | Alias for front() |
insert(item) |
O(log_d n) | Add new item |
increase_priority(item) |
O(log_d n) | Update to higher priority |
decrease_priority(item) |
O(d·log_d n) | Update to lower priority |
pop() |
O(d·log_d n) | Remove highest priority item |
clear() |
O(1) | Remove all items |
to_string() |
O(n) | String representation |
| Arity | Use Case | Insert | Pop |
|---|---|---|---|
| d=2 | Mixed workloads | O(log n) | O(log n) |
| d=3-4 | Insert-heavy | O(log₃ n) | O(3·log₃ n) |
| d=8+ | Very insert-heavy | O(log₈ n) | O(8·log₈ n) |
Recommendation: Start with d=4 for most workloads.
This implementation provides API parity with:
PriorityQueue<T> in Cpp/PriorityQueue.hDHeap(T) in zig/src/d_heap.zigPriorityQueue<T> in TypeScript/src/PriorityQueue.tsAll implementations share:
A d-ary heap is a tree structure where:
This implementation follows the d-ary heap specification from:
# Run all tests
cargo test
# Run specific test
cargo test test_min_heap_ordering
# Run demo
cargo run --bin demo
Apache License 2.0 - See LICENSE for details.