mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-27 15:41:16 +08:00
Make it possible to insert meta information to bst node.
This commit is contained in:
parent
33ac110cce
commit
02d7abcf65
@ -5,7 +5,7 @@ export default class BinaryTreeNode {
|
|||||||
* @param {*} [value] - node value.
|
* @param {*} [value] - node value.
|
||||||
* @param {Object} meta - any meta information that needs to be attached to the node.
|
* @param {Object} meta - any meta information that needs to be attached to the node.
|
||||||
*/
|
*/
|
||||||
constructor(value = null, meta = {}) {
|
constructor(value = null, meta = null) {
|
||||||
this.left = null;
|
this.left = null;
|
||||||
this.right = null;
|
this.right = null;
|
||||||
this.parent = null;
|
this.parent = null;
|
||||||
|
@ -5,7 +5,7 @@ export default class BinarySearchTree {
|
|||||||
* @param {function} [nodeValueCompareFunction]
|
* @param {function} [nodeValueCompareFunction]
|
||||||
*/
|
*/
|
||||||
constructor(nodeValueCompareFunction) {
|
constructor(nodeValueCompareFunction) {
|
||||||
this.root = new BinarySearchTreeNode(null, nodeValueCompareFunction);
|
this.root = new BinarySearchTreeNode(null, null, nodeValueCompareFunction);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -3,11 +3,12 @@ import Comparator from '../../../utils/comparator/Comparator';
|
|||||||
|
|
||||||
export default class BinarySearchTreeNode extends BinaryTreeNode {
|
export default class BinarySearchTreeNode extends BinaryTreeNode {
|
||||||
/**
|
/**
|
||||||
* @param {*} [value]
|
* @param {*} [value] - node value.
|
||||||
* @param {function} [compareFunction]
|
* @param {Object} [meta] - any meta information that is attached to the node.
|
||||||
|
* @param {function} [compareFunction] - comparator function for node values.
|
||||||
*/
|
*/
|
||||||
constructor(value = null, compareFunction = undefined) {
|
constructor(value = null, meta = null, compareFunction = undefined) {
|
||||||
super(value);
|
super(value, meta);
|
||||||
|
|
||||||
// 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;
|
||||||
@ -16,27 +17,30 @@ export default class BinarySearchTreeNode extends BinaryTreeNode {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {*} value
|
* @param {*} value
|
||||||
|
* @param {Object} [meta]
|
||||||
* @return {BinarySearchTreeNode}
|
* @return {BinarySearchTreeNode}
|
||||||
*/
|
*/
|
||||||
insert(value) {
|
insert(value, meta = null) {
|
||||||
if (this.nodeValueComparator.equal(this.value, null)) {
|
if (this.nodeValueComparator.equal(this.value, null)) {
|
||||||
this.value = value;
|
this.value = value;
|
||||||
|
this.meta = meta;
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.nodeValueComparator.lessThan(value, this.value)) {
|
if (this.nodeValueComparator.lessThan(value, this.value)) {
|
||||||
// Insert to the left.
|
// Insert to the left.
|
||||||
if (this.left) {
|
if (this.left) {
|
||||||
this.left.insert(value);
|
this.left.insert(value, meta);
|
||||||
} else {
|
} else {
|
||||||
this.setLeft(new BinarySearchTreeNode(value, this.compareFunction));
|
this.setLeft(new BinarySearchTreeNode(value, meta, 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, meta);
|
||||||
} else {
|
} else {
|
||||||
this.setRight(new BinarySearchTreeNode(value, this.compareFunction));
|
this.setRight(new BinarySearchTreeNode(value, meta, this.compareFunction));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,6 +79,23 @@ describe('BinarySearchTreeNode', () => {
|
|||||||
expect(node.findMin().value).toBe(1);
|
expect(node.findMin().value).toBe(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should be possible to attach meta information to binary search tree nodes', () => {
|
||||||
|
const node = new BinarySearchTreeNode(10, { value: 10 });
|
||||||
|
|
||||||
|
node.insert(20, { value: 20 });
|
||||||
|
node.insert(30, { value: 30 });
|
||||||
|
node.insert(5, { value: 5 });
|
||||||
|
node.insert(40, { value: 40 });
|
||||||
|
node.insert(1, { value: 1 });
|
||||||
|
|
||||||
|
expect(node.meta.value).toBe(10);
|
||||||
|
|
||||||
|
expect(node.findMin()).not.toBeNull();
|
||||||
|
expect(node.findMin().value).toBe(1);
|
||||||
|
expect(node.findMin().meta.value).toBe(1);
|
||||||
|
expect(node.find(30).meta.value).toBe(30);
|
||||||
|
});
|
||||||
|
|
||||||
it('should find node', () => {
|
it('should find node', () => {
|
||||||
const node = new BinarySearchTreeNode(10);
|
const node = new BinarySearchTreeNode(10);
|
||||||
|
|
||||||
@ -188,7 +205,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, nodeValueComparatorCallback);
|
const bstNode = new BinarySearchTreeNode(obj2, null, 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