theme

" जीतने वाले छोड़ते नहीं और छोड़ने वाले जीतते नही "

Wednesday, July 6, 2011

programme to print the binary tree

#include
#include
#include
struct btreenode
{
struct btreenode*leftchild;
int data;
struct btreenode*rightchild;
};
void insert(struct btreenode**,int);
void inorder(struct btreenode*);
void preorder(struct btreenode*);
void postorder(struct btreenode*);
void main()
{
struct btreenode*bt;
int req,i=1,num;
bt=NULL;
clrscr();
printf("specify the number of items to be inserted:");
scanf("%d",&req);
while(i++<=req)
{
printf("enter the data");
scanf("%d",&num);
insert(&bt,num);
}
printf("\ninorder travershal");
inorder(bt);
printf("\n preorder travershal");
preorder(bt);
printf("\n post order travershal");
postorder(bt);
}
void insert(struct btreenode **sr,int num)
{
if(*sr==NULL)
{
*sr=malloc(sizeof(struct btreenode));
(*sr)->leftchild=NULL;
(*sr)->data=NULL;
(*sr)->rightchild=NULL;
return;
}
else
{
if(num<(*sr)->data)
insert(&((*sr)->rightchild),num);
else
insert(&((*sr)->leftchild),num);
}
return;
}
void inorder(struct btreenode*sr)
{
if(sr!=NULL)
{
inorder(sr->leftchild);
printf("\t%d",sr->data);
inorder(sr->rightchild);
}
else
return;
}
void postorder(struct btreenode*sr)
{
if(sr!=NULL)
{
postorder(sr->leftchild);
postorder(sr->rightchild);
printf("\t%d",sr->data);
}
else
return;
}
void preorder(struct btreenode*sr)
{
if(sr!=NULL)
{
printf("\t%d",sr->data);
preorder(sr->leftchild);
preorder(sr->rightchild);
}
else
return;
}


No comments:

Post a Comment