mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-26 23:21:18 +08:00
Code style fixes.
This commit is contained in:
parent
8d868ae582
commit
fcc546347d
@ -2,8 +2,8 @@ import Comparator from '../../utils/comparator/Comparator';
|
|||||||
|
|
||||||
export default class BinaryTreeNode {
|
export default class BinaryTreeNode {
|
||||||
/**
|
/**
|
||||||
* @param {*} value
|
* @param {*} [value]
|
||||||
* @param {BinaryTreeNode} parent
|
* @param {BinaryTreeNode} [parent]
|
||||||
*/
|
*/
|
||||||
constructor(value = null, parent = null) {
|
constructor(value = null, parent = null) {
|
||||||
this.left = null;
|
this.left = null;
|
||||||
|
@ -1,8 +1,12 @@
|
|||||||
import BinarySearchTreeNode from './BinarySearchTreeNode';
|
import BinarySearchTreeNode from './BinarySearchTreeNode';
|
||||||
|
|
||||||
export default class BinarySearchTree {
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -3,14 +3,15 @@ import Comparator from '../../../utils/comparator/Comparator';
|
|||||||
|
|
||||||
export default class BinarySearchTreeNode extends BinaryTreeNode {
|
export default class BinarySearchTreeNode extends BinaryTreeNode {
|
||||||
/**
|
/**
|
||||||
* @param {*} value
|
* @param {*} [value]
|
||||||
* @param {BinaryTreeNode} parent
|
* @param {BinaryTreeNode} [parent]
|
||||||
* @param {function} compareFunction
|
* @param {function} [compareFunction]
|
||||||
*/
|
*/
|
||||||
constructor(value = null, parent = null, compareFunction = undefined) {
|
constructor(value = null, parent = null, compareFunction = undefined) {
|
||||||
super(value, parent);
|
super(value, parent);
|
||||||
|
|
||||||
// This comparator is used to compare node values with each other.
|
// This comparator is used to compare node values with each other.
|
||||||
|
this.compareFunction = compareFunction;
|
||||||
this.nodeValueComparator = new Comparator(compareFunction);
|
this.nodeValueComparator = new Comparator(compareFunction);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -29,14 +30,14 @@ export default class BinarySearchTreeNode extends BinaryTreeNode {
|
|||||||
if (this.left) {
|
if (this.left) {
|
||||||
this.left.insert(value);
|
this.left.insert(value);
|
||||||
} else {
|
} else {
|
||||||
this.setLeft(new BinarySearchTreeNode(value));
|
this.setLeft(new BinarySearchTreeNode(value, null, this.compareFunction));
|
||||||
}
|
}
|
||||||
} else if (this.nodeValueComparator.greaterThan(value, this.value)) {
|
} else if (this.nodeValueComparator.greaterThan(value, this.value)) {
|
||||||
// Insert to the right.
|
// Insert to the right.
|
||||||
if (this.right) {
|
if (this.right) {
|
||||||
this.right.insert(value);
|
this.right.insert(value);
|
||||||
} else {
|
} else {
|
||||||
this.setRight(new BinarySearchTreeNode(value));
|
this.setRight(new BinarySearchTreeNode(value, null, this.compareFunction));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,4 +46,29 @@ describe('BinarySearchTree', () => {
|
|||||||
bst.remove(20);
|
bst.remove(20);
|
||||||
expect(bst.toString()).toBe('10');
|
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');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user