BINARY TREE IMPLEMENTATION AND TRAVERSAL
#include<iostream.h>
#include<alloc.h>
#include<stdlib.h>
#include<conio.h>
struct freeptr
{
unsigned entry;
struct freeptr *right,*left;
}*root;
char waste;
void preorder(struct freeptr *root)
{
if(root)
{
cout<<"\t"<<root->entry;
preorder(root->left);
preorder(root->right);
}
}
void inorder(struct freeptr *root)
{
if(root)
{
inorder(root->left);
cout<<"\t"<<root->entry;
inorder(root->right);
}
}
void postorder(struct freeptr *root)
{
if(root)
{
postorder(root->left);
postorder(root->right);
cout<<"\t"<<root->entry;
}
}
int init(struct freeptr *t)
{
cin>>t->entry;
if(t->entry)
{
t->left=(struct freeptr*)malloc(sizeof(struct freeptr));
cout<<"the lest child of:"<<t->entry;
cout<<"\t";
if(init(t->left)==0)
{
free(t->left);
t->left=NULL;
}
cout<<"the right child of:"<<t->entry;
cout<<"\t";
t->right=(struct freeptr*)malloc(sizeof(struct freeptr));
if(init(t->right)==0)
{
free(t->right);
t->right=NULL;
}
return 1;
}
else
return 0;
}
void main()
{
clrscr();
root=(struct freeptr*)malloc(sizeof(struct freeptr));
cout<<"\n BINARY TREE TRAVERSAL-USING RECURSION"<<endl;
cout<<"enter the binary tree starting from the root node"<<endl;
init(root);
cout<<"press any key.."<<endl;
waste=getch();
cout<<"by preorder traversal"<<endl;
preorder(root);
cout<<endl;
cout<<"by inorder traversal"<<endl;
inorder(root);
cout<<endl;
cout<<"by postorder traversal"<<endl;
postorder(root);
cout<<endl;
getch();
}
OUTPUT:
BINARY TREE TRAVERSAL-USING RECURSION
enter the binary tree starting from the root node
1
the lest child of:1 5
the lest child of:5 1
the lest child of:1 2
the lest child of:2 3
the lest child of:3 0
the right child of:3 0
the right child of:2 1
the lest child of:1 2
the lest child of:2 5
the lest child of:5 0
the right child of:5 0
the right child of:2 0
the right child of:1 0
the right child of:1 0
the right child of:5 0
the right child of:1 0
press any key..
by preorder traversal
1 5 1 2 3 1 2 5
by inorder traversal
3 2 5 2 1 1 5 1
by postorder traversal
3 5 2 1 2 1 5 1
#include<iostream.h>
#include<alloc.h>
#include<stdlib.h>
#include<conio.h>
struct freeptr
{
unsigned entry;
struct freeptr *right,*left;
}*root;
char waste;
void preorder(struct freeptr *root)
{
if(root)
{
cout<<"\t"<<root->entry;
preorder(root->left);
preorder(root->right);
}
}
void inorder(struct freeptr *root)
{
if(root)
{
inorder(root->left);
cout<<"\t"<<root->entry;
inorder(root->right);
}
}
void postorder(struct freeptr *root)
{
if(root)
{
postorder(root->left);
postorder(root->right);
cout<<"\t"<<root->entry;
}
}
int init(struct freeptr *t)
{
cin>>t->entry;
if(t->entry)
{
t->left=(struct freeptr*)malloc(sizeof(struct freeptr));
cout<<"the lest child of:"<<t->entry;
cout<<"\t";
if(init(t->left)==0)
{
free(t->left);
t->left=NULL;
}
cout<<"the right child of:"<<t->entry;
cout<<"\t";
t->right=(struct freeptr*)malloc(sizeof(struct freeptr));
if(init(t->right)==0)
{
free(t->right);
t->right=NULL;
}
return 1;
}
else
return 0;
}
void main()
{
clrscr();
root=(struct freeptr*)malloc(sizeof(struct freeptr));
cout<<"\n BINARY TREE TRAVERSAL-USING RECURSION"<<endl;
cout<<"enter the binary tree starting from the root node"<<endl;
init(root);
cout<<"press any key.."<<endl;
waste=getch();
cout<<"by preorder traversal"<<endl;
preorder(root);
cout<<endl;
cout<<"by inorder traversal"<<endl;
inorder(root);
cout<<endl;
cout<<"by postorder traversal"<<endl;
postorder(root);
cout<<endl;
getch();
}
OUTPUT:
BINARY TREE TRAVERSAL-USING RECURSION
enter the binary tree starting from the root node
1
the lest child of:1 5
the lest child of:5 1
the lest child of:1 2
the lest child of:2 3
the lest child of:3 0
the right child of:3 0
the right child of:2 1
the lest child of:1 2
the lest child of:2 5
the lest child of:5 0
the right child of:5 0
the right child of:2 0
the right child of:1 0
the right child of:1 0
the right child of:5 0
the right child of:1 0
press any key..
by preorder traversal
1 5 1 2 3 1 2 5
by inorder traversal
3 2 5 2 1 1 5 1
by postorder traversal
3 5 2 1 2 1 5 1
No comments:
Post a Comment