From 33ac110ccea0427239cc36024912ef4d56faa176 Mon Sep 17 00:00:00 2001 From: Oleksii Trekhleb Date: Thu, 31 May 2018 07:03:33 +0300 Subject: [PATCH] Make it possible to attach meta information to binary tree node. --- src/data-structures/tree/BinaryTreeNode.js | 6 ++++-- src/data-structures/tree/__test__/BinaryTreeNode.test.js | 8 ++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/data-structures/tree/BinaryTreeNode.js b/src/data-structures/tree/BinaryTreeNode.js index e894792c..f7a8c817 100644 --- a/src/data-structures/tree/BinaryTreeNode.js +++ b/src/data-structures/tree/BinaryTreeNode.js @@ -2,13 +2,15 @@ import Comparator from '../../utils/comparator/Comparator'; 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.right = null; this.parent = null; this.value = value; + this.meta = meta; // This comparator is used to compare binary tree nodes with each other. this.nodeComparator = new Comparator(); diff --git a/src/data-structures/tree/__test__/BinaryTreeNode.test.js b/src/data-structures/tree/__test__/BinaryTreeNode.test.js index 8474798f..ea0dde6c 100644 --- a/src/data-structures/tree/__test__/BinaryTreeNode.test.js +++ b/src/data-structures/tree/__test__/BinaryTreeNode.test.js @@ -196,4 +196,12 @@ describe('BinaryTreeNode', () => { expect(node1.toString()).toBe('object_1'); 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'); + }); });