Skip to content

Commit 7a42654

Browse files
committed
Use Map for node meta data.
1 parent e572de6 commit 7a42654

File tree

5 files changed

+47
-73
lines changed

5 files changed

+47
-73
lines changed

src/data-structures/tree/BinaryTreeNode.js

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ import Comparator from '../../utils/comparator/Comparator';
33
export default class BinaryTreeNode {
44
/**
55
* @param {*} [value] - node value.
6-
* @param {Object} meta - any meta information that needs to be attached to the node.
76
*/
8-
constructor(value = null, meta = {}) {
7+
constructor(value = null) {
98
this.left = null;
109
this.right = null;
1110
this.parent = null;
1211
this.value = value;
13-
this.meta = meta;
12+
13+
// Any node related meta information may be stored here.
14+
this.meta = new Map();
1415

1516
// This comparator is used to compare binary tree nodes with each other.
1617
this.nodeComparator = new Comparator();
@@ -157,29 +158,6 @@ export default class BinaryTreeNode {
157158
return traverse;
158159
}
159160

160-
/**
161-
* @param {string} property
162-
* @param {*} value
163-
* @return {BinaryTreeNode}
164-
*/
165-
setMeta(property, value) {
166-
this.meta[property] = value;
167-
168-
return this;
169-
}
170-
171-
/**
172-
* @param property
173-
* @return {*}
174-
*/
175-
getMeta(property) {
176-
if (!this.meta || !Object.prototype.hasOwnProperty.call(this.meta, property)) {
177-
return null;
178-
}
179-
180-
return this.meta[property];
181-
}
182-
183161
/**
184162
* @return {string}
185163
*/

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

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -198,29 +198,13 @@ describe('BinaryTreeNode', () => {
198198
});
199199

200200
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' });
201+
const redNode = new BinaryTreeNode(1);
202+
const blackNode = new BinaryTreeNode(2);
203203

204-
expect(redNode.meta.color).toBe('red');
205-
expect(blackNode.meta.color).toBe('black');
206-
});
207-
208-
it('should be possible to use get/set methods to change node meta information', () => {
209-
const redNode = new BinaryTreeNode(1, { color: 'red' });
210-
const blackNode = new BinaryTreeNode(2, { color: 'black' });
211-
212-
expect(redNode.getMeta('color')).toBe('red');
213-
expect(blackNode.getMeta('color')).toBe('black');
214-
215-
redNode.setMeta('size', 8);
216-
217-
expect(redNode.getMeta('size')).toBe(8);
218-
expect(redNode.getMeta('color')).toBe('red');
219-
expect(redNode.getMeta('not_existing')).toBeNull();
204+
redNode.meta.set('color', 'red');
205+
blackNode.meta.set('color', 'black');
220206

221-
// It must also be possible to override meta information.
222-
redNode.setMeta('color', 'blue');
223-
expect(redNode.getMeta('size')).toBe(8);
224-
expect(redNode.getMeta('color')).toBe('blue');
207+
expect(redNode.meta.get('color')).toBe('red');
208+
expect(blackNode.meta.get('color')).toBe('black');
225209
});
226210
});

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

Lines changed: 1 addition & 1 deletion
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, null, nodeValueCompareFunction);
8+
this.root = new BinarySearchTreeNode(null, nodeValueCompareFunction);
99
}
1010

1111
/**

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ import Comparator from '../../../utils/comparator/Comparator';
44
export default class BinarySearchTreeNode extends BinaryTreeNode {
55
/**
66
* @param {*} [value] - node value.
7-
* @param {Object} [meta] - any meta information that is attached to the node.
87
* @param {function} [compareFunction] - comparator function for node values.
98
*/
10-
constructor(value = null, meta = null, compareFunction = undefined) {
11-
super(value, meta);
9+
constructor(value = null, compareFunction = undefined) {
10+
super(value);
1211

1312
// This comparator is used to compare node values with each other.
1413
this.compareFunction = compareFunction;
@@ -17,31 +16,35 @@ export default class BinarySearchTreeNode extends BinaryTreeNode {
1716

1817
/**
1918
* @param {*} value
20-
* @param {Object} [meta]
2119
* @return {BinarySearchTreeNode}
2220
*/
23-
insert(value, meta = null) {
21+
insert(value) {
2422
if (this.nodeValueComparator.equal(this.value, null)) {
2523
this.value = value;
26-
this.meta = meta;
2724

2825
return this;
2926
}
3027

3128
if (this.nodeValueComparator.lessThan(value, this.value)) {
3229
// Insert to the left.
3330
if (this.left) {
34-
this.left.insert(value, meta);
35-
} else {
36-
this.setLeft(new BinarySearchTreeNode(value, meta, this.compareFunction));
31+
return this.left.insert(value);
3732
}
33+
34+
const newNode = new BinarySearchTreeNode(value, this.compareFunction);
35+
this.setLeft(newNode);
36+
37+
return newNode;
3838
} else if (this.nodeValueComparator.greaterThan(value, this.value)) {
3939
// Insert to the right.
4040
if (this.right) {
41-
this.right.insert(value, meta);
42-
} else {
43-
this.setRight(new BinarySearchTreeNode(value, meta, this.compareFunction));
41+
return this.right.insert(value);
4442
}
43+
44+
const newNode = new BinarySearchTreeNode(value, this.compareFunction);
45+
this.setRight(newNode);
46+
47+
return newNode;
4548
}
4649

4750
return this;
@@ -78,6 +81,7 @@ export default class BinarySearchTreeNode extends BinaryTreeNode {
7881

7982
/**
8083
* @param {*} value
84+
* @return {BinarySearchTreeNode}
8185
*/
8286
remove(value) {
8387
const nodeToRemove = this.find(value);
@@ -115,6 +119,8 @@ export default class BinarySearchTreeNode extends BinaryTreeNode {
115119
parent.replaceChild(nodeToRemove, nodeToRemove.right);
116120
}
117121
}
122+
123+
return nodeToRemove;
118124
}
119125

120126
/**

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@ describe('BinarySearchTreeNode', () => {
2020

2121
it('should insert nodes in correct order', () => {
2222
const bstNode = new BinarySearchTreeNode(2);
23-
bstNode.insert(1);
23+
const insertedNode1 = bstNode.insert(1);
2424

25+
expect(insertedNode1.value).toBe(1);
2526
expect(bstNode.toString()).toBe('1,2');
2627
expect(bstNode.contains(1)).toBeTruthy();
2728
expect(bstNode.contains(3)).toBeFalsy();
2829

29-
bstNode.insert(3);
30+
const insertedNode2 = bstNode.insert(3);
3031

32+
expect(insertedNode2.value).toBe(3);
3133
expect(bstNode.toString()).toBe('1,2,3');
3234
expect(bstNode.contains(3)).toBeTruthy();
3335
expect(bstNode.contains(4)).toBeFalsy();
@@ -80,20 +82,24 @@ describe('BinarySearchTreeNode', () => {
8082
});
8183

8284
it('should be possible to attach meta information to binary search tree nodes', () => {
83-
const node = new BinarySearchTreeNode(10, { value: 10 });
85+
const node = new BinarySearchTreeNode(10);
86+
87+
node.insert(20);
88+
const node1 = node.insert(30);
89+
node.insert(5);
90+
node.insert(40);
91+
const node2 = node.insert(1);
8492

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 });
93+
node.meta.set('color', 'red');
94+
node1.meta.set('color', 'black');
95+
node2.meta.set('color', 'white');
9096

91-
expect(node.meta.value).toBe(10);
97+
expect(node.meta.get('color')).toBe('red');
9298

9399
expect(node.findMin()).not.toBeNull();
94100
expect(node.findMin().value).toBe(1);
95-
expect(node.findMin().meta.value).toBe(1);
96-
expect(node.find(30).meta.value).toBe(30);
101+
expect(node.findMin().meta.get('color')).toBe('white');
102+
expect(node.find(30).meta.get('color')).toBe('black');
97103
});
98104

99105
it('should find node', () => {
@@ -205,7 +211,7 @@ describe('BinarySearchTreeNode', () => {
205211
const obj2 = { key: 'obj2', value: 2, toString: () => 'obj2' };
206212
const obj3 = { key: 'obj3', value: 3, toString: () => 'obj3' };
207213

208-
const bstNode = new BinarySearchTreeNode(obj2, null, nodeValueComparatorCallback);
214+
const bstNode = new BinarySearchTreeNode(obj2, nodeValueComparatorCallback);
209215
bstNode.insert(obj1);
210216

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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy