Binary Tree Using Linked List
#include
#include
#include
struct Node{
struct Node *lchild;
struct Node *rchild;
int data;
}*root=NULL;
void inorder(struct Node *temp)
{
if(temp!=NULL)
{
inorder(temp->lchild);
printf("%d",temp->data);
inorder(temp->rchild);
}
}
void create(struct Node *temp)
{
struct Node *q;
int x,opt;
q=(struct Node *)malloc(sizeof(struct Node));
q->lchild=q->rchild=NULL;
if(root==NULL){
printf("enter root element");
scanf("%d",&x);
q->data=x;
root=q;
create(root);
}
else{
printf("do u want left of %d",temp->data);
scanf("%d",&opt);
if(opt==1)
{
q=(struct Node *)malloc(sizeof(struct Node));
q->lchild=q->rchild=NULL;
printf("enter element");
scanf("%d",&x);
q->data=x;
temp->lchild=q;
create(temp->lchild);
}
printf("do u want right of %d",temp->data);
scanf("%d",&opt);
if(opt==1)
{
q=(struct Node *)malloc(sizeof(struct Node));
q->lchild=q->rchild=NULL;
printf("enter element");
scanf("%d",&x);
q->data=x;
temp->rchild=q;
create(temp->rchild);
}
}
}
main()
{
create(root);
printf("elements are");
inorder(root);
getch();
}
0 comments:
Post a Comment