0% found this document useful (0 votes)
12 views2 pages

Bitree

About bitee program source code
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Bitree

About bitee program source code
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include<stdio.

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);
}

You might also like