Skip to content

Commit 853728d

Browse files
authored
Merge pull request #19 from datastructures-js/development
v3.1.1
2 parents b84ae4b + 5c360d0 commit 853728d

File tree

3 files changed

+11
-35
lines changed

3 files changed

+11
-35
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [3.1.1] - 2020-04-01
10+
### Fixed
11+
- use the same balancing algorithm for insert and remove.
12+
913
## [3.1.0] - 2020-03-31
1014
### Added
1115
- AvlTreeNode & AvlTree implementation.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@datastructures-js/binary-search-tree",
3-
"version": "3.1.0",
3+
"version": "3.1.1",
44
"description": "binary search tree & avl tree (self balancing tree) implementation in javascript",
55
"main": "index.js",
66
"scripts": {

src/avlTree.js

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,10 @@ const AvlTreeNode = require('./avlTreeNode');
1414
class AvlTree extends BinarySearchTree {
1515
/**
1616
* @private
17-
* applies the proper rotation on nodes after inserting a node
17+
* applies the proper rotation on nodes after an insert or remove
1818
* @param {AvlTreeNode} node
1919
*/
20-
balanceAfterInsert(key, node) {
21-
if (!node) return;
22-
23-
node.updateHeight();
24-
const balance = node.calculateBalance();
25-
if (balance > 1) {
26-
if (key < node.getLeft().getKey()) {
27-
node.rotateRight();
28-
} else {
29-
node.rotateLeftRight();
30-
}
31-
} else if (balance < -1) {
32-
if (key > node.getRight().getKey()) {
33-
node.rotateLeft();
34-
} else {
35-
node.rotateRightLeft();
36-
}
37-
}
38-
if (node === this.rootNode && (balance < -1 || balance > 1)) {
39-
this.rootNode = node.getParent();
40-
}
41-
}
42-
43-
/**
44-
* @private
45-
* applies the proper rotation on nodes after removing a node
46-
* @param {AvlTreeNode} node
47-
*/
48-
balanceAfterRemove(node) {
20+
balanceNode(node) {
4921
if (!node) return;
5022

5123
node.updateHeight();
@@ -110,12 +82,12 @@ class AvlTree extends BinarySearchTree {
11082

11183
if (key < node.getKey()) {
11284
const newNode = this.insert(key, value, node.getLeft());
113-
this.balanceAfterInsert(key, node); // back-tracking
85+
this.balanceNode(node); // back-tracking
11486
return newNode;
11587
}
11688

11789
const newNode = this.insert(key, value, node.getRight());
118-
this.balanceAfterInsert(key, node); // back-tracking
90+
this.balanceNode(node); // back-tracking
11991
return newNode;
12092
}
12193

@@ -124,13 +96,13 @@ class AvlTree extends BinarySearchTree {
12496

12597
if (key < node.getKey()) {
12698
const removed = this.remove(key, node.getLeft());
127-
this.balanceAfterRemove(node);
99+
this.balanceNode(node);
128100
return removed;
129101
}
130102

131103
if (key > node.getKey()) {
132104
const removed = this.remove(key, node.getRight());
133-
this.balanceAfterRemove(node);
105+
this.balanceNode(node);
134106
return removed;
135107
}
136108

0 commit comments

Comments
 (0)