Skip to content

Commit aa997d3

Browse files
committed
Testing random insert properly.
1 parent 9832fb6 commit aa997d3

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@
231231
newNode = new Node(value, node, Nil, Nil, 1);
232232
node.left = newNode;
233233
} else {
234-
node = findRightMost(node);
234+
node = findRightMost(node.left);
235235
newNode = new Node(value, node, Nil, Nil, 1);
236236
node.right = newNode;
237237
}

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,24 @@ describe('SBTree', function () {
8888
expect(right.parent).toBe(Nil);
8989
checkNil();
9090
});
91+
// Returns a random integer between min (included) and max (excluded)
92+
// Using Math.round() will give you a non-uniform distribution!
93+
function getRandomInt(min, max) {
94+
return Math.floor(Math.random() * (max - min)) + min;
95+
}
96+
// Returns a random integer between min (included) and max (included)
97+
// Using Math.round() will give you a non-uniform distribution!
98+
function getRandomIntInclusive(min, max) {
99+
return Math.floor(Math.random() * (max - min + 1)) + min;
100+
}
91101

92102
it('push and get 100000 elements, remove the array by always remove the first/last element', function () {
93103
var sTree = new SBTree();
94104
for (var i = 0; i < 2000000; ++i) {
95105
sTree.push(i);
96106
}
97107
checkNil();
98-
let maxHeight = 0;
108+
var maxHeight = 0;
99109
for (var i = 0; i < 2000000; ++i) {
100110
var node = sTree.get(i);
101111
maxHeight = Math.max(maxHeight, node.height);
@@ -119,5 +129,31 @@ describe('SBTree', function () {
119129
expect(sTree.size).toBe(count - i - 1);
120130
}
121131
checkNil();
132+
var expectedArray = [];
133+
for (var i = 0; i < 10000; ++i) {
134+
var isAdded = sTree.size === 0;
135+
if (!isAdded) {
136+
isAdded = getRandomIntInclusive(0, 3) < 3;
137+
}
138+
if (isAdded) {
139+
var newPos = getRandomIntInclusive(0, sTree.size);
140+
sTree.insert(newPos, i);
141+
expectedArray.splice(newPos, 0, i);
142+
} else {
143+
var removedPos = getRandomInt(0, sTree.size);
144+
// sTree.remove(removedPos);
145+
//expectedArray.splice(removedPos, 1);
146+
}
147+
}
148+
expect(sTree.size).toBe(expectedArray.length);
149+
maxHeight = 0;
150+
for (var i = 0; i < sTree.size; ++i) {
151+
var node = sTree.get(i);
152+
maxHeight = Math.max(maxHeight, node.height);
153+
expect(node.value).toBe(expectedArray[i]);
154+
//console.log(node.value, expectedArray[i]);
155+
}
156+
console.log(maxHeight);
157+
checkNil();
122158
});
123159
});

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy