Skip to content

Commit

Permalink
Checking the maxHeight properly.
Browse files Browse the repository at this point in the history
  • Loading branch information
lygstate committed Sep 23, 2015
1 parent 62700e1 commit 9832fb6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/data-structures/size-balanced-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
this.left = left;
this.right = right;
this.size = size;
this.height = 0;
}

/**
Expand All @@ -62,6 +63,7 @@
*/
Node.prototype.updateSize = function () {
this.size = this.left.size + this.right.size + 1;
this.height = Math.max(this.left.height, this.right.height) + 1;
};

exports.Node = Node;
Expand Down
12 changes: 8 additions & 4 deletions test/data-structures/size-balanced-tree.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,18 @@ describe('SBTree', function () {

it('push and get 100000 elements, remove the array by always remove the first/last element', function () {
var sTree = new SBTree();
for (var i = 0; i < 100000; ++i) {
for (var i = 0; i < 2000000; ++i) {
sTree.push(i);
}
checkNil();
for (var i = 0; i < 100000; ++i) {
expect(sTree.get(i).value).toBe(i);
let maxHeight = 0;
for (var i = 0; i < 2000000; ++i) {
var node = sTree.get(i);
maxHeight = Math.max(maxHeight, node.height);
expect(node.value).toBe(i);
}
for (var i = 0; i < 100000; ++i) {
expect(maxHeight).toBe(21);
for (var i = 0; i < 2000000; ++i) {
expect(sTree.get(0).value).toBe(i);
var node = sTree.remove(0); // Always remove the first element;
expect(node.value).toBe(i);
Expand Down

0 comments on commit 9832fb6

Please sign in to comment.