From 472db7ff3115fa41137ce5fc0873af2d8512a0dc Mon Sep 17 00:00:00 2001 From: NIKHIL BN Date: Wed, 31 Jul 2019 16:43:55 +0530 Subject: [PATCH] Fixed balancing of avl tree after insertion or deletion --- src/data-structures/tree/avl-tree/AvlTree.js | 40 ++++++++++++++++++-- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/src/data-structures/tree/avl-tree/AvlTree.js b/src/data-structures/tree/avl-tree/AvlTree.js index 8a8b5d12..6d4743c0 100644 --- a/src/data-structures/tree/avl-tree/AvlTree.js +++ b/src/data-structures/tree/avl-tree/AvlTree.js @@ -9,9 +9,12 @@ export default class AvlTree extends BinarySearchTree { super.insert(value); // Let's move up to the root and check balance factors along the way. - let currentNode = this.root.find(value); + let insertedNode = this.root.find(value); + let currentNode = insertedNode; while (currentNode) { - this.balance(currentNode); + if (this.balanceInsert(currentNode, insertedNode)) { + return; + } currentNode = currentNode.parent; } } @@ -25,13 +28,42 @@ export default class AvlTree extends BinarySearchTree { super.remove(value); // Balance the tree starting from the root node. - this.balance(this.root); + + let currentNode = this.root.find(value); + + while(currentNode) { + this.balanceRemove(currentNode); + currentNode = currentNode.parent; + } + } + + /** + * @param {BinarySearchTreeNode} node + * @return {Boolean} + */ + balanceInsert(node, insertedNode) { + if (node.balanceFactor > 1) { + if (insertedNode.value < node.value) { + this.rotateLeftLeft(node); + } else { + this.rotateLeftRight(node); + } + return true; + } else if (node.balanceFactor < -1) { + if (insertedNode.value > node.value) { + this.rotateRightRight(node); + } else { + this.rotateRightLeft(node); + } + return true; + } + return false; } /** * @param {BinarySearchTreeNode} node */ - balance(node) { + balanceRemove(node) { // If balance factor is not OK then try to balance the node. if (node.balanceFactor > 1) { // Left rotation.