From fc5e0d09c3d0064aba99a6ca526bb9d2dc7bdf66 Mon Sep 17 00:00:00 2001
From: Anastasios Pantzartzis <apantzar@csd.auth.gr>
Date: Thu, 22 Aug 2024 00:32:24 +0300
Subject: [PATCH] Enhance BinaryTreeNode: Add type checking for child nodes

---
 src/data-structures/tree/BinaryTreeNode.js | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/src/data-structures/tree/BinaryTreeNode.js b/src/data-structures/tree/BinaryTreeNode.js
index 44c9390e41..0d41000c97 100644
--- a/src/data-structures/tree/BinaryTreeNode.js
+++ b/src/data-structures/tree/BinaryTreeNode.js
@@ -100,6 +100,12 @@ export default class BinaryTreeNode {
    * @return {BinaryTreeNode}
    */
   setLeft(node) {
+
+    // Check if it is a proper node.
+    if (node && !(node instanceof BinaryTreeNode)) {
+      throw new Error('The left node must be an instance of BinaryTreeNode');
+    }
+
     // Reset parent for left node since it is going to be detached.
     if (this.left) {
       this.left.parent = null;
@@ -121,6 +127,12 @@ export default class BinaryTreeNode {
    * @return {BinaryTreeNode}
    */
   setRight(node) {
+
+    // Check if it is a proper node.
+    if (node && !(node instanceof BinaryTreeNode)) {
+      throw new Error('The right node must be an instance of BinaryTreeNode');
+    }
+
     // Reset parent for right node since it is going to be detached.
     if (this.right) {
       this.right.parent = null;