mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-26 15:11:16 +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 {
|
||||
/**
|
||||
* @param {*} [value]
|
||||
* @param {BinaryTreeNode} [parent]
|
||||
*/
|
||||
constructor(value = null, parent = null) {
|
||||
constructor(value = null) {
|
||||
this.left = null;
|
||||
this.right = null;
|
||||
this.parent = parent;
|
||||
this.parent = null;
|
||||
this.value = value;
|
||||
|
||||
// This comparator is used to compare binary tree nodes with each other.
|
||||
|
@ -6,7 +6,7 @@ export default class BinarySearchTree {
|
||||
*/
|
||||
constructor(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 {
|
||||
/**
|
||||
* @param {*} [value]
|
||||
* @param {BinaryTreeNode} [parent]
|
||||
* @param {function} [compareFunction]
|
||||
*/
|
||||
constructor(value = null, parent = null, compareFunction = undefined) {
|
||||
super(value, parent);
|
||||
constructor(value = null, compareFunction = undefined) {
|
||||
super(value);
|
||||
|
||||
// This comparator is used to compare node values with each other.
|
||||
this.compareFunction = compareFunction;
|
||||
@ -30,14 +29,14 @@ export default class BinarySearchTreeNode extends BinaryTreeNode {
|
||||
if (this.left) {
|
||||
this.left.insert(value);
|
||||
} else {
|
||||
this.setLeft(new BinarySearchTreeNode(value, null, this.compareFunction));
|
||||
this.setLeft(new BinarySearchTreeNode(value, 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, 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 obj3 = { key: 'obj3', value: 3, toString: () => 'obj3' };
|
||||
|
||||
const bstNode = new BinarySearchTreeNode(obj2, null, nodeValueComparatorCallback);
|
||||
const bstNode = new BinarySearchTreeNode(obj2, nodeValueComparatorCallback);
|
||||
bstNode.insert(obj1);
|
||||
|
||||
expect(bstNode.toString()).toBe('obj1,obj2');
|
||||
|
Loading…
Reference in New Issue
Block a user