Binary Tree Using Array
#include
#include
#include
char arr[1000];
void inorder(int i){
if(arr[i]!='-'){
inorder(2*i+1);
printf("%c",arr[i]);
inorder(2*i+2);
}
}
void create(int i)
{
int opt;
char x;
if(arr[0]=='-'){
printf("enter root element");
scanf("%c",&arr[0]);
fflush(stdin);
create(0);
}
else{
printf("do u want left of %c",arr[i]);
scanf("%d",&opt);
fflush(stdin);
if(opt==1)
{
printf("enter element");
scanf("%c",&arr[2*i+1]);
fflush(stdin);
create(2*i+1);
}
printf("do u want right of %c",arr[i]);
scanf("%d",&opt);
fflush(stdin);
if(opt==1)
{
printf("enter element");
scanf("%c",&arr[2*i+2]);
fflush(stdin);
create(2*i+2);
}
}
}
main()
{
for(int i=0;i<1000 i="" p=""> arr[i]='-';
create(0);
printf("elements are");
inorder(0);
getch();
}
0 comments:
Post a Comment