So you want codes, right?
Here you can get each and every code as per your requirement and if you don't, feel FREE to ask for one.We would make it available for you as soon as possible.
Everyone in this world has to go through a student's life and therefore the codes you get here are easy for a better understanding of STUDENTS.But we guarantee we will not make PROFESSIONALS feel lonely here.
Enough of chit-chat.Now some codes-
Recursive and non-recursive tree traversals (C++ code):
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class node
{
public:
int data;
node *left,*right;
node(int x)
{
data=x;
left=right=NULL;
}
};
class tree
{
node *root;
void inorder1(node *t);
void preorder1(node *t);
void postorder1(node *t);
node *create1();
public:
tree()
{
root=NULL;
}
void create()
{
root=create1();
}
void inorder()
{
inorder1(root);
}
void preorder()
{
preorder1(root);
}
void postorder()
{
postorder1(root);
}
void non_rec_inorder();
void non_rec_preorder();
void non_rec_postorder();
};
class stack
{
node *data[30];
int top;
public:
stack()
{
top=-1;
}
int empty()
{
if(top==-1)
return -1;
return 0;
}
void push(node *p)
{
data[++top]=p;
}
node *pop()
{
return(data[top--]);
}
};
void tree::inorder1(node *t)
{
if(t!=NULL)
{
inorder1(t->left);
cout<<" "<<t->data;
inorder1(t->right);
}
}
void tree::preorder1(node *t)
{
if(t!=NULL)
{
cout<<" "<<t->data;
preorder1(t->left);
preorder1(t->right);
}
}
void tree::postorder1(node *t)
{
if(t!=NULL)
{
postorder1(t->left);
postorder1(t->right);
cout<<" "<<t->data;
}
}
node *tree::create1()
{
node *p;
int x;
cout<<"\nEnter data(-1 for no data or NULL node)";
cin>>x;
if(x==-1)
return NULL;
p=new node(x);
cout<<"\nEnter left child of"<<x;
p->left=create1();
cout<<"\nEnter right child of"<<x;
p->right=create1();
return p;
}
void tree::non_rec_inorder()
{
stack s;
node *t=root;
cout<<"\n";
while(t!=NULL)
{
s.push(t);
t=t->left;
}
while(!s.empty())
{
t=s.pop();
cout<<" "<<t->data;
t=t->right;
while(t!=NULL)
{
s.push(t);
t=t->left;
}
}
}
void tree::non_rec_preorder()
{
stack s;
node *t=root;
cout<<"\n";
while(t!=NULL)
{
cout<<" "<<t->data;
s.push(t);
t=t->left;
}
while(!s.empty())
{
t=s.pop();
t=t->right;
while(t!=NULL)
{
cout<<" "<<t->data;
s.push(t);
t=t->left;
}
}
}
void tree::non_rec_postorder()
{
stack s,s1;
node *t=root;
cout<<"\n";
while(t!=NULL)
{
s.push(t);
s1.push(NULL);
t=t->left;
}
while(!s.empty())
{
t=s.pop();
if(s1.pop()==NULL)
{
s.push(t);
s1.push((node *)1);
t=t->right;
while(t!=NULL)
{
s.push(t);
s1.push(NULL);
t=t->left;
}
}
else
cout<<" "<<t->data;
}
}
void main()
{
tree t1;
node *p;
int x,op;
clrscr();
do
{
cout<<"\n*****MENU*****";
cout<<"\n1.Create \n2.Recursive Preorder";
cout<<"\n3.Recursive Inorder\n4.Recursive Postorder";
cout<<"\n5.Non-recursive Inorder \n6.Non-recursive Preorder";
cout<<"\n7.Non-recursive Postorder \n8.Quit";
cout<<"\nEnter your choice:";
cin>>op;
switch(op)
{
case 1:
t1.create();
break;
case 2:
t1.preorder();
break;
case 3:
t1.inorder();
break;
case 4:
t1.postorder();
break;
case 5:
t1.non_rec_inorder();
break;
case 6:
t1.non_rec_preorder();
break;
case 7:
t1.non_rec_postorder();
break;
}
}while(op!=8);
}
Recursive and non-recursive tree traversals (C++ code):
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class node
{
public:
int data;
node *left,*right;
node(int x)
{
data=x;
left=right=NULL;
}
};
class tree
{
node *root;
void inorder1(node *t);
void preorder1(node *t);
void postorder1(node *t);
node *create1();
public:
tree()
{
root=NULL;
}
void create()
{
root=create1();
}
void inorder()
{
inorder1(root);
}
void preorder()
{
preorder1(root);
}
void postorder()
{
postorder1(root);
}
void non_rec_inorder();
void non_rec_preorder();
void non_rec_postorder();
};
class stack
{
node *data[30];
int top;
public:
stack()
{
top=-1;
}
int empty()
{
if(top==-1)
return -1;
return 0;
}
void push(node *p)
{
data[++top]=p;
}
node *pop()
{
return(data[top--]);
}
};
void tree::inorder1(node *t)
{
if(t!=NULL)
{
inorder1(t->left);
cout<<" "<<t->data;
inorder1(t->right);
}
}
void tree::preorder1(node *t)
{
if(t!=NULL)
{
cout<<" "<<t->data;
preorder1(t->left);
preorder1(t->right);
}
}
void tree::postorder1(node *t)
{
if(t!=NULL)
{
postorder1(t->left);
postorder1(t->right);
cout<<" "<<t->data;
}
}
node *tree::create1()
{
node *p;
int x;
cout<<"\nEnter data(-1 for no data or NULL node)";
cin>>x;
if(x==-1)
return NULL;
p=new node(x);
cout<<"\nEnter left child of"<<x;
p->left=create1();
cout<<"\nEnter right child of"<<x;
p->right=create1();
return p;
}
void tree::non_rec_inorder()
{
stack s;
node *t=root;
cout<<"\n";
while(t!=NULL)
{
s.push(t);
t=t->left;
}
while(!s.empty())
{
t=s.pop();
cout<<" "<<t->data;
t=t->right;
while(t!=NULL)
{
s.push(t);
t=t->left;
}
}
}
void tree::non_rec_preorder()
{
stack s;
node *t=root;
cout<<"\n";
while(t!=NULL)
{
cout<<" "<<t->data;
s.push(t);
t=t->left;
}
while(!s.empty())
{
t=s.pop();
t=t->right;
while(t!=NULL)
{
cout<<" "<<t->data;
s.push(t);
t=t->left;
}
}
}
void tree::non_rec_postorder()
{
stack s,s1;
node *t=root;
cout<<"\n";
while(t!=NULL)
{
s.push(t);
s1.push(NULL);
t=t->left;
}
while(!s.empty())
{
t=s.pop();
if(s1.pop()==NULL)
{
s.push(t);
s1.push((node *)1);
t=t->right;
while(t!=NULL)
{
s.push(t);
s1.push(NULL);
t=t->left;
}
}
else
cout<<" "<<t->data;
}
}
void main()
{
tree t1;
node *p;
int x,op;
clrscr();
do
{
cout<<"\n*****MENU*****";
cout<<"\n1.Create \n2.Recursive Preorder";
cout<<"\n3.Recursive Inorder\n4.Recursive Postorder";
cout<<"\n5.Non-recursive Inorder \n6.Non-recursive Preorder";
cout<<"\n7.Non-recursive Postorder \n8.Quit";
cout<<"\nEnter your choice:";
cin>>op;
switch(op)
{
case 1:
t1.create();
break;
case 2:
t1.preorder();
break;
case 3:
t1.inorder();
break;
case 4:
t1.postorder();
break;
case 5:
t1.non_rec_inorder();
break;
case 6:
t1.non_rec_preorder();
break;
case 7:
t1.non_rec_postorder();
break;
}
}while(op!=8);
}
No comments:
Post a Comment
Thanks for your valuable comment