feat(rtl): add rbtree

This commit is contained in:
Loïc Grégoire
2026-03-29 10:56:14 -04:00
parent d1b011a27e
commit ff7d378ed2
3 changed files with 454 additions and 6 deletions
+11 -6
View File
@@ -1,10 +1,10 @@
const rtl = @import("rtl");
const TaggedPtr = @import("tagged_ptr.zig").TaggedPtr;
const std = @import("std");
pub const Node = struct {
left: *Node,
right: *Node,
parent: rtl.TaggedPtr(Node),
parent: TaggedPtr(Node),
};
/// Base struct for binary search trees.
@@ -20,7 +20,7 @@ pub fn BST(comptime cmp: fn (*Node, *Node) std.math.Order) type {
pub fn init(self: *Self) void {
self.nil.left = &self.nil;
self.nil.right = &self.nil;
self.nil.parent.set_ptr(&self.nil);
self.nil.parent = .init(&self.nil, 0);
self.root = &self.nil;
}
@@ -100,7 +100,7 @@ pub fn BST(comptime cmp: fn (*Node, *Node) std.math.Order) type {
// Insert `elem` into the BST, if already inserted, do nothing.
// This does not perform any balancing.
pub fn insert(self: *Self, elem: *Node) void {
pub fn insert(self: *Self, elem: *Node) !void {
var current = self.root;
if (self.is_nil(current)) {
@@ -128,7 +128,7 @@ pub fn BST(comptime cmp: fn (*Node, *Node) std.math.Order) type {
current = current.right;
} else {
// Already in the tree, do nothing.
return;
return error.AlreadyExists;
}
}
@@ -136,6 +136,11 @@ pub fn BST(comptime cmp: fn (*Node, *Node) std.math.Order) type {
elem.right = &self.nil;
elem.parent.set_ptr(current);
}
/// Return whether or not the tree is empty.
pub fn is_empty(self: *Self) bool {
return self.is_nil(self.root);
}
};
}
@@ -167,7 +172,7 @@ test BST {
};
for (&nodes) |*node| {
tree.insert(&node.node);
try tree.insert(&node.node);
}
for (&nodes) |*node| {