Bitree
Bitree
h>
#include<stdlib.h>
struct node{
int data;
struct node *lc,*rc;
};
struct node*root=NULL;
void insert(int d){
struct node *newnode,*parent,*current;
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=d;
newnode->lc=NULL;
newnode->rc=NULL;
if(root==NULL)
{
root=newnode;
}
else
{
current=root;
parent=NULL;
while(1){
parent=current;
if(d<parent->data)
{
current=current->lc;
if(current==NULL){
parent->lc=newnode;
return;
}
}
else{
current=current->rc;
if(current==NULL){
parent->rc=newnode;
return;
}
}
}
}
}
void pre_order_traversal(struct node *root)
{
if(root!=NULL){
printf("%d\t",root->data);
pre_order_traversal(root->lc);
pre_order_traversal(root->rc);
}
}
void in_order_traversal(struct node *root)
{
if(root!=NULL){
in_order_traversal(root->lc);
printf("%d\t",root->data);
in_order_traversal(root->rc);
}
}
void post_order_traversal(struct node *root)
{
if(root!=NULL){
post_order_traversal(root->lc);
post_order_traversal(root->rc);
printf("%d\t",root->data);
}
}
void main(){
int a[20],n,i;
printf("enter array size=");
scanf("%d",&n);
printf("enter values=");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
for(i=0;i<n;i++){
insert(a[i]);
}
printf("\n pre order traversal=\n");
pre_order_traversal(root);
printf("\n in order trsaversal=\n");
in_order_traversal(root);
printf("\n post order traversal=\n");
post_order_traversal(root);
}