Skip to content

Commit 19a12da

Browse files
committed
Fixes of the Nil pointer problem,
testing push 10000 elements at the end and get the 10000 elements and check it.
1 parent 46f29d4 commit 19a12da

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
NL CL
108108
*/
109109
node.right = childNode.left;
110-
node.right.parent = node;
110+
if (node.right !== Nil) node.right.parent = node;
111111
childNode.left = node;
112112
updateChild(node, childNode) //Access node.parent
113113
node.parent = childNode;
@@ -132,7 +132,7 @@
132132
CR NR
133133
*/
134134
node.left = childNode.right;
135-
node.left.parent = node;
135+
if (node.left !== Nil) node.left.parent = node;
136136
childNode.right = node;
137137
updateChild(node, childNode) //Access node.parent
138138
node.parent = childNode;
@@ -171,6 +171,7 @@
171171
node = node.left;
172172
} else {
173173
pos -= node.left.size;
174+
--pos; //The node element should be decrement by 1
174175
node = node.right;
175176
}
176177
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,17 @@ describe('SBTree', function () {
8181
expect(root.left).toBe(Nil);
8282
updateChild(Nil, right);
8383
expect(right.parent).toBe(Nil);
84+
checkNil();
85+
});
86+
87+
it('push and get 100000 elements', function () {
88+
var sTree = new SBTree();
89+
for (var i = 0; i < 100000; ++i) {
90+
sTree.push(i);
91+
}
92+
checkNil();
93+
for (var i = 0; i < 100000; ++i) {
94+
expect(sTree.get(i).value).toBe(i);
95+
}
8496
});
8597
});

0 commit comments

Comments
 (0)