Tuesday 24 April 2012

5 th Experiment BINARY TREE TRAVERSAL


                                                          ALGORITHM:-TREE TRAVERSAL:
--------------------------------------------------------------------------------------------------------------------------------------

Step-1: Start.
Step-2: Create pointer ‘*b’ to structure ‘bintree’ to insert elements and traverse the tree.
Step-3: Read the total no of elements.
Step-4: Read the elements of the tree using the function insert (&b,data) which accesses data from the structure bintree.
Step-5: For preorder traversal, invoke the function preorder(b).
Step-6: For inorder traversal, invoke the function inorder (b).
Step-7: For postorder traversal, invoke the function postorder (b).
Step-8: Stop.

---------------------------------------------------------------------------------------------------------------------------------------
                                                                 FLOW CHARTs
--------------------------------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------------------------------
C++ IMPLEMENTATION
-------------------------------------------------------------------------------------------------------------------------------------

// program for tree traversing using linked list

#include<iostream.h>
#include<conio.h>
#define NULL 0
struct bintree
{
struct bintree *left;
int data;
struct bintree *right;
};
void insert(struct bintree **sr,int num)
{
if(*sr==NULL)
{
*sr=new bintree;
(*sr)->left=NULL;
(*sr)->data=num;
(*sr)->right=NULL;
}
else
{
if(num<(*sr)->data)
insert(&(*sr)->left,num);
else
insert(&(*sr)->right,num);
}
return;
}
void inorder(bintree *sr)
{
if(sr!=NULL)
{
inorder(sr->left);
cout<<"\n\t\t\t"<<sr->data;
inorder(sr->right);
}
else
return;
}
void postorder(bintree *sr)
{
if(sr!=NULL)
{
postorder(sr->left);
postorder(sr->right);
cout<<"\n\t\t\t"<<sr->data;
}
else
return;
}
void preorder(bintree *sr)
{
if(sr!=NULL)
{
cout<<"\n\t\t\t"<<sr->data;
preorder(sr->left);
preorder(sr->right);

}
else
return;
}
//***********************************************************
void main()
{
int no,data,i=1;
bintree *b;
b=NULL;
clrscr();
cout<<"\n\t\t\t BINARY TREE TRAVERSAL\n";
cout<<"\n\t\t\t ---------------------\n";
cout<<"\n\t      Enter Total no of elment in BINARY TREE\n";
cin>>no;
cout<<"entered no="<<no<<"\n";
while(i++<=no)
{
cout<<"\n\t\t\t Enter ELEMENTS:\n";
cin>>data;
cout<<data<<"\n";
insert(&b,data);
}
clrscr();
cout<<"\n\t\t\t PREORDER TRAVERSAL\n";
cout<<"\n\t\t\t-------------------\n";
preorder(b);
getch();
clrscr();

cout<<"\n\t\t\t INORDER TRAVERSAL\n";
cout<<"\n\t\t\t -----------------\n";
inorder(b);
getch();
clrscr();

cout<<"\n\t\t\t POST ORDER TRAVERSAL\n";
cout<<"\n\t\t\t --------------------\n";
postorder(b);
getch();
clrscr();

cout<<"\n\t\t THANKS FOR USING PROGRAM:\n";
cout<<"\n\t    BYE !!! press any key2 EXIT\n";
getch();

}
//---------------------------------------------------------------------------------------------------------------
out put
tree traversal

enter how many element u want to process
3
enter your elements
25
23
26
pre order traversal
25
23
26
inorder traversal
23
25
26
post order traversal
23
26
25
Thanks for using program
BYE…!



No comments:

Post a Comment