Remove parent parameter from binary tree node constructor to simplify syntax.

This commit is contained in:
Oleksii Trekhleb 2018-05-30 08:16:41 +03:00
parent fcc546347d
commit b7e27b2f07
4 changed files with 8 additions and 10 deletions

View File

@ -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.

View File

@ -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);
} }
/** /**

View File

@ -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));
} }
} }

View File

@ -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');