Skip to content

Commit 3ae9c40

Browse files
committed
Test that it is possible to use objects and binary tree node values.
1 parent 625217a commit 3ae9c40

File tree

2 files changed

+71
-4
lines changed

2 files changed

+71
-4
lines changed

src/data-structures/tree/BinaryTreeNode.js

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
1+
import Comparator from '../../utils/comparator/Comparator';
2+
13
export default class BinaryTreeNode {
4+
/**
5+
* @param {*} value
6+
* @param {BinaryTreeNode} parent
7+
*/
28
constructor(value = null, parent = null) {
39
this.left = null;
410
this.right = null;
511
this.parent = parent;
612
this.value = value;
13+
14+
// This comparator is used to compare binary tree nodes with each other.
15+
this.nodeComparator = new Comparator();
716
}
817

18+
/**
19+
* @return {number}
20+
*/
921
get leftHeight() {
1022
if (!this.left) {
1123
return 0;
@@ -14,6 +26,9 @@ export default class BinaryTreeNode {
1426
return this.left.height + 1;
1527
}
1628

29+
/**
30+
* @return {number}
31+
*/
1732
get rightHeight() {
1833
if (!this.right) {
1934
return 0;
@@ -22,14 +37,24 @@ export default class BinaryTreeNode {
2237
return this.right.height + 1;
2338
}
2439

40+
/**
41+
* @return {number}
42+
*/
2543
get height() {
2644
return Math.max(this.leftHeight, this.rightHeight);
2745
}
2846

47+
/**
48+
* @return {number}
49+
*/
2950
get balanceFactor() {
3051
return this.leftHeight - this.rightHeight;
3152
}
3253

54+
/**
55+
* @param {BinaryTreeNode} node
56+
* @return {BinaryTreeNode}
57+
*/
3358
setLeft(node) {
3459
// Reset parent for left node since it is going to be detached.
3560
if (this.left) {
@@ -47,6 +72,10 @@ export default class BinaryTreeNode {
4772
return this;
4873
}
4974

75+
/**
76+
* @param {BinaryTreeNode} node
77+
* @return {BinaryTreeNode}
78+
*/
5079
setRight(node) {
5180
// Reset parent for right node since it is going to be detached.
5281
if (this.right) {
@@ -64,38 +93,50 @@ export default class BinaryTreeNode {
6493
return this;
6594
}
6695

96+
/**
97+
* @param {BinaryTreeNode} nodeToRemove
98+
* @return {boolean}
99+
*/
67100
removeChild(nodeToRemove) {
68-
if (this.left && this.left === nodeToRemove) {
101+
if (this.left && this.nodeComparator.equal(this.left, nodeToRemove)) {
69102
this.left = null;
70103
return true;
71104
}
72105

73-
if (this.right && this.right === nodeToRemove) {
106+
if (this.right && this.nodeComparator.equal(this.right, nodeToRemove)) {
74107
this.right = null;
75108
return true;
76109
}
77110

78111
return false;
79112
}
80113

114+
/**
115+
* @param {BinaryTreeNode} nodeToReplace
116+
* @param {BinaryTreeNode} replacementNode
117+
* @return {boolean}
118+
*/
81119
replaceChild(nodeToReplace, replacementNode) {
82120
if (!nodeToReplace || !replacementNode) {
83121
return false;
84122
}
85123

86-
if (this.left && this.left === nodeToReplace) {
124+
if (this.left && this.nodeComparator.equal(this.left, nodeToReplace)) {
87125
this.left = replacementNode;
88126
return true;
89127
}
90128

91-
if (this.right && this.right === nodeToReplace) {
129+
if (this.right && this.nodeComparator.equal(this.right, nodeToReplace)) {
92130
this.right = replacementNode;
93131
return true;
94132
}
95133

96134
return false;
97135
}
98136

137+
/**
138+
* @return {*[]}
139+
*/
99140
traverseInOrder() {
100141
let traverse = [];
101142

@@ -115,6 +156,9 @@ export default class BinaryTreeNode {
115156
return traverse;
116157
}
117158

159+
/**
160+
* @return {string}
161+
*/
118162
toString() {
119163
return this.traverseInOrder().toString();
120164
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,27 @@ describe('BinaryTreeNode', () => {
173173
expect(root.left).toBeNull();
174174
expect(root.right).toBeNull();
175175
});
176+
177+
it('should be possible to create node with object as a value', () => {
178+
const obj1 = { key: 'object_1', toString: () => 'object_1' };
179+
const obj2 = { key: 'object_2' };
180+
181+
const node1 = new BinaryTreeNode(obj1);
182+
const node2 = new BinaryTreeNode(obj2);
183+
184+
node1.setLeft(node2);
185+
186+
expect(node1.value).toEqual(obj1);
187+
expect(node2.value).toEqual(obj2);
188+
expect(node1.left.value).toEqual(obj2);
189+
190+
node1.removeChild(node2);
191+
192+
expect(node1.value).toEqual(obj1);
193+
expect(node2.value).toEqual(obj2);
194+
expect(node1.left).toBeNull();
195+
196+
expect(node1.toString()).toBe('object_1');
197+
expect(node2.toString()).toBe('[object Object]');
198+
});
176199
});

0 commit comments

Comments
 (0)