From 2c51332c94653d59c231d4ae7b9147807356d6d3 Mon Sep 17 00:00:00 2001 From: Aleksandr Demakin Date: Mon, 9 Sep 2024 23:53:03 +0100 Subject: [PATCH] tree: optimise `findReplacement` used in deletion operation. source: Russell A. Brown, Optimized Deletion From an AVL Tree. https://arxiv.org/pdf/2406.05162v5. --- src/avl.zig | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/avl.zig b/src/avl.zig index d409d28..1e27a26 100644 --- a/src/avl.zig +++ b/src/avl.zig @@ -599,16 +599,16 @@ pub fn TreeWithOptions(comptime K: type, comptime V: type, comptime Cmp: fn (a: const right = loc.child(.right); if (left) |l| { if (right) |r| { - if (l.data().h <= r.data().h) { + // Russell A. Brown, Optimized Deletion From an AVL Tree. + // https://arxiv.org/pdf/2406.05162v5 + if (loc.balance() <= 0) { return goRight(l); } return goLeft(r); } - return goRight(l); - } else if (right) |r| { - return goLeft(r); + return left; } - return null; + return right; } // getMin returns the minimum element of the tree.