mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2026-03-13 08:51:02 +08:00
Fix issue #37 with AVL tree.
This commit is contained in:
@@ -80,6 +80,12 @@ export default class AvlTree extends BinarySearchTree {
|
||||
const leftRightNode = leftNode.right;
|
||||
leftNode.setRight(null);
|
||||
|
||||
// Preserve leftRightNode's left subtree.
|
||||
if (leftRightNode.left) {
|
||||
leftNode.setRight(leftRightNode.left);
|
||||
leftRightNode.setLeft(null);
|
||||
}
|
||||
|
||||
// Attach leftRightNode to the rootNode.
|
||||
rootNode.setLeft(leftRightNode);
|
||||
|
||||
@@ -102,6 +108,11 @@ export default class AvlTree extends BinarySearchTree {
|
||||
const rightLeftNode = rightNode.left;
|
||||
rightNode.setLeft(null);
|
||||
|
||||
if (rightLeftNode.right) {
|
||||
rightNode.setLeft(rightLeftNode.right);
|
||||
rightLeftNode.setRight(null);
|
||||
}
|
||||
|
||||
// Attach rightLeftNode to the rootNode.
|
||||
rootNode.setRight(rightLeftNode);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user