fn remove(self: *Self, node: *Node) void 240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259 fn remove(self: *Self, node: *Node) void {
// rotate the node down to be a leaf of the tree for removal, respecting priorities.
while (node.children[0] orelse node.children[1]) |_| {
self.rotate(node, rotate_right: {
const right = node.children[1] orelse break :rotate_right true;
const left = node.children[0] orelse break :rotate_right false;
break :rotate_right (left.priority < right.priority);
});
}
// node is a now a leaf; remove by nulling out the parent's reference to it.
const link = if (node.parent) |p| &p.children[@intFromBool(p.children[1] == node)] else &self.root;
assert(link.* == node);
link.* = null;
// clean up after ourselves
node.priority = 0;
node.parent = null;
node.children = [_]?*Node{ null, null };
}