## What is Tree?

In computer science, a tree is a widely used abstract data type that simulates a hierarchical tree structure, with a root value and subtrees of children with a parent node, represented as a set of linked nodes.

A *tree* is a nonlinear *data structure*, compared to arrays, linked lists, stacks and queues which are linear *data structures*. A *tree* can be empty with no nodes or a *tree* is a *structure* consisting of one node called the root and zero or one or more subtrees.

## What is Binary Tree

In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left child and the right child.

In a tree an arbitrary node can have any number of child. But in binary tree each node can have at most two child.

## What is Binary Search Tree

In computer science, a binary search tree, also called an ordered or sorted binary tree, is a rooted binary tree whose internal nodes each store a key greater than all the keys in the node’s left subtree and less than those in its right subtree.

## Insert into Binary Search Tree

To insert an element into binary search tree we need to follow 2 steps:

- Insert the element at last position of the binary search tree and increase the size of BST n to n+1.
- Recursively test and swap the new value as long as the tree don’t satisfy BST property.

## C++ Code to Insert into Binary Search Tree

```
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,i,j; //n is Number of Elements in the Tree and i,j are Normal Integer
cout<<"How Much Elements in your Binary Search Tree: ";
cin>>n;
long Tree[n+5]; //Tree is an Array for Elements of Tree
cout<<"Enter Your Binary Search Tree in Inorder:"<<endl;
for(i=0;i<n;i++){
cin>>Tree[i];
}
cout<<"What Will You Insert: ";
cin>>Tree[n]; //The Element What Will Be Inserted
cout<<endl;
n=n+1; //Increasing Total Number of Elements
for(i=0;i<n;i++){ //For Loop to Search and Short the Tree
if((Tree[i]==Tree[n-1])&&(i!=n-1)){ //Check Whether the Element Already Exist
cout<<"This Element Already Exist at Position: "<<i+1<<endl;
n=n-1; //If Exist Decrease Total Number of Element as no Need to Insert
}
for(j=i;j<n;j++){
if(Tree[i]>Tree[j])swap(Tree[i],Tree[j]); //If not Exist Then Short to Bring the Element at Correct Position
}
}
cout<<"Inorder Traversing After Insertion:"<<endl;
for(i=0;i<n;i++){
cout<<Tree[i]<<" ";
}
cout<<endl;
return 0;
}
```

Hope your experience with this tutorial is quite good. Comment how do you feel about this tutorial. If you have any confusion you can also comment here. Subscribe for latest posts.