Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9832fb6

Browse files
committed
Checking the maxHeight properly.
1 parent 62700e1 commit 9832fb6

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

src/data-structures/size-balanced-tree.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
this.left = left;
5353
this.right = right;
5454
this.size = size;
55+
this.height = 0;
5556
}
5657

5758
/**
@@ -62,6 +63,7 @@
6263
*/
6364
Node.prototype.updateSize = function () {
6465
this.size = this.left.size + this.right.size + 1;
66+
this.height = Math.max(this.left.height, this.right.height) + 1;
6567
};
6668

6769
exports.Node = Node;

test/data-structures/size-balanced-tree.spec.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,18 @@ describe('SBTree', function () {
9191

9292
it('push and get 100000 elements, remove the array by always remove the first/last element', function () {
9393
var sTree = new SBTree();
94-
for (var i = 0; i < 100000; ++i) {
94+
for (var i = 0; i < 2000000; ++i) {
9595
sTree.push(i);
9696
}
9797
checkNil();
98-
for (var i = 0; i < 100000; ++i) {
99-
expect(sTree.get(i).value).toBe(i);
98+
let maxHeight = 0;
99+
for (var i = 0; i < 2000000; ++i) {
100+
var node = sTree.get(i);
101+
maxHeight = Math.max(maxHeight, node.height);
102+
expect(node.value).toBe(i);
100103
}
101-
for (var i = 0; i < 100000; ++i) {
104+
expect(maxHeight).toBe(21);
105+
for (var i = 0; i < 2000000; ++i) {
102106
expect(sTree.get(0).value).toBe(i);
103107
var node = sTree.remove(0); // Always remove the first element;
104108
expect(node.value).toBe(i);

0 commit comments

Comments
 (0)