diff --git a/src/data-structures/tree/BinaryTreeNode.js b/src/data-structures/tree/BinaryTreeNode.js index f7a8c817..fbd53e70 100644 --- a/src/data-structures/tree/BinaryTreeNode.js +++ b/src/data-structures/tree/BinaryTreeNode.js @@ -5,7 +5,7 @@ export default class BinaryTreeNode { * @param {*} [value] - node value. * @param {Object} meta - any meta information that needs to be attached to the node. */ - constructor(value = null, meta = {}) { + constructor(value = null, meta = null) { this.left = null; this.right = null; this.parent = null; diff --git a/src/data-structures/tree/binary-search-tree/BinarySearchTree.js b/src/data-structures/tree/binary-search-tree/BinarySearchTree.js index 53ce46e9..96fffd52 100644 --- a/src/data-structures/tree/binary-search-tree/BinarySearchTree.js +++ b/src/data-structures/tree/binary-search-tree/BinarySearchTree.js @@ -5,7 +5,7 @@ export default class BinarySearchTree { * @param {function} [nodeValueCompareFunction] */ constructor(nodeValueCompareFunction) { - this.root = new BinarySearchTreeNode(null, nodeValueCompareFunction); + this.root = new BinarySearchTreeNode(null, null, nodeValueCompareFunction); } /** diff --git a/src/data-structures/tree/binary-search-tree/BinarySearchTreeNode.js b/src/data-structures/tree/binary-search-tree/BinarySearchTreeNode.js index 8f457e3b..97391d71 100644 --- a/src/data-structures/tree/binary-search-tree/BinarySearchTreeNode.js +++ b/src/data-structures/tree/binary-search-tree/BinarySearchTreeNode.js @@ -3,11 +3,12 @@ import Comparator from '../../../utils/comparator/Comparator'; export default class BinarySearchTreeNode extends BinaryTreeNode { /** - * @param {*} [value] - * @param {function} [compareFunction] + * @param {*} [value] - node value. + * @param {Object} [meta] - any meta information that is attached to the node. + * @param {function} [compareFunction] - comparator function for node values. */ - constructor(value = null, compareFunction = undefined) { - super(value); + constructor(value = null, meta = null, compareFunction = undefined) { + super(value, meta); // This comparator is used to compare node values with each other. this.compareFunction = compareFunction; @@ -16,27 +17,30 @@ export default class BinarySearchTreeNode extends BinaryTreeNode { /** * @param {*} value + * @param {Object} [meta] * @return {BinarySearchTreeNode} */ - insert(value) { + insert(value, meta = null) { if (this.nodeValueComparator.equal(this.value, null)) { this.value = value; + this.meta = meta; + return this; } if (this.nodeValueComparator.lessThan(value, this.value)) { // Insert to the left. if (this.left) { - this.left.insert(value); + this.left.insert(value, meta); } else { - this.setLeft(new BinarySearchTreeNode(value, this.compareFunction)); + this.setLeft(new BinarySearchTreeNode(value, meta, this.compareFunction)); } } else if (this.nodeValueComparator.greaterThan(value, this.value)) { // Insert to the right. if (this.right) { - this.right.insert(value); + this.right.insert(value, meta); } else { - this.setRight(new BinarySearchTreeNode(value, this.compareFunction)); + this.setRight(new BinarySearchTreeNode(value, meta, this.compareFunction)); } } diff --git a/src/data-structures/tree/binary-search-tree/__test__/BinarySearchTreeNode.test.js b/src/data-structures/tree/binary-search-tree/__test__/BinarySearchTreeNode.test.js index dfe733c8..1862c1f3 100644 --- a/src/data-structures/tree/binary-search-tree/__test__/BinarySearchTreeNode.test.js +++ b/src/data-structures/tree/binary-search-tree/__test__/BinarySearchTreeNode.test.js @@ -79,6 +79,23 @@ describe('BinarySearchTreeNode', () => { expect(node.findMin().value).toBe(1); }); + it('should be possible to attach meta information to binary search tree nodes', () => { + const node = new BinarySearchTreeNode(10, { value: 10 }); + + node.insert(20, { value: 20 }); + node.insert(30, { value: 30 }); + node.insert(5, { value: 5 }); + node.insert(40, { value: 40 }); + node.insert(1, { value: 1 }); + + expect(node.meta.value).toBe(10); + + expect(node.findMin()).not.toBeNull(); + expect(node.findMin().value).toBe(1); + expect(node.findMin().meta.value).toBe(1); + expect(node.find(30).meta.value).toBe(30); + }); + it('should find node', () => { const node = new BinarySearchTreeNode(10); @@ -188,7 +205,7 @@ describe('BinarySearchTreeNode', () => { const obj2 = { key: 'obj2', value: 2, toString: () => 'obj2' }; const obj3 = { key: 'obj3', value: 3, toString: () => 'obj3' }; - const bstNode = new BinarySearchTreeNode(obj2, nodeValueComparatorCallback); + const bstNode = new BinarySearchTreeNode(obj2, null, nodeValueComparatorCallback); bstNode.insert(obj1); expect(bstNode.toString()).toBe('obj1,obj2');