Binary_Tree
Binary_Tree
class treenode
public:
int data;
treenode *rchild,*lchild;
};
class stack
public:
int top;
treenode *tempstack[15];
public:
treenode* pop();
stack()
top=-1;
};
class tree
public:
void create();
void deltree(treenode * );
treenode *root;
tree()
root=NULL;
};
char ch;
treenode *freshnode,*root1;
while(1)
freshnode=new treenode;
freshnode->rchild=NULL;
freshnode->lchild=NULL;
cin>>freshnode->data;
if(root==NULL)
root=freshnode;
//root1=root;
else
insert(root,freshnode);
cin>>ch;
if(ch=='n')
break;
char ch;
cin>>ch;
if(ch=='r')
if(root->rchild==NULL)
{
root->rchild=freshnode;
else
insert(root->rchild,freshnode);
else
if(root->lchild==NULL)
root->lchild=freshnode;
else
insert(root->lchild,freshnode);
int ans;
if(root1==NULL)
cout<<"\n\n\t\tTree is empty";
else
do
cout<<"\n\n\t1.Rec-Preorder\n\t2.Rec-Postorder\n\t3.Rec-
Inorder\n";
cout<<"\t4.NonRec-Preorder\n\t5.NonRec-Postorder\n\
t6.NonRec-Inorder\n\t7.Exit\n";
cin>>ans;
switch(ans)
case 1:
cout<<"\n-------------------------------------\n";
preorder(root1);
cout<<"\n-------------------------------------\n";
break;
case 2:
cout<<"\n-------------------------------------\n";
postorder(root1);
cout<<"\n-------------------------------------\n";
break;
case 3:
cout<<"\n-------------------------------------\n";
inorder(root1);
cout<<"\n-------------------------------------\n";
break;
case 4:
p->lchild
cout<<"\n-------------------------------------\n";
preorder1(root1);
cout<<"\n-------------------------------------\n";
break;
case 5:
cout<<"\n-------------------------------------\n";
postorder1(root1);
cout<<"\n-------------------------------------\n";
break;
case 6:
cout<<"\n-------------------------------------\n";
inorder1(root1);
cout<<"\n-------------------------------------\n";
break;
case 7:
break;
}
} while(ans<7);
/* Recursive Functions*/
if(root1!=NULL)
cout<<"\t"<<root1->data;
preorder(root1->lchild);
preorder(root1->rchild);
if(root1!=NULL)
postorder(root1->lchild);
postorder(root1->rchild);
cout<<"\t"<<root1->data;
}
}
if(root1!=NULL)
inorder(root1->lchild);
cout<<"\t"<<root1->data;
inorder(root1->rchild);
/* Non-Recursive Functions*/
treenode *temp;
temp=root;
stack s;
s.tempstack[0]=temp;
s.top=0;
while(1)
{
cout<<"\t"<<temp->data;
if(temp->rchild!=NULL)
s.push(temp->rchild);
if(temp->lchild!=NULL)
s.push(temp->lchild);
if(s.top==0)
break;
else
temp=s.pop();
s.top--;
treenode *temp=root;
stack s,s1;
while(temp!=NULL)
s.push(temp);
s1.push(NULL);
temp=temp->lchild;
while(s.top!=-1)
temp=s.pop();
if(s1.pop()==NULL)
s.push(temp);
s1.push((treenode* )1);
temp=temp->rchild;
while(temp!=NULL)
s.push(temp);
s1.push(NULL);
temp=temp->lchild;
}
else
cout<<"\t"<<temp->data;
treenode *temp;
temp=root;
stack s;
s.top=-1;
cout<<"\n";
while(1)
if(temp!=NULL)
s.push(temp);
temp=temp->lchild;
}
else
if(s.top==-1)
break;
else
temp=s.pop();
cout<<"\t"<<temp->data;
s.top--;
temp=temp->rchild;
top=top+1;
tempstack[top]=visited;
treenode * stack::pop()
{
return(tempstack[top]);
//Main function//
int main()
tree t;
treenode *copyroot;
int ch;
do
cout<<"\n\n-----------------TREE------------------------ \n\n";
cin>>ch;
switch(ch)
{
case 1:
t.create();
break;
case 2:
t.display(t.root);
break;
case 3 :
copyroot=t.ccopy(t.root);
break;
case 4:
t.display(copyroot);
break;p->lchild
case 5:
t.deltree(t.root);
t.root=NULL;
break;
} while(ch<6);
return(0);
}
treenode *temp;
temp=NULL;
if(root1!=NULL)
temp=new(treenode);
temp->data=root1->data;
temp->lchild=ccopy(root1->lchild);
temp->rchild=ccopy(root1->rchild);
return(temp);
if (root1)
deltree(root1->lchild);
deltree(root1->rchild);
cout<<endl<<"Deleting"<<root1->data;
delete root1;