Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 33ac110

Browse files
committed
Make it possible to attach meta information to binary tree node.
1 parent c18fd63 commit 33ac110

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

‎src/data-structures/tree/BinaryTreeNode.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ import Comparator from '../../utils/comparator/Comparator';
22

33
export default class BinaryTreeNode {
44
/**
5-
* @param {*} [value]
5+
* @param {*} [value] - node value.
6+
* @param {Object} meta - any meta information that needs to be attached to the node.
67
*/
7-
constructor(value = null) {
8+
constructor(value = null,meta={}) {
89
this.left = null;
910
this.right = null;
1011
this.parent = null;
1112
this.value = value;
13+
this.meta = meta;
1214

1315
// This comparator is used to compare binary tree nodes with each other.
1416
this.nodeComparator = new Comparator();

‎src/data-structures/tree/__test__/BinaryTreeNode.test.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,4 +196,12 @@ describe('BinaryTreeNode', () => {
196196
expect(node1.toString()).toBe('object_1');
197197
expect(node2.toString()).toBe('[object Object]');
198198
});
199+
200+
it('should be possible to attach meta information to the node', () => {
201+
const redNode = new BinaryTreeNode(1, { color: 'red' });
202+
const blackNode = new BinaryTreeNode(2, { color: 'black' });
203+
204+
expect(redNode.meta.color).toBe('red');
205+
expect(blackNode.meta.color).toBe('black');
206+
});
199207
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /