Inserting a key into a tree in Javascript


The first insertion in a newly created binary tree creates a node at the root. Further insertions will be inserted according to the binary search tree property of left children being smaller than parent and right ones being greater than parents.

Let us look at how we can implement this algorithm in code −

Example

insertIter(data) {
   let node = new this.Node(data);

   // Check if the tree is empty
   if (this.root === null) {
      // Insert as the first element
      this.root = node; return;
   }

   let currNode = this.root;
   while (true) {
      if (data < currNode.data) {
         // Set the value here as we've reached a leaf node
         if (currNode.left === null) {
            currNode.left = node;
            break;
         } else {
            currNode = currNode.left;
         }
      } else {
         // Set the value here as we've reached a leaf node
         if (currNode.right === null) {
            currNode.right = node;
            break;
         } else {
            currNode = currNode.right;
         }
      }
   }
}

Let us understand how this function works. First, we check if the root is a null meaning tree is empty if yes then we assign new node as root and we're done. If not, we create a currNode variable and point it to root. Then we check if our data is less than currNode or not, if it is, we check if its left child is null. If it is then we keep our data here and exit. Else we go on iterating until we reach a leaf and finally place our data there.

You can test this function using −

Example

let BST = new BinarySearchTree();
BST.insertIter(10);
BST.insertIter(15);
BST.insertIter(5);
BST.insertIter(50);
BST.insertIter(3);
BST.insertIter(7);
BST.insertIter(12);

We can also make this function recursive. Trees are inherently recursive structures and we can make use of this recursive property fairly easily. Let's look at a recursive version of insert −

Example

insertRec(data) {
   let node = new this.Node(data);

   // Check if the tree is empty
   if (this.root === null) {
      // Insert as the first element
      this.root = node;
   } else {
      insertRecHelper(this.root, node);
   }
}

We need to create a helper function that can recurse but we don't want to expose that as a property of the class, so this function will be out of the class definition.

Example

function insertRecHelper(root, node) {
   if (node.data < root.data) {
      // Set the value here as we've reached a leaf node

      if (root.left === null) {
         root.left = node;
      } else {
         // Recursively call this function with the left subtree
            insertRecHelper(root.left, node);
      }
   } else {
      // Set the value here as we've reached a leaf node
      if (root.right === null) {
         root.right = node;
      } else {
         // Recursively call this function with the right subtree
         insertRecHelper(root.right, node);
      }
   }
}

You can test this using −

Example

let BST = new BinarySearchTree();
BST.insertRec(10);
BST.insertRec(15);
BST.insertRec(5);
BST.insertRec(50);
BST.insertRec(3);
BST.insertRec(7);
BST.insertRec(12);

Sai Subramanyam
Sai Subramanyam

Passionate, Curious and Enthusiastic.

Updated on: 15-Jun-2020

495 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements