//Binary Search Tree Program
#include <iostream>
using namespace std;
//Node
struct Node
{
int data;
Node* left, *right;
}*root = NULL;
// Finding position for the current node and Inserting it
void Insert_In_Tree(Node* ptr, Node* newnode)
{
if (root == NULL)
{
root = newnode;
}
else
{
if (newnode->data <= ptr->data)
{
if (ptr->left == NULL)
{
ptr->left = newnode;
}
else
{
Insert_In_Tree(ptr->left, newnode);
}
}
else
{
if(ptr->right == NULL)
{
ptr->right = newnode;
}
else
{
Insert_In_Tree(ptr->right, newnode);
}
}
}
return;
}
int main()
{
int size;
cout<<"\n Enter the size of the array : ";
cin>>size;
int arr[size];
cout<<"\n Enter the elements in the array : ";
for (int i = 0; i < size; i++)
cin>>arr[i];
MakeNode(arr,size);
return 0;
}
For Program with Output Just insert the Below Functions Click here
#include <iostream>
using namespace std;
//Node
struct Node
{
int data;
Node* left, *right;
}*root = NULL;
// Finding position for the current node and Inserting it
void Insert_In_Tree(Node* ptr, Node* newnode)
{
if (root == NULL)
{
root = newnode;
}
else
{
if (newnode->data <= ptr->data)
{
if (ptr->left == NULL)
{
ptr->left = newnode;
}
else
{
Insert_In_Tree(ptr->left, newnode);
}
}
else
{
if(ptr->right == NULL)
{
ptr->right = newnode;
}
else
{
Insert_In_Tree(ptr->right, newnode);
}
}
}
return;
}
int main()
{
int size;
cout<<"\n Enter the size of the array : ";
cin>>size;
int arr[size];
cout<<"\n Enter the elements in the array : ";
for (int i = 0; i < size; i++)
cin>>arr[i];
MakeNode(arr,size);
return 0;
}
For Program with Output Just insert the Below Functions Click here
No comments:
Post a Comment