From 05f3e78881ce7651d6ec9739576e71b970170fb7 Mon Sep 17 00:00:00 2001 From: Zak Tsai Date: Mon, 1 Sep 2014 23:03:41 -0500 Subject: [PATCH] Make value optional --- lib/bst.js | 22 ++++++++++++++-------- lib/customUtils.js | 6 ++++++ test/bst.test.js | 26 +++++++++++++------------- 3 files changed, 33 insertions(+), 21 deletions(-) diff --git a/lib/bst.js b/lib/bst.js index 2276f67..c7b1413 100644 --- a/lib/bst.js +++ b/lib/bst.js @@ -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; @@ -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; } @@ -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); } } }; @@ -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 @@ -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; diff --git a/lib/customUtils.js b/lib/customUtils.js index 742ff03..1092cca 100644 --- a/lib/customUtils.js +++ b/lib/customUtils.js @@ -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; diff --git a/test/bst.test.js b/test/bst.test.js index ceb20a9..fcebaad 100644 --- a/test/bst.test.js +++ b/test/bst.test.js @@ -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; } @@ -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);