Movatterモバイル変換


[0]ホーム

URL:


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

Commit9832fb6

Browse files
committed
Checking the maxHeight properly.
1 parent62700e1 commit9832fb6

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
varsTree=newSBTree();
94-
for(vari=0;i<100000;++i){
94+
for(vari=0;i<2000000;++i){
9595
sTree.push(i);
9696
}
9797
checkNil();
98-
for(vari=0;i<100000;++i){
99-
expect(sTree.get(i).value).toBe(i);
98+
letmaxHeight=0;
99+
for(vari=0;i<2000000;++i){
100+
varnode=sTree.get(i);
101+
maxHeight=Math.max(maxHeight,node.height);
102+
expect(node.value).toBe(i);
100103
}
101-
for(vari=0;i<100000;++i){
104+
expect(maxHeight).toBe(21);
105+
for(vari=0;i<2000000;++i){
102106
expect(sTree.get(0).value).toBe(i);
103107
varnode=sTree.remove(0);// Always remove the first element;
104108
expect(node.value).toBe(i);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp