#include<iostream>
using namespace std;
struct node{
int info;
node *right,*left;
}*root=NULL;
void Add(int n)
{
node *ptr=new node;
ptr->info=n;
ptr->left=NULL;
ptr->right=NULL;
if(root==NULL)
{
root=ptr;
}
else
{
node *ptr1=root;
node *ptr2=NULL;
while(ptr1!=NULL)
{
ptr2=ptr1;
if(ptr1->info<n)
{
ptr1=ptr1->right;
}
else
{
ptr1=ptr1->left;
}
}
if(ptr2->info>n)
{
ptr2->left=ptr;
}
else
{
ptr2->right=ptr;
}
}
}
void Inorder(node *root)
{
if(root!=NULL)
{
Inorder(root->left);
cout<<root->info<<"n";
Inorder(root->right);
}
}
int main()
{
int item,choice;
while(1)
{
cout<<"n1. Add a Valuen2. Inorder Traversaln3. Exitn";
cout<<"nEnter Your Choice";
cin>>choice;
switch(choice)
{
case 1:
cout<<"nENter the value to be addedd";
cin>>item;
Add(item);
break;
case 2:
Inorder(root);
case 3:
exit(0);
}
}
return 0;
}
0 Comments