TBT
TBT
struct node
{
struct node *left;
int lthread;
int info;
int rthread;
struct node *right;
};
int main( )
{
int choice,num;
struct node *root=NULL;
while(1)
{
cout<<endl;
cout<<"1.Insert";
cout<<"2.Recursive Inorder Traversal";
cout<<"3.Recursive Preorder Traversal";
cout<<"4.Recursive Post Traversal";
cout<<"5.Recursive Inorder Traversal";
cout<<"6.Recursive Preorder Traversal";
cout<<"7.Recursive Post Traversal";
cout<<"8.Quit";
cout<<"Enter your choice";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter the number to be inserted";
cin>>num;
root = insert(root,num);
break;
case 2:
inorder(root);
break;
case 3:
preorder(root);
break;
case 4:
postorder(root);
break;
case 5:
exit(1);
default:
cout<<"Wrong choice";
}/*End of switch */
}/*End of while */
return 0;
}
struct node *insert(struct node *root, int ikey)
{
struct node *tmp,*par,*ptr;
int found=0;
ptr = root;
par = NULL;
while( ptr!=NULL )
{
if( ikey == ptr->info)
{
found =1;
break;
}
par = ptr;
if(ikey < ptr->info)
{
if(ptr->lthread == 0)
ptr = ptr->left;
else
break;
}
else
{
if(ptr->rthread == 0)
ptr = ptr->right;
else
break;
}
}
if(found)
cout<<"nDuplicate key";
else
{
ptr=root;
if(ptr->lthread==0)
inorder(ptr->left);
cout<<ptr->info<<" ";
if(ptr->rthread==0)
inorder(ptr->right);
}/*End of inorder( )*/
if(ptr->lthread==0)
preorder(ptr->left);
if(ptr->rthread==0)
preorder(ptr->right);
if(ptr->lthread==0)
preorder(ptr->left);
if(ptr->rthread==0)
preorder(ptr->right);
cout<<ptr->info<<" ";
}