Skip to content

Commit 02d7abc

Browse files
committed
Make it possible to insert meta information to bst node.
1 parent 33ac110 commit 02d7abc

File tree

4 files changed

+33
-12
lines changed

4 files changed

+33
-12
lines changed

src/data-structures/tree/BinaryTreeNode.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default class BinaryTreeNode {
55
* @param {*} [value] - node value.
66
* @param {Object} meta - any meta information that needs to be attached to the node.
77
*/
8-
constructor(value = null, meta = {}) {
8+
constructor(value = null, meta = null) {
99
this.left = null;
1010
this.right = null;
1111
this.parent = null;

src/data-structures/tree/binary-search-tree/BinarySearchTree.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default class BinarySearchTree {
55
* @param {function} [nodeValueCompareFunction]
66
*/
77
constructor(nodeValueCompareFunction) {
8-
this.root = new BinarySearchTreeNode(null, nodeValueCompareFunction);
8+
this.root = new BinarySearchTreeNode(null, null, nodeValueCompareFunction);
99
}
1010

1111
/**

src/data-structures/tree/binary-search-tree/BinarySearchTreeNode.js

+13-9
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import Comparator from '../../../utils/comparator/Comparator';
33

44
export default class BinarySearchTreeNode extends BinaryTreeNode {
55
/**
6-
* @param {*} [value]
7-
* @param {function} [compareFunction]
6+
* @param {*} [value] - node value.
7+
* @param {Object} [meta] - any meta information that is attached to the node.
8+
* @param {function} [compareFunction] - comparator function for node values.
89
*/
9-
constructor(value = null, compareFunction = undefined) {
10-
super(value);
10+
constructor(value = null, meta = null, compareFunction = undefined) {
11+
super(value, meta);
1112

1213
// This comparator is used to compare node values with each other.
1314
this.compareFunction = compareFunction;
@@ -16,27 +17,30 @@ export default class BinarySearchTreeNode extends BinaryTreeNode {
1617

1718
/**
1819
* @param {*} value
20+
* @param {Object} [meta]
1921
* @return {BinarySearchTreeNode}
2022
*/
21-
insert(value) {
23+
insert(value, meta = null) {
2224
if (this.nodeValueComparator.equal(this.value, null)) {
2325
this.value = value;
26+
this.meta = meta;
27+
2428
return this;
2529
}
2630

2731
if (this.nodeValueComparator.lessThan(value, this.value)) {
2832
// Insert to the left.
2933
if (this.left) {
30-
this.left.insert(value);
34+
this.left.insert(value, meta);
3135
} else {
32-
this.setLeft(new BinarySearchTreeNode(value, this.compareFunction));
36+
this.setLeft(new BinarySearchTreeNode(value, meta, this.compareFunction));
3337
}
3438
} else if (this.nodeValueComparator.greaterThan(value, this.value)) {
3539
// Insert to the right.
3640
if (this.right) {
37-
this.right.insert(value);
41+
this.right.insert(value, meta);
3842
} else {
39-
this.setRight(new BinarySearchTreeNode(value, this.compareFunction));
43+
this.setRight(new BinarySearchTreeNode(value, meta, this.compareFunction));
4044
}
4145
}
4246

src/data-structures/tree/binary-search-tree/__test__/BinarySearchTreeNode.test.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,23 @@ describe('BinarySearchTreeNode', () => {
7979
expect(node.findMin().value).toBe(1);
8080
});
8181

82+
it('should be possible to attach meta information to binary search tree nodes', () => {
83+
const node = new BinarySearchTreeNode(10, { value: 10 });
84+
85+
node.insert(20, { value: 20 });
86+
node.insert(30, { value: 30 });
87+
node.insert(5, { value: 5 });
88+
node.insert(40, { value: 40 });
89+
node.insert(1, { value: 1 });
90+
91+
expect(node.meta.value).toBe(10);
92+
93+
expect(node.findMin()).not.toBeNull();
94+
expect(node.findMin().value).toBe(1);
95+
expect(node.findMin().meta.value).toBe(1);
96+
expect(node.find(30).meta.value).toBe(30);
97+
});
98+
8299
it('should find node', () => {
83100
const node = new BinarySearchTreeNode(10);
84101

@@ -188,7 +205,7 @@ describe('BinarySearchTreeNode', () => {
188205
const obj2 = { key: 'obj2', value: 2, toString: () => 'obj2' };
189206
const obj3 = { key: 'obj3', value: 3, toString: () => 'obj3' };
190207

191-
const bstNode = new BinarySearchTreeNode(obj2, nodeValueComparatorCallback);
208+
const bstNode = new BinarySearchTreeNode(obj2, null, nodeValueComparatorCallback);
192209
bstNode.insert(obj1);
193210

194211
expect(bstNode.toString()).toBe('obj1,obj2');

0 commit comments

Comments
 (0)