Skip to content

Commit fcc5463

Browse files
committed
Code style fixes.
1 parent 8d868ae commit fcc5463

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed

src/data-structures/tree/BinaryTreeNode.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import Comparator from '../../utils/comparator/Comparator';
22

33
export default class BinaryTreeNode {
44
/**
5-
* @param {*} value
6-
* @param {BinaryTreeNode} parent
5+
* @param {*} [value]
6+
* @param {BinaryTreeNode} [parent]
77
*/
88
constructor(value = null, parent = null) {
99
this.left = null;

src/data-structures/tree/binary-search-tree/BinarySearchTree.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import BinarySearchTreeNode from './BinarySearchTreeNode';
22

33
export default class BinarySearchTree {
4-
constructor() {
5-
this.root = new BinarySearchTreeNode();
4+
/**
5+
* @param {function} [nodeValueCompareFunction]
6+
*/
7+
constructor(nodeValueCompareFunction) {
8+
this.nodeValueCompareFunction = nodeValueCompareFunction;
9+
this.root = new BinarySearchTreeNode(null, null, this.nodeValueCompareFunction);
610
}
711

812
/**

src/data-structures/tree/binary-search-tree/BinarySearchTreeNode.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ import Comparator from '../../../utils/comparator/Comparator';
33

44
export default class BinarySearchTreeNode extends BinaryTreeNode {
55
/**
6-
* @param {*} value
7-
* @param {BinaryTreeNode} parent
8-
* @param {function} compareFunction
6+
* @param {*} [value]
7+
* @param {BinaryTreeNode} [parent]
8+
* @param {function} [compareFunction]
99
*/
1010
constructor(value = null, parent = null, compareFunction = undefined) {
1111
super(value, parent);
1212

1313
// This comparator is used to compare node values with each other.
14+
this.compareFunction = compareFunction;
1415
this.nodeValueComparator = new Comparator(compareFunction);
1516
}
1617

@@ -29,14 +30,14 @@ export default class BinarySearchTreeNode extends BinaryTreeNode {
2930
if (this.left) {
3031
this.left.insert(value);
3132
} else {
32-
this.setLeft(new BinarySearchTreeNode(value));
33+
this.setLeft(new BinarySearchTreeNode(value, null, this.compareFunction));
3334
}
3435
} else if (this.nodeValueComparator.greaterThan(value, this.value)) {
3536
// Insert to the right.
3637
if (this.right) {
3738
this.right.insert(value);
3839
} else {
39-
this.setRight(new BinarySearchTreeNode(value));
40+
this.setRight(new BinarySearchTreeNode(value, null, this.compareFunction));
4041
}
4142
}
4243

src/data-structures/tree/binary-search-tree/__test__/BinarySearchTree.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,29 @@ describe('BinarySearchTree', () => {
4646
bst.remove(20);
4747
expect(bst.toString()).toBe('10');
4848
});
49+
50+
it('should insert object values', () => {
51+
const nodeValueComparatorCallback = (a, b) => {
52+
const normalizedA = a || { value: null };
53+
const normalizedB = b || { value: null };
54+
55+
if (normalizedA.value === normalizedB.value) {
56+
return 0;
57+
}
58+
59+
return normalizedA.value < normalizedB.value ? -1 : 1;
60+
};
61+
62+
const obj1 = { key: 'obj1', value: 1, toString: () => 'obj1' };
63+
const obj2 = { key: 'obj2', value: 2, toString: () => 'obj2' };
64+
const obj3 = { key: 'obj3', value: 3, toString: () => 'obj3' };
65+
66+
const bst = new BinarySearchTree(nodeValueComparatorCallback);
67+
68+
bst.insert(obj2);
69+
bst.insert(obj3);
70+
bst.insert(obj1);
71+
72+
expect(bst.toString()).toBe('obj1,obj2,obj3');
73+
});
4974
});

0 commit comments

Comments
 (0)