A robust, generic, and idiomatic B+ tree implementation in Zig (0.15+). This package is designed for high performance, reliability, and ease of use—perfect for use as a dependency or as a learning resource for advanced data structures in Zig.
A B+ tree is a self-balancing tree data structure that maintains sorted data and allows fast insertions, deletions, and lookups. It is widely used in databases and filesystems.
Example (order 4):
[ 10 | 20 ]
/ | \
[1 5 7] [12 15] [22 25 30]
- All values are stored in the leaves.
- Internal nodes only store keys for navigation.
- All leaves are linked for fast range/iteration queries.
- Generic: Works with any comparable key/value type.
- Self-balancing: Maintains optimal height for fast operations.
- Fast: O(log n) insert, search, and (future) remove.
- Range iteration: (Planned) Efficient in-order traversal.
- Memory safe: Uses Zig's allocator model.
- Tested: Includes edge cases and stress tests.
- MIT licensed: Free for any use.
Fetch and add this repo as a dependency:
zig fetch --save git+https://github.com/dayvster/zig-bplus-treeAdd to your build.zig.zon dependencies:
{
"dependencies": {
"bplustree": {
"url": "https://github.com/dayvster/zig-bplus-tree/archive/refs/heads/main.zip"
}
}
}Then in your build.zig:
const bplustree_mod = b.dependency("bplustree", .{}).module("bplustree");
// ...
.imports = &.{ .{ .name = "bplustree", .module = bplustree_mod } },And in your code:
const BPlusTree = @import("bplustree").BPlusTree(i32, 4);- Download or clone this repository.
- Copy
src/bplustree.ziginto your project (e.g.lib/bplustree.zig). - Import it directly:
const BPlusTree = @import("lib/bplustree.zig").BPlusTree(i32, 4);const std = @import("std");
const BPlusTree = @import("bplustree").BPlusTree(i32, 4);
pub fn main() !void {
var tree = BPlusTree.init(&std.heap.page_allocator);
defer tree.deinit();
try tree.insert(42, 100);
const found = tree.search(42);
std.debug.print("Found: {any}\n", .{found});
}See the examples/ directory for more usage patterns, including:
- Basic usage
- Iteration
- Self-balancing stress test
Run all tests:
zig build testPull requests, bug reports, and feature requests are welcome! Please open an issue or PR on GitHub.
If you have questions or need help, open an issue or start a discussion on the repo.
MIT. See LICENSE.