From 966e1dd935be05fcca1b20f9fb7a2ea15c1936e7 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Fri, 6 May 2022 09:40:51 +0200 Subject: [PATCH] fix: transplant with successor as a replace MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When transplanting node with a successor, instead of redoing the links, just swap the value and delete „abused“ successor, which allows more straightforward rebalance. Signed-off-by: Matej Focko --- ranked_tree.js | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/ranked_tree.js b/ranked_tree.js index a68a028..c504c90 100644 --- a/ranked_tree.js +++ b/ranked_tree.js @@ -143,18 +143,12 @@ class RankedTree { this.transplant(node, node.left); } else { let successor = nodeMinimum(node.right); - parent = successor.parent != node ? successor.parent : successor; - if (successor.parent != node) { - parent = successor.right ? successor.right : successor.parent; - this.transplant(successor, successor.right); - successor.right = node.right; - successor.right.parent = successor; - } + node.value = successor.value; + successor.value = null; + this.record(); - this.transplant(node, successor); - successor.left = node.left; - node.left.parent = successor; + return this.deleteNode(successor); } this.record();