#include <iostream>
using namespace std;
struct node
{
node * right, *left;
int info;
bool lthread,rthread;
};
class tree
{
private:
node *temp,*current;
public:
node *root,*Dummy;
void insert(node*,int);
void inorder(node*) ;
node* next_node(node* );
tree()
{ temp=root=current=NULL;
Dummy=new node;
Dummy->left=root;
Dummy->right=Dummy;
Dummy->rthread= Dummy->lthread=false;
}
} ;
void tree::insert (node *temp,int key)
{
if(root == NULL)
{root = new node;
root->info=key;
root->left=root->right= Dummy;
root->rthread=root->lthread=true;
return;
}
if(key<temp->info)
{if(!temp->lthread)
insert(temp->left,key);
else
{
current=new node;
current->info=key;
current->right=temp;
current->left=temp->left;
current->lthread=current->rthread=true;
temp->left=current;
temp->lthread=false;
//cout<<"left child";
return;}
}
if(key>temp->info)
{if(!temp->rthread)
{//cout<<"right;";
insert(temp->right,key);
}
else
{ //cout<<"right child";
current=new node;
current->info=key;
current->right=temp;
current->left=temp->right;
current->lthread=current->rthread=true;
temp->right=current;
temp->rthread=false;
return;
}
}
if(key==temp->info)
{
cout<<"dplicte.entr again"; cin>>key;
insert(temp,key);
}
}
node* tree :: next_node(node* temp)
{
if(temp->rthread)
{ //cout<<"enterd if";
return temp->right;
}
temp=temp->right;
while(!temp->lthread )
{//cout<<"enterd w2";
temp=temp->left; // the problem is in this statement or whenever i use temp->left
}
return temp;
}
void tree::inorder(node* temp)
{ int i=0;
while(next_node(temp)!=Dummy)
{
cout<<"node"<<i++<<endl;
cout<<temp->info<<endl;}
return;
}
//........................................
int main( ) {
tree obj;
int n;
for(int i=0;i<5;i++)
{ cin>>n;
obj.insert(obj.root,n);
}
cout<<"output\n";
obj.inorder(obj.Dummy);
return 0;
}
san467 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.