mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-26 23:21:18 +08:00
Make it possible to attach meta information to binary tree node.
This commit is contained in:
parent
c18fd63a53
commit
33ac110cce
@ -2,13 +2,15 @@ import Comparator from '../../utils/comparator/Comparator';
|
|||||||
|
|
||||||
export default class BinaryTreeNode {
|
export default class BinaryTreeNode {
|
||||||
/**
|
/**
|
||||||
* @param {*} [value]
|
* @param {*} [value] - node value.
|
||||||
|
* @param {Object} meta - any meta information that needs to be attached to the node.
|
||||||
*/
|
*/
|
||||||
constructor(value = null) {
|
constructor(value = null, meta = {}) {
|
||||||
this.left = null;
|
this.left = null;
|
||||||
this.right = null;
|
this.right = null;
|
||||||
this.parent = null;
|
this.parent = null;
|
||||||
this.value = value;
|
this.value = value;
|
||||||
|
this.meta = meta;
|
||||||
|
|
||||||
// This comparator is used to compare binary tree nodes with each other.
|
// This comparator is used to compare binary tree nodes with each other.
|
||||||
this.nodeComparator = new Comparator();
|
this.nodeComparator = new Comparator();
|
||||||
|
@ -196,4 +196,12 @@ describe('BinaryTreeNode', () => {
|
|||||||
expect(node1.toString()).toBe('object_1');
|
expect(node1.toString()).toBe('object_1');
|
||||||
expect(node2.toString()).toBe('[object Object]');
|
expect(node2.toString()).toBe('[object Object]');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should be possible to attach meta information to the node', () => {
|
||||||
|
const redNode = new BinaryTreeNode(1, { color: 'red' });
|
||||||
|
const blackNode = new BinaryTreeNode(2, { color: 'black' });
|
||||||
|
|
||||||
|
expect(redNode.meta.color).toBe('red');
|
||||||
|
expect(blackNode.meta.color).toBe('black');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user