Insert a Node in Binary tree using JavaScript
Last Updated :
24 Apr, 2024
A binary tree is a tree data structure where each node has at most two children, referred to as the left child and the right child. The topmost node is called the root. Nodes can have values or data, and the left child's value is less than the parent's value, while the right child's value is greater.
There are various approaches to inserting a node in a binary tree using JavaScript which are as follows:
Using Recursive Approach
In the recursive approach, we define a recursive function to insert a node into the binary tree. If the current node is null, we create a new node and return it. Otherwise, we recursively traverse the tree to the left or right based on the value of the new node.
Example: This example uses a recursive approach to Insert a Node in a Binary tree.
JavaScript
class Node {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
class BinaryTree {
constructor() {
this.root = null;
}
insertRecursively(value, node = this.root)
{
if (!node) {
return new Node(value);
}
if (value < node.value)
{
node.left = this
.insertRecursively(value, node.left);
} else if (value > node.value)
{
node.right = this
.insertRecursively(value, node.right);
}
return node;
}
}
// Create a binary tree
const binaryTree = new BinaryTree();
binaryTree.root = binaryTree.
insertRecursively(5);
binaryTree.insertRecursively(3);
binaryTree.insertRecursively(8);
binaryTree.insertRecursively(1);
binaryTree.insertRecursively(4);
binaryTree.insertRecursively(6);
console.log(binaryTree.root.right.left.value);
Time complexity: O(h), where h is the height of the binary tree.
Space complexity: O(n)
Using Iterative Approach
Iterative approach uses a loop to traverse the binary tree iteratively. It starts at the root node and compares the value of the new node with the current node. If the new node's value is less than the current node's value, it moves to the left child; otherwise, it moves to the right child. This process continues until it reaches a leaf node, where it inserts the new node.
Example: This example uses Iterative approach to Insert a Node in Binary tree.
JavaScript
class Node {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
class BinaryTree {
constructor() {
this.root = null;
}
insertIteratively(value) {
const newNode = new Node(value);
if (!this.root) {
this.root = newNode;
return;
}
let current = this.root;
while (true) {
if (value < current.value)
{
if (!current.left)
{
current.left = newNode;
return;
}
current = current.left;
} else {
if (!current.right)
{
current
.right = newNode;
return;
}
current = current.right;
}
}
}
}
// Create a binary tree
const binaryTree = new BinaryTree();
binaryTree.insertIteratively(5);
binaryTree.insertIteratively(3);
binaryTree.insertIteratively(8);
binaryTree.insertIteratively(1);
binaryTree.insertIteratively(4);
binaryTree.insertIteratively(6);
console.log(binaryTree.root.right.left.value);
Time complexity: O(h), where h is the height of the binary tree.
Space complexity: O(1)
Similar Reads
Height of Binary Tree using JavaScript The height of a binary tree can be defined as the maximum depth or length of the longest path from the root node to any leaf node. Below is an example to illustrate the height of the binary tree. Height of Binary tree is 3There are several approaches to find height of Binary tree using JavaScript wh
3 min read
LCA in Binary Tree using JavaScript The lowest common ancestor between two nodes n1 and n2 in a binary tree is defined as the lowest node in a Tree that has both n1 and n2 as descendants. If either n1 or n2 is not present in the tree, or if they are not connected through a common ancestor, the LCA of the binary tree is NULL. Below is
4 min read
Kth Ancestor in Binary Tree using JavaScript Given a binary tree and a node in this tree, our task is to find the k-th ancestor of the specified node in JavaScript. If no such ancestor exists, or when k exceeds the depth of the node, the function should return null. Example: Input: Node = 5, k = 2Output: 1ApproachFirst, calculate the depth of
2 min read
Find Distance Between Two Nodes of a Binary Tree using JavaScript In computer science, binary trees are hierarchical data structures that consist of nodes. Each node can have two children at most - one on the leÂft side and one on the right. These structures have a top-to-bottom order. Solving for distance between any two giveÂn nodes is a problem often seÂen w
7 min read
Search a Given Key in BST using JavaScript Given a binary search tree and a key, determine if the key is present in the tree. If the key is present, return the node containing the key otherwise, return null. Example: Input : {15,20,10,8,12,16,25}, Key : 20Output : 20 found 15 / \ 10 20 / \ / \ 8 12 16 25Table of Content Recursive ApproachIte
3 min read
Sum of Left leaves in Binary Tree using JavaScript Calculating the sum of all left leaves in a binary tree is a common task that helps in understanding tree traversal techniques. A left leaf is defined as a leaf node that is the left child of its parent node. This problem can serve various purposes, such as evaluating symmetry in data structures or
5 min read