Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions lib/bst.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function BinarySearchTree (options) {
this.right = null;
this.parent = options.parent !== undefined ? options.parent : null;
if (options.hasOwnProperty('key')) { this.key = options.key; }
this.data = options.hasOwnProperty('value') ? [options.value] : [];
this.data = customUtils.isDef(options.value) ? [options.value] : [];
this.unique = options.unique || false;

this.compareKeys = options.compareKeys || customUtils.defaultCompareKeysFunction;
Expand Down Expand Up @@ -202,12 +202,14 @@ BinarySearchTree.prototype.createRightChild = function (options) {

/**
* Insert a new element
* @param {Key} key
* @param {Value=} value Optional
*/
BinarySearchTree.prototype.insert = function (key, value) {
// Empty tree, insert as root
if (!this.hasOwnProperty('key')) {
this.key = key;
this.data.push(value);
customUtils.isDef(value) && this.data.push(value);
return;
}

Expand All @@ -219,24 +221,28 @@ BinarySearchTree.prototype.insert = function (key, value) {
, errorType: 'uniqueViolated'
};
} else {
this.data.push(value);
customUtils.isDef(value) && this.data.push(value);
}
return;
}

var childNode = { key: key };
if (customUtils.isDef(value)) {
childNode.value = value;
}
if (this.compareKeys(key, this.key) < 0) {
// Insert in left subtree
if (this.left) {
this.left.insert(key, value);
} else {
this.createLeftChild({ key: key, value: value });
this.createLeftChild(childNode);
}
} else {
// Insert in right subtree
if (this.right) {
this.right.insert(key, value);
} else {
this.createRightChild({ key: key, value: value });
this.createRightChild(childNode);
}
}
};
Expand Down Expand Up @@ -430,7 +436,7 @@ BinarySearchTree.prototype.deleteIfOnlyOneChild = function () {
/**
* Delete a key or just a value
* @param {Key} key
* @param {Value} value Optional. If not set, the whole key is deleted. If set, only this value is deleted
* @param {Value=} value Optional. If not set, the whole key is deleted. If set, only this value is deleted
*/
BinarySearchTree.prototype.delete = function (key, value) {
var newData = [], replaceWith
Expand All @@ -452,8 +458,8 @@ BinarySearchTree.prototype.delete = function (key, value) {
if (!this.compareKeys(key, this.key) === 0) { return; }

// Delete only a value
if (this.data.length > 1 && value !== undefined) {
this.data.forEach(function (d) {
if (this.data.length > 1 && customUtils.isDef(value)) {
this.data.forEach(function(d) {
if (!self.checkValueEquality(d, value)) { newData.push(d); }
});
self.data = newData;
Expand Down
6 changes: 6 additions & 0 deletions lib/customUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,9 @@ function defaultCheckValueEquality (a, b) {
return a === b;
}
module.exports.defaultCheckValueEquality = defaultCheckValueEquality;


function isDef(v) {
return v !== undefined;
}
module.exports.isDef = isDef;
26 changes: 13 additions & 13 deletions test/bst.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ describe('Binary search tree', function () {
}); // ==== End of 'Deletion' ==== //


it('Can use undefined as key and value', function () {
it('Can use undefined as key but not value', function () {
function compareKeys (a, b) {
if (a === undefined && b === undefined) { return 0; }
if (a === undefined) { return -1; }
Expand All @@ -806,50 +806,50 @@ describe('Binary search tree', function () {

var bst = new BinarySearchTree({ compareKeys: compareKeys });

bst.insert(2, undefined);
bst.insert(2);
bst.checkIsBST();
bst.getNumberOfKeys().should.equal(1);
assert.deepEqual(bst.search(2), [undefined]);
assert.deepEqual(bst.search(2), []);
assert.deepEqual(bst.search(undefined), []);

bst.insert(undefined, 'hello');
bst.checkIsBST();
bst.getNumberOfKeys().should.equal(2);
assert.deepEqual(bst.search(2), [undefined]);
assert.deepEqual(bst.search(2), []);
assert.deepEqual(bst.search(undefined), ['hello']);

bst.insert(undefined, 'world');
bst.checkIsBST();
bst.getNumberOfKeys().should.equal(2);
assert.deepEqual(bst.search(2), [undefined]);
assert.deepEqual(bst.search(2), []);
assert.deepEqual(bst.search(undefined), ['hello', 'world']);

bst.insert(4, undefined);
bst.insert(4);
bst.checkIsBST();
bst.getNumberOfKeys().should.equal(3);
assert.deepEqual(bst.search(2), [undefined]);
assert.deepEqual(bst.search(4), [undefined]);
assert.deepEqual(bst.search(2), []);
assert.deepEqual(bst.search(4), []);
assert.deepEqual(bst.search(undefined), ['hello', 'world']);

bst.delete(undefined, 'hello');
bst.checkIsBST();
bst.getNumberOfKeys().should.equal(3);
assert.deepEqual(bst.search(2), [undefined]);
assert.deepEqual(bst.search(4), [undefined]);
assert.deepEqual(bst.search(2), []);
assert.deepEqual(bst.search(4), []);
assert.deepEqual(bst.search(undefined), ['world']);

bst.delete(undefined);
bst.checkIsBST();
bst.getNumberOfKeys().should.equal(2);
assert.deepEqual(bst.search(2), [undefined]);
assert.deepEqual(bst.search(4), [undefined]);
assert.deepEqual(bst.search(2), []);
assert.deepEqual(bst.search(4), []);
assert.deepEqual(bst.search(undefined), []);

bst.delete(2, undefined);
bst.checkIsBST();
bst.getNumberOfKeys().should.equal(1);
assert.deepEqual(bst.search(2), []);
assert.deepEqual(bst.search(4), [undefined]);
assert.deepEqual(bst.search(4), []);
assert.deepEqual(bst.search(undefined), []);

bst.delete(4);
Expand Down