From fcc546347d124992f339d921f6c9df8d380f9dea Mon Sep 17 00:00:00 2001 From: Oleksii Trekhleb Date: Wed, 30 May 2018 08:00:25 +0300 Subject: [PATCH] Code style fixes. --- src/data-structures/tree/BinaryTreeNode.js | 4 +-- .../binary-search-tree/BinarySearchTree.js | 8 ++++-- .../BinarySearchTreeNode.js | 11 ++++---- .../__test__/BinarySearchTree.test.js | 25 +++++++++++++++++++ 4 files changed, 39 insertions(+), 9 deletions(-) diff --git a/src/data-structures/tree/BinaryTreeNode.js b/src/data-structures/tree/BinaryTreeNode.js index 847626a3..8796abc1 100644 --- a/src/data-structures/tree/BinaryTreeNode.js +++ b/src/data-structures/tree/BinaryTreeNode.js @@ -2,8 +2,8 @@ import Comparator from '../../utils/comparator/Comparator'; export default class BinaryTreeNode { /** - * @param {*} value - * @param {BinaryTreeNode} parent + * @param {*} [value] + * @param {BinaryTreeNode} [parent] */ constructor(value = null, parent = null) { this.left = null; diff --git a/src/data-structures/tree/binary-search-tree/BinarySearchTree.js b/src/data-structures/tree/binary-search-tree/BinarySearchTree.js index 1c2937ee..4c2f855d 100644 --- a/src/data-structures/tree/binary-search-tree/BinarySearchTree.js +++ b/src/data-structures/tree/binary-search-tree/BinarySearchTree.js @@ -1,8 +1,12 @@ import BinarySearchTreeNode from './BinarySearchTreeNode'; export default class BinarySearchTree { - constructor() { - this.root = new BinarySearchTreeNode(); + /** + * @param {function} [nodeValueCompareFunction] + */ + constructor(nodeValueCompareFunction) { + this.nodeValueCompareFunction = nodeValueCompareFunction; + this.root = new BinarySearchTreeNode(null, null, this.nodeValueCompareFunction); } /** diff --git a/src/data-structures/tree/binary-search-tree/BinarySearchTreeNode.js b/src/data-structures/tree/binary-search-tree/BinarySearchTreeNode.js index f71a7b5b..f3726cb2 100644 --- a/src/data-structures/tree/binary-search-tree/BinarySearchTreeNode.js +++ b/src/data-structures/tree/binary-search-tree/BinarySearchTreeNode.js @@ -3,14 +3,15 @@ import Comparator from '../../../utils/comparator/Comparator'; export default class BinarySearchTreeNode extends BinaryTreeNode { /** - * @param {*} value - * @param {BinaryTreeNode} parent - * @param {function} compareFunction + * @param {*} [value] + * @param {BinaryTreeNode} [parent] + * @param {function} [compareFunction] */ constructor(value = null, parent = null, compareFunction = undefined) { super(value, parent); // This comparator is used to compare node values with each other. + this.compareFunction = compareFunction; this.nodeValueComparator = new Comparator(compareFunction); } @@ -29,14 +30,14 @@ export default class BinarySearchTreeNode extends BinaryTreeNode { if (this.left) { this.left.insert(value); } else { - this.setLeft(new BinarySearchTreeNode(value)); + this.setLeft(new BinarySearchTreeNode(value, null, this.compareFunction)); } } else if (this.nodeValueComparator.greaterThan(value, this.value)) { // Insert to the right. if (this.right) { this.right.insert(value); } else { - this.setRight(new BinarySearchTreeNode(value)); + this.setRight(new BinarySearchTreeNode(value, null, this.compareFunction)); } } diff --git a/src/data-structures/tree/binary-search-tree/__test__/BinarySearchTree.test.js b/src/data-structures/tree/binary-search-tree/__test__/BinarySearchTree.test.js index bfc7b976..af93fc50 100644 --- a/src/data-structures/tree/binary-search-tree/__test__/BinarySearchTree.test.js +++ b/src/data-structures/tree/binary-search-tree/__test__/BinarySearchTree.test.js @@ -46,4 +46,29 @@ describe('BinarySearchTree', () => { bst.remove(20); expect(bst.toString()).toBe('10'); }); + + it('should insert object values', () => { + const nodeValueComparatorCallback = (a, b) => { + const normalizedA = a || { value: null }; + const normalizedB = b || { value: null }; + + if (normalizedA.value === normalizedB.value) { + return 0; + } + + return normalizedA.value < normalizedB.value ? -1 : 1; + }; + + const obj1 = { key: 'obj1', value: 1, toString: () => 'obj1' }; + const obj2 = { key: 'obj2', value: 2, toString: () => 'obj2' }; + const obj3 = { key: 'obj3', value: 3, toString: () => 'obj3' }; + + const bst = new BinarySearchTree(nodeValueComparatorCallback); + + bst.insert(obj2); + bst.insert(obj3); + bst.insert(obj1); + + expect(bst.toString()).toBe('obj1,obj2,obj3'); + }); });