Skip to content

Commit

Permalink
fix remove bug
Browse files Browse the repository at this point in the history
  • Loading branch information
sisobus committed Nov 12, 2018
1 parent d13b14b commit c6446cb
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/splaytree.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,21 +197,25 @@ export class SplayTree {
x.right = p.right;
p.right.parent = x;
p = null;
this._size--;
return true;
}
this._root = p.left;
this._root.parent = null;
p = null;
this._size--;
return true;
}
if (p.right) {
this._root = p.right;
this._root.parent = null;
p = null;
this._size--;
return true;
}
p = null;
this._root = null;
this._size = 0;
return true;
}
clear() {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "splaytreejs",
"version": "1.0.4",
"version": "1.0.5",
"main": "dist/index.js",
"browser": "dist/splaytree.min.js",
"repository": "https://github.com/sisobus/SplayTree.git",
Expand Down
17 changes: 17 additions & 0 deletions tests/remove.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,23 @@ describe ("remove", () => {
assert.equal(output.toString(), expected.toString());
});

it ('should be size is zero when all node is removed', () => {
const tree = new SplayTree();
let l = [3, 2, 1, 5, 8, 4, 11, 6];
for (let i = 0; i < l.length; i++) {
tree.insert(l[i]);
}
const input = [5, 1, 2, 3, 4, 8, 11, 6, 7];
const expected = [7, 6, 5, 4, 3, 2, 1, 0, 0];
const output = [];

for (let i = 0; i < input.length; i++) {
tree.remove(input[i]);
output.push(tree.size);
}
assert.equal(output.toString(), expected.toString());
});

it ('should maintain shape of Splay Tree', () => {
const tree = new SplayTree();
let l = [3, 2, 1, 5, 8, 4, 11, 6];
Expand Down

0 comments on commit c6446cb

Please sign in to comment.