mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-11-10 11:09:43 +08:00
Fixed balancing of avl tree after insertion or deletion
This commit is contained in:
parent
dc1047df72
commit
472db7ff31
@ -9,9 +9,12 @@ export default class AvlTree extends BinarySearchTree {
|
|||||||
super.insert(value);
|
super.insert(value);
|
||||||
|
|
||||||
// Let's move up to the root and check balance factors along the way.
|
// 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) {
|
while (currentNode) {
|
||||||
this.balance(currentNode);
|
if (this.balanceInsert(currentNode, insertedNode)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
currentNode = currentNode.parent;
|
currentNode = currentNode.parent;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -25,13 +28,42 @@ export default class AvlTree extends BinarySearchTree {
|
|||||||
super.remove(value);
|
super.remove(value);
|
||||||
|
|
||||||
// Balance the tree starting from the root node.
|
// 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
|
* @param {BinarySearchTreeNode} node
|
||||||
*/
|
*/
|
||||||
balance(node) {
|
balanceRemove(node) {
|
||||||
// If balance factor is not OK then try to balance the node.
|
// If balance factor is not OK then try to balance the node.
|
||||||
if (node.balanceFactor > 1) {
|
if (node.balanceFactor > 1) {
|
||||||
// Left rotation.
|
// Left rotation.
|
||||||
|
Loading…
Reference in New Issue
Block a user