Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tree#insert: replace data with the supplied data if it compares equal #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/bintree.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ BinTree.prototype.insert = function(data) {

// stop if found
if(this._comparator(node.data, data) === 0) {
// overwrite the node with the supplied data
node.data = data;
return false;
}

Expand Down
2 changes: 2 additions & 0 deletions lib/rbtree.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ RBTree.prototype.insert = function(data) {

// stop if found
if(cmp === 0) {
// overwrite the node with the supplied data
node.data = data;
break;
}

Expand Down
10 changes: 10 additions & 0 deletions test/test_correctness.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@ TREES.forEach(function(tree) {
assert.done();
};
});

/* Tweaked behaviour: want reinserting a node that compares equal with an existing node to
* still replace the data */
test_funcs[tree + "_test_insert_matching"] = function(assert) {
var tree = new tree_class((a, b) => a.score - b.score);
tree.insert({score: 1, id: 1});
tree.insert({score: 1, id: 2});
assert.equal(tree.find({score: 1}).id, 2);
assert.done();
};
});

exports.correctness = test_funcs;