0% found this document useful (0 votes)
9 views8 pages

TBT

The document contains C++ code for implementing threaded binary tree data structure. It includes functions for insertion, inorder, preorder and postorder tree traversals. The main function takes user input for performing these operations on the tree.

Uploaded by

hinej92245
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views8 pages

TBT

The document contains C++ code for implementing threaded binary tree data structure. It includes functions for insertion, inorder, preorder and postorder tree traversals. The main function takes user input for performing these operations on the tree.

Uploaded by

hinej92245
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

#include<iostream>

using namespace std;

struct node *in_succ(struct node *p);


struct node *in_pred(struct node *p);
struct node *insert(struct node *root, int ikey);
void inorder( struct node *root);
void preorder( struct node *root);
void postorder( struct node *root);

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
{

tmp=(struct node *)malloc(sizeof(struct node));


tmp->info=ikey;
tmp->lthread = 1;
tmp->rthread = 1;
if(par==NULL)
{
root=tmp;
tmp->left=NULL;
tmp->right=NULL;
}
else if( ikey < par->info )
{
tmp->left=par->left;
tmp->right=par;
par->lthread=0;
par->left=tmp;
}
else
{
tmp->left=par;
tmp->right=par->right;
par->rthread=0;
par->right=tmp;
}
}
return root;
}/*End of insert( )*/

void inorder( struct node *root)


{
struct node *ptr;
if(root == NULL )
{
cout<<"Tree is empty";
return;
}

ptr=root;
if(ptr->lthread==0)
inorder(ptr->left);
cout<<ptr->info<<" ";
if(ptr->rthread==0)
inorder(ptr->right);
}/*End of inorder( )*/

void preorder(struct node *root )


{
struct node *ptr;
if(root==NULL)
{
cout<<"Tree is empty";
return;
}
ptr=root;
cout<<ptr->info<<" ";

if(ptr->lthread==0)
preorder(ptr->left);

if(ptr->rthread==0)
preorder(ptr->right);

void postorder(struct node *root )


{
struct node *ptr;
if(root==NULL)
{
cout<<"Tree is empty";
return;
}
ptr=root;

if(ptr->lthread==0)
preorder(ptr->left);

if(ptr->rthread==0)
preorder(ptr->right);

cout<<ptr->info<<" ";
}

You might also like