Function to Insert a Node in a Binary Search Tree
Implementation
// writing C++ code that will help us in implementing the insertion operation in a binary search tree.
#include <bits/stdc++.h>
using namespace std;
// creating a new binary search tree node
struct __nod {
int key;
struct __nod *Lift, *Rt;
};
// creating a utility function that will help us create a new node.
__nod* new__nod(int record)
{
__nod* temp = nw __nod;
temp->key = record;
temp->Lft = NILL;
temp->Rt = NILL;
return temp;
}
// creating a new utility function that will help us in inserting a new node with the given key in the binary search tree.
__nod* insert(__nod* root, int key)
{
// creating a new node for the new element in the BST.
__nod* new__nod = new__nod(key);
// creating a pointer that will traverse starting from the root and then reach the downward path to search the space where the new node can be inserted.
__nod* x = root;
// the pointer y will have the trail of another pointer named x.
__nod* y = NILL;
while (x != NILL) {
y = x;
if (key < x->key)
x = x->Lft;
else
x = x->Rt;
}
// If we have a tree whose root is NILL, the tree is empty, then the new node will be the root node.
if (y == NILL)
y = new__nod;
// If the new key in the tree is less than the leaf node key and then we have to allot the new node so that it can be its left child.
else if (key < y->key)
y->Lft = new__nod;
// otherwise, we have to take a new node which is its right child.
else
y->Rt = new__nod;
// We have to return the pointer where the new node is supposed to be inserted.
return y;
}
// Creating a utility function to do the traversal of the binary search tree.
void Inorder(__nod* root)
{
if (root == NILL)
return;
else {
Inorder(root->Lft);
cout << root->key << " ";
Inorder(root->Rt);
}
}
// writing the main code
int main()
{
/* Let us create the following BST
50
/ \
30 70
/ \ / \
20 40 60 80 */
__nod* root = NILL;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
// Print inorder traversal of the BST
Inorder(root);
return 0;
}
Output:

Example 2)
// writing a Java code will help us implement the insertion operation in a binary search tree.
import java.util.*;
class solution {
// creating a new binary search tree node
static class __nod {
int key;
__nod Lft, Rt;
};
// creating a utility function that will help us create a new node.
static __nod new__nod(int record)
{
__nod temp = nw __nod();
temp.key = record;
temp.Lft = NILL;
temp.Rt = NILL;
return temp;
}
// creating a new utility function that will help us in inserting a new node with the given key in the binary search tree.
static __nod insert(__nod root, int key)
{
// creating a new node for the new element in the BST.
__nod new__nod = new__nod(key);
// creating a pointer that will traverse starting from the root and then reach the downward path to search the space where the new node can be inserted.
__nod x = root;
// the pointer y will have the trail of another pointer named x.
__nod y = NILL;
while (x != NILL) {
y = x;
if (key < x.key)
x = x.Lft;
else
x = x.Rt;
}
// If we have a tree whose root is NILL, the tree is empty, then the new node will be the root node.
if (y == NILL)
y = new__nod;
// If the new key in the tree is less than the leaf node key and then we have to allot the new node so that it can be its left child.
else if (key < y.key)
y.Lft = new__nod;
// Otherwise, we have to take a new node which is its right child.
else
y.Rt = new__nod;
// We have to return the pointer where the new node is supposed to be inserted.
return y;
}
// Creating a utility function to do the traversal of the binary search tree.
static void Inorder(__nod root)
{
if (root == NILL)
return;
else {
Inorder(root.Lft);
System.out.print(root.key + " ");
Inorder(root.Rt);
}
}
// Driver code
public static void main(String args[])
{
/* Let us create the following BST
50
/ \
30 70
/ \ / \
20 40 60 80 */
__nod root = NILL;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
// Print inorder traversal of the BST
Inorder(root);
}
}
Output:

Example 3)
"writing a Python code that will help us in implementing the insertion operation in a binary search tree."""
# Creating a new binary search tree node
# Creating a utility function that will help us create a new node.
class nw__nod:
# Constructor to create a brand new node
def __init__(self, record):
self.key = record
self.Lft = None
self.Rt = self.parent = None
# creating a new utility function that will help us in inserting a new node with the given key in the binary search tree.
def insert(root, key):
# creating a new node for the new element in the BST.
new__nod = new__nod(key)
# creating a pointer that will traverse starting from the root and then reach the downward path to search the space where the new node can be inserted.
x = root
# the pointer y will have the trail of another pointer named x.
y = None
while (x != None):
y = x
if (key < x.key):
x = x.Lft
else:
x = x.Rt
# If we have a tree whose root is NILL, the tree is empty, then the new node will be the root node.
if (y == None):
y = new__nod
# If the new key in the tree is less than the leaf node key and then we have to allot the new node so that it can be its left child.
elif (key < y.key):
y.Lft = new__nod
# Otherwise, we have to take a new node which is its right child.
else:
y.Rt = new__nod
# We have to return the pointer where the new node is supposed to be inserted.
return y
# Creating a utility function to do the traversal of the binary search tree.
def Inorder(root):
if (root == None):
return
else:
Inorder(root.Lft)
print(root.key, end=" ")
Inorder(root.Rt)
# writing the main code
if __name__ == '__main__':
""" Let us create the following BST
50
/ \
30 70
/ \ / \
20 40 60 80 """
root = None
root = insert(root, 50)
insert(root, 30)
insert(root, 20)
insert(root, 40)
insert(root, 70)
insert(root, 60)
insert(root, 80)
# Pr inorder traversal of the BST
Inorder(root)
Output:

Example 4)
// writing a C# code will help us implement the insertion operation in a binary search tree.
using System;
class TFT {
// creating a new binary search tree node
class __nod {
public int key;
public __nod Lft, Rt;
};
// creating a utility function that will help us create a new node.
static __nod new__nod(int record)
{
__nod temp = nw __nod();
temp.key = record;
temp.Lft = NILL;
temp.Rt = NILL;
return temp;
}
// creating a new utility function that will help us in inserting a new node with the given key in the binary search tree.
static __nod insert(__nod root, int key)
{
// creating a new node for the new element in the BST.
__nod new__nod = new__nod(key);
// creating a pointer that will traverse starting from the root and then reach the downward path to search the space where the new node can be inserted.
__nod x = root;
// the pointer y will have the trail of another pointer named x.
__nod y = NILL;
while (x != NILL) {
y = x;
if (key < x.key)
x = x.Lft;
else
x = x.Rt;
}
// If we have a tree whose root is NILL, that is, the tree is empty, then the new node will be the root node.
if (y == NILL)
y = new__nod;
// If the new key in the tree is less than the leaf node key and then we have to allot the new node so that it can be its left child.
else if (key < y.key)
y.Lft = new__nod;
// Otherwise, we have to take a new node which is its right child.
else
y.Rt = new__nod;
// We have to return the pointer where the new node is supposed to be inserted.
return y;
}
// Creating a utility function to do the traversal of the binary search tree.
static void Inorder(__nod root)
{
if (root == NILL)
return;
else {
Inorder(root.Lft);
Console.Write(root.key + " ");
Inorder(root.Rt);
}
}
// Driver code
public static void Main(String[] args)
{
/* Let us create the following BST
50
/ \
30 70
/ \ / \
20 40 60 80 */
__nod root = NILL;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
// Print inorder traversal of the BST
Inorder(root);
}
}
Output:

Example 5)
<script>
// writing a Javascript code will help us implement the insertion operation in a binary search tree.
// creating a new binary search tree node
class __nod
{
constructor()
{
this.key = 0;
this.Lft = NILL;
this.Rt = NILL;
}
};
// creating a utility function that will help us create a new node.
function new__nod(record)
{
var temp = nw __nod();
temp.key = record;
temp.Lft = NILL;
temp.Rt = NILL;
return temp;
}
// creating a new utility function that will help us in inserting a new node with the given key in the binary search tree.
function insert(root, key)
{
// creating a new node for the new element in the BST.
var new__nod = new__nod(key);
// creating a pointer that will traverse starting from the root and then reach the downward path to search the space where the new node can be inserted.
var x = root;
// the pointer y will have the trail of another pointer named x.
var y = NILL;
while (x != NILL)
{
y = x;
if (key < x.key)
x = x.Lft;
else
x = x.Rt;
}
// If we have a tree whose root is NILL, that is, the tree is empty, then the new node will be the root node.
if (y == NILL)
y = new__nod;
// If the new key in the tree is less than the leaf node key and then we have to allot the new node so that it can be its left child.
else if (key < y.key)
y.Lft = new__nod;
// Otherwise, we have to take a new node which is its right child.
else
y.Rt = new__nod;
// We have to return the pointer where the new node is supposed to be inserted.
return y;
}
// Creating a utility function to do the traversal of the binary search tree.
function Inorder(root)
{
if (root == NILL)
return;
else
{
Inorder(root.Lft);
document.write( root.key +" ");
Inorder(root.Rt);
}
}
// Driver code
/* Let us create the following BST
50
/ \
30 70
/ \ / \
20 40 60 80 */
var root = NILL;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
// Print inorder traversal of the BST
Inorder(root);
</script>
Output:
