d-ary Heap Priority Queue (Rust) v2.3.0
Wikipedia-standard d-ary heap implementation with O(1) item lookup and configurable arity.
Key Features
- d-ary heap (not binary): Configurable arity
d(number of children per node) - Min/Max flexibility: Supports both min-heap and max-heap behavior via comparators
- O(1) item lookup: Internal hash map enables efficient priority updates
- Efficient operations:
- O(1):
front(),peek(),len(),is_empty(),contains() - O(log_d n):
insert(),increase_priority() - O(d × log_d n):
pop(),decrease_priority()
- O(1):
- Cross-language API: Unified methods matching C++, Zig, and TypeScript implementations
- Rust-idiomatic: Implements
Displaytrait alongsideto_string()for flexibility
Installation
Add to your Cargo.toml:
[]
= "2.3.0"
Quick Start
use ;
// Min-heap by priority (lower value = higher priority)
let mut heap = new;
// Insert items
heap.insert;
heap.insert;
// Get highest priority item
let top = heap.front.unwrap;
assert_eq!;
// Update priority (make more important)
heap.increase_priority;
let top = heap.front.unwrap;
assert_eq!;
// Remove items in priority order
while let Some = heap.pop
Usage Examples
Basic Operations
use ;
let mut heap = new;
// Insert items
heap.insert;
heap.insert;
heap.insert;
// Check properties
assert_eq!;
assert!;
assert_eq!;
// Access highest priority
assert_eq!;
assert_eq!;
// Remove items
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Max-Heap Example
use ;
let mut heap = new;
heap.insert;
heap.insert;
heap.insert;
// Max-heap: highest value has highest priority
assert_eq!;
Custom Comparators
use DHeap;
// Custom comparator for complex types
let heap = new;
heap.insert;
heap.insert;
heap.insert;
// Shortest string has highest priority
assert_eq!;
API Reference
Core Types
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)
Methods
| 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 |
Performance Considerations
Choosing Optimal Arity (d)
| 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.
Optimization Tips
- Pre-size when possible: If you know approximate size, create with capacity
- Choose d wisely: Benchmark with your workload (d=4 often optimal)
- Use simple comparators: Inline closures are faster than complex functions
- Stable identity: Ensure Hash/Eq are based on stable identity, not priority
Cross-Language Compatibility
This implementation provides API parity with:
- C++:
PriorityQueue<T>inCpp/PriorityQueue.h - Zig:
DHeap(T)inzig/src/d_heap.zig - TypeScript:
PriorityQueue<T>inTypeScript/src/PriorityQueue.ts
All implementations share:
- Identical time complexities
- Unified method names (with language-appropriate casing)
- Cross-language API consistency
What is a d-ary Heap?
A d-ary heap is a tree structure where:
- Each node has up to d children (configurable arity)
- The root contains the highest-priority item
- Children are unordered (unlike binary heaps)
- Heap property: Each parent has higher priority than all children
- Complete tree: Filled left-to-right, level by level
Advantages over Binary Heaps (d=2)
- Shallower tree: Height is log_d(n) instead of log₂(n)
- Faster inserts: O(log_d n) vs O(log₂ n)
- Configurable: Optimize for your specific workload
Trade-offs
- Slower pops: O(d·log_d n) vs O(log₂ n)
- More comparisons: Each pop examines up to d children
- Memory: Slightly higher overhead for position tracking
Reference Implementation
This implementation follows the d-ary heap specification from:
- Wikipedia: D-ary heap
- Network Flows textbook: Section A.3, pp. 773–778
- Ravindra Ahuja, Thomas Magnanti & James Orlin
- Prentice Hall (1993)
- Book information
Testing
# Run all tests
# Run specific test
# Run demo
License
Apache License 2.0 - See LICENSE for details.