mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-26 23:21:18 +08:00
Remove parent parameter from binary tree node constructor to simplify syntax.
This commit is contained in:
parent
fcc546347d
commit
b7e27b2f07
@ -3,12 +3,11 @@ import Comparator from '../../utils/comparator/Comparator';
|
|||||||
export default class BinaryTreeNode {
|
export default class BinaryTreeNode {
|
||||||
/**
|
/**
|
||||||
* @param {*} [value]
|
* @param {*} [value]
|
||||||
* @param {BinaryTreeNode} [parent]
|
|
||||||
*/
|
*/
|
||||||
constructor(value = null, parent = null) {
|
constructor(value = null) {
|
||||||
this.left = null;
|
this.left = null;
|
||||||
this.right = null;
|
this.right = null;
|
||||||
this.parent = parent;
|
this.parent = null;
|
||||||
this.value = value;
|
this.value = value;
|
||||||
|
|
||||||
// This comparator is used to compare binary tree nodes with each other.
|
// This comparator is used to compare binary tree nodes with each other.
|
||||||
|
@ -6,7 +6,7 @@ export default class BinarySearchTree {
|
|||||||
*/
|
*/
|
||||||
constructor(nodeValueCompareFunction) {
|
constructor(nodeValueCompareFunction) {
|
||||||
this.nodeValueCompareFunction = nodeValueCompareFunction;
|
this.nodeValueCompareFunction = nodeValueCompareFunction;
|
||||||
this.root = new BinarySearchTreeNode(null, null, this.nodeValueCompareFunction);
|
this.root = new BinarySearchTreeNode(null, this.nodeValueCompareFunction);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -4,11 +4,10 @@ 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 {function} [compareFunction]
|
* @param {function} [compareFunction]
|
||||||
*/
|
*/
|
||||||
constructor(value = null, parent = null, compareFunction = undefined) {
|
constructor(value = null, compareFunction = undefined) {
|
||||||
super(value, parent);
|
super(value);
|
||||||
|
|
||||||
// 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.compareFunction = compareFunction;
|
||||||
@ -30,14 +29,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, null, this.compareFunction));
|
this.setLeft(new BinarySearchTreeNode(value, 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, null, this.compareFunction));
|
this.setRight(new BinarySearchTreeNode(value, this.compareFunction));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -188,7 +188,7 @@ describe('BinarySearchTreeNode', () => {
|
|||||||
const obj2 = { key: 'obj2', value: 2, toString: () => 'obj2' };
|
const obj2 = { key: 'obj2', value: 2, toString: () => 'obj2' };
|
||||||
const obj3 = { key: 'obj3', value: 3, toString: () => 'obj3' };
|
const obj3 = { key: 'obj3', value: 3, toString: () => 'obj3' };
|
||||||
|
|
||||||
const bstNode = new BinarySearchTreeNode(obj2, null, nodeValueComparatorCallback);
|
const bstNode = new BinarySearchTreeNode(obj2, nodeValueComparatorCallback);
|
||||||
bstNode.insert(obj1);
|
bstNode.insert(obj1);
|
||||||
|
|
||||||
expect(bstNode.toString()).toBe('obj1,obj2');
|
expect(bstNode.toString()).toBe('obj1,obj2');
|
||||||
|
Loading…
Reference in New Issue
Block a user