#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<process.h>
#define MAX 10
// #define LEFTCHILD(K) 2K-1
// #define RIGHTCHILD(K) 2K
struct btree
{
int data;
int k;
}tree[100];
void initialize(struct btree tree[],int maxlength)
{
int i;
for(i=0;i<=maxlength;i++)
{
tree[i].data=-1;
tree[i].k=0;
}
}
void create(struct btree tree[],int num)
{
int i;
if(tree[0].data==-1)
{
tree[0].data=num;
tree[0].k=1;
}
else
{
i=tree[0].k;
while(tree[i-1].data!=-1 && i<=MAX)
{
if(tree[i-1].data>num)
{
i=2*tree[i-1].k;
}
else
{
i=2*tree[i-1].k+1;
}
}
tree[i-1].k=i;
tree[i-1].data=num;
}
}
void display(void)
{
int row=0,col=0,root=0;
int i=0;
for(i=0;i<=MAX;i++)
{
printf("\n %d",tree[i].data);
}
//getch();
}
void main()
{
int length=10;
char ch='y';
int choice=0;
int key=0;
clrscr();
do
{
clrscr();
printf("\n\t <<< ARRAY REPRESENTATION OF BINARY TREE >>>\n");
printf("\n 1. Initialize. ");
printf("\n 2. Insert. ");
printf("\n 3. Delete. ");
printf("\n 4. Display. ");
printf("\n 5. EXIT. \n");
printf("\n Enter Your Choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
// INITIALIZE
initialize(tree,length);
printf("\n Binary Tree Initialized. \n");
break;
case 2:
// INSERT
printf("\n Enter The Key : ");
scanf("%d",&key);
create(tree,key);
break;
case 3:
// DELETE
break;
case 4:
// DISPLAY
display();
break;
case 5:
// EXIT
exit(1);
default:
printf("\n Invalid Choice . Try Again...");
}
fflush(stdin);
printf("\n Do you Wish to continue [y/n] :");
ch=getch();
}
while(ch=='y');
getch();
}
Thanks a lot for this
ReplyDelete