Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 22d0fb2

Browse files
Merge pull request amejiarosario#34 from amejiarosario/beblueblue-question
fix(avl-tree): balance was not working properly
2 parents 48fe6f3 + 01ad65d commit 22d0fb2

File tree

5 files changed

+84
-15
lines changed

5 files changed

+84
-15
lines changed

‎CHANGELOG.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313

1414
### Bug Fixes (patch)
1515

16+
## [1.3.7]
17+
18+
### Breaking Changes (major)
19+
20+
### New Features (minor)
21+
22+
### Bug Fixes (patch)
23+
- fix(avl-tree): balance was not working properly [commit](https://github.com/amejiarosario/dsa.js/commit/98e2c037f05caf37731da1dc50dd8867a1804c0e)
24+
1625
## [1.3.6]
1726

1827
### Breaking Changes (major)
@@ -21,7 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2130

2231
### Bug Fixes (patch)
2332
- update deps [commit](https://github.com/amejiarosario/dsa.js/commit/d8ce2f5b1a3bfeb861928d6c99d7624cd9ac144a)
24-
- style: fix eslint issue [commit](https://github.com/amejiarosario/dsa.js/commit/72e3d68e09bb9c7dd3fabf5cbeba1ae5571fc686)%
33+
- style: fix eslint issue [commit](https://github.com/amejiarosario/dsa.js/commit/72e3d68e09bb9c7dd3fabf5cbeba1ae5571fc686)
2534

2635
## [1.3.5]
2736

@@ -116,7 +125,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
116125

117126
-
118127

119-
[Unreleased]: https://github.com/amejiarosario/dsa.js/compare/1.3.6...HEAD
128+
[Unreleased]: https://github.com/amejiarosario/dsa.js/compare/1.3.7...HEAD
129+
[1.3.6]: https://github.com/amejiarosario/dsa.js/compare/1.3.6...1.3.7
120130
[1.3.6]: https://github.com/amejiarosario/dsa.js/compare/1.3.5...1.3.6
121131
[1.3.5]: https://github.com/amejiarosario/dsa.js/compare/1.3.4...1.3.5
122132
[1.3.4]: https://github.com/amejiarosario/dsa.js/compare/1.2.3...1.3.4

‎notes.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ git log <last tag> HEAD --pretty=format:%s
2222
# example
2323
git log 1.1.0..HEAD --pretty=format:%s
2424

25-
git log 1.3.4..HEAD --pretty=format:"- %s [commit](https://github.com/amejiarosario/dsa.js/commit/%H)" --grep "BREAKING CHANGE:"
26-
git log 1.3.4..HEAD --pretty=format:"- %s [commit](https://github.com/amejiarosario/dsa.js/commit/%H)" --grep "^feat.*:"
27-
git log 1.3.4..HEAD --pretty=format:"- %s [commit](https://github.com/amejiarosario/dsa.js/commit/%H)" --grep "^fix.*:"
25+
git log 1.3.6..HEAD --pretty=format:"- %s [commit](https://github.com/amejiarosario/dsa.js/commit/%H)" --grep "BREAKING CHANGE:"
26+
git log 1.3.6..HEAD --pretty=format:"- %s [commit](https://github.com/amejiarosario/dsa.js/commit/%H)" --grep "^feat.*:"
27+
git log 1.3.6..HEAD --pretty=format:"- %s [commit](https://github.com/amejiarosario/dsa.js/commit/%H)" --grep "^fix.*:"
2828
```
2929

3030
New features in this release

‎package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dsa.js",
3-
"version": "1.3.6",
3+
"version": "1.3.7",
44
"description": "Data Structures & Algorithms in JS",
55
"author": "Adrian Mejia <hi+dsajs@adrianmejia.com> (https://adrianmejia.com)",
66
"homepage": "https://github.com/amejiarosario/dsa.js",

‎src/data-structures/trees/avl-tree.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,21 @@ const {
1717
* - LR rotations: double rotation left-right
1818
* - RL rotations: double rotation right-left
1919
*
20-
* @param {TreeNode} node
20+
* @param {BinaryTreeNode} node
2121
*/
2222
function balance(node) {
2323
if (node.balanceFactor > 1) {
2424
// left subtree is higher than right subtree
25-
if (node.left.balanceFactor > 0) {
26-
return rightRotation(node);
27-
} if (node.left.balanceFactor < 0) {
25+
if (node.left.balanceFactor < 0) {
2826
return leftRightRotation(node);
2927
}
30-
} else if (node.balanceFactor < -1) {
28+
return rightRotation(node);
29+
} if (node.balanceFactor < -1) {
3130
// right subtree is higher than left subtree
32-
if (node.right.balanceFactor < 0) {
33-
return leftRotation(node);
34-
} if (node.right.balanceFactor > 0) {
31+
if (node.right.balanceFactor > 0) {
3532
return rightLeftRotation(node);
3633
}
34+
return leftRotation(node);
3735
}
3836
return node;
3937
}
@@ -43,7 +41,7 @@ function balance(node) {
4341
/**
4442
* Bubbles up balancing nodes a their parents
4543
*
46-
* @param {TreeNode} node
44+
* @param {BinaryTreeNode} node
4745
*/
4846
function balanceUpstream(node) {
4947
let current = node;

‎src/data-structures/trees/avl-tree.spec.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,65 @@ describe('AvlTree', () => {
165165
null, null, null, null, null, null]);
166166
});
167167
});
168+
169+
describe('balancing to the left', () => {
170+
let n32;
171+
beforeEach(() => {
172+
n32 = tree.add(32);
173+
tree.add(8);
174+
tree.add(64);
175+
tree.add(4);
176+
tree.add(16);
177+
tree.add(48);
178+
tree.add(128);
179+
tree.add(2);
180+
tree.add(6);
181+
tree.add(10);
182+
tree.add(20);
183+
});
184+
185+
it('should have all nodes', () => {
186+
expect(tree.toArray()).toEqual([32, 8, 64, 4, 16, 48, 128, 2, 6, 10, 20,
187+
null, null, null, null, null, null, null, null, null, null, null, null]);
188+
});
189+
190+
it('should rebalance when removing', () => {
191+
tree.remove(64);
192+
expect(tree.toArray()).toEqual([32, 8, 128, 4, 16, 48, null, 2, 6, 10, 20,
193+
null, null, null, null, null, null, null, null, null, null]);
194+
expect(n32.balanceFactor).toBe(1);
195+
expect(n32.right.balanceFactor).toBe(1);
196+
expect(n32.left.balanceFactor).toBe(0);
197+
198+
tree.remove(48);
199+
expect(tree.toArray()).toEqual([8, 4, 32, 2, 6, 16, 128, null, null, null, null, 10, 20,
200+
null, null, null, null, null, null]);
201+
});
202+
});
203+
204+
describe('balancing to the right', () => {
205+
beforeEach(() => {
206+
tree.add(8);
207+
tree.add(4);
208+
tree.add(32);
209+
tree.add(2);
210+
tree.add(16);
211+
tree.add(64);
212+
tree.add(10);
213+
tree.add(20);
214+
tree.add(60);
215+
tree.add(70);
216+
});
217+
218+
it('should build the tree', () => {
219+
expect(tree.toArray()).toEqual([8, 4, 32, 2, null, 16, 64, null, null, 10, 20, 60, 70,
220+
null, null, null, null, null, null, null, null]);
221+
});
222+
223+
it('should rebalance right side', () => {
224+
tree.remove(2);
225+
expect(tree.toArray()).toEqual([32, 8, 64, 4, 16, 60, 70, null, null, 10, 20,
226+
null, null, null, null, null, null, null, null]);
227+
});
228+
});
168229
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /