Skip to content

Conversation

@errendir
Copy link
Contributor

The code responsible for removing nodes from the AVL tree can leave the tree in an invalid state. Whether a node is a left or a right child of its parent is not correctly detected. This is due to a typo. Compare:

this._compare(val, current.getParent().getValue() < 0)

with

this._compare(val, current.getParent().getValue()) < 0

The first line is not a correct comparison, since the expression current.getParent().getValue() < 0 is a boolean which should not be compared with val which may be a string or a number. The intention is to check which value (either val or current.getParent().getValue()) is larger, so this._compare should be applied to those directly and then compared with 0. This PR fixes this issue.

Additionally the .remove method of AvlTree sometimes doesn't do enough rebalancing. I added a test which shows when that happens and I also added a fix. The problem was with the "case 4" of the removeRecursively function not calling this._balanceNode

@eyas-ranjous eyas-ranjous merged commit 2b354a3 into datastructures-js:master Aug 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants