Skip to content

Commit fc5e0d0

Browse files
committed
Enhance BinaryTreeNode: Add type checking for child nodes
1 parent ca3d16d commit fc5e0d0

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/data-structures/tree/BinaryTreeNode.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ export default class BinaryTreeNode {
100100
* @return {BinaryTreeNode}
101101
*/
102102
setLeft(node) {
103+
104+
// Check if it is a proper node.
105+
if (node && !(node instanceof BinaryTreeNode)) {
106+
throw new Error('The left node must be an instance of BinaryTreeNode');
107+
}
108+
103109
// Reset parent for left node since it is going to be detached.
104110
if (this.left) {
105111
this.left.parent = null;
@@ -121,6 +127,12 @@ export default class BinaryTreeNode {
121127
* @return {BinaryTreeNode}
122128
*/
123129
setRight(node) {
130+
131+
// Check if it is a proper node.
132+
if (node && !(node instanceof BinaryTreeNode)) {
133+
throw new Error('The right node must be an instance of BinaryTreeNode');
134+
}
135+
124136
// Reset parent for right node since it is going to be detached.
125137
if (this.right) {
126138
this.right.parent = null;

0 commit comments

Comments
 (0)