Tree data structure always attract me lot. I made this complete solution for tree data structure for computer science students.
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct tree
{
int info;
struct tree *left;
struct tree *right;
};
struct tree *makenode(int item)
{
struct tree *n;
n=(struct tree *)malloc(sizeof(struct tree));
n->left=NULL;
n->right=NULL;
n->info=item;
return n;
}
struct tree *find(struct tree *r,int item)
{
struct tree *p;
p=NULL;
while(r)
{
if(r->info==item)
return p;
if(r->info>item)
{
p=r;
r=r->left;
}
else
{
p=r;
r=r->right;
}
}
return p;
}
void create(struct tree **r,int item)
{
struct tree *ptr;
if(!(*r))
{
*r=makenode(item);
}
else
{
ptr=find(*r,item);
if(ptr)
{
if(ptr->info>item)
ptr->left=makenode(item);
else
ptr->right=makenode(item);
}
else
printf("Not possible");
}
}
void pre(struct tree *r)
{
if(r)
{
printf("\nAddress %u Value %d Left %u Right %u",r,r->info,r->left,r->right);
pre(r->left);
pre(r->right);
}
}
void nonpre(struct tree *r)
{
struct tree *arr[20];
int top=0;
arr[0]=NULL;
while(r)
{
printf("\nAddress %u Value %d Left %u Right %u",r,r->info,r->left,r->right);
if(r->right)
{
top++;
arr[top]=r->right;
}
if(!r->left)
{
r=arr[top];top--;
}
else
r=r->left;
}
}
void in(struct tree *r)
{
struct tree *arr[20];
int top=0;
arr[0]=NULL;
while(top>-1)
{
while(r)
{
top++;arr[top]=r;
r=r->left;
}
r=arr[top];top--;
while(r)
{
printf("\nAddress %u Value %d Left %u Right %u",r,r->info,r->left,r->right);
if(r->right)
{
r=r->right;
break;
}
r=arr[top];top--;
}
}
}
void main()
{
int item,i;
struct tree *r;
clrscr();
r=NULL;
for(i=0;i<=9;i++)
{
printf("Enter the number:");
scanf("%d",&item);
create(&r,item);
}
nonpre(r);
in(r);
free(r);
getch();
}
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct tree
{
int info;
struct tree *left;
struct tree *right;
};
struct tree *makenode(int item)
{
struct tree *n;
n=(struct tree *)malloc(sizeof(struct tree));
n->left=NULL;
n->right=NULL;
n->info=item;
return n;
}
struct tree *find(struct tree *r,int item)
{
struct tree *p;
p=NULL;
while(r)
{
if(r->info==item)
return p;
if(r->info>item)
{
p=r;
r=r->left;
}
else
{
p=r;
r=r->right;
}
}
return p;
}
void create(struct tree **r,int item)
{
struct tree *ptr;
if(!(*r))
{
*r=makenode(item);
}
else
{
ptr=find(*r,item);
if(ptr)
{
if(ptr->info>item)
ptr->left=makenode(item);
else
ptr->right=makenode(item);
}
else
printf("Not possible");
}
}
void pre(struct tree *r)
{
if(r)
{
printf("\nAddress %u Value %d Left %u Right %u",r,r->info,r->left,r->right);
pre(r->left);
pre(r->right);
}
}
void nonpre(struct tree *r)
{
struct tree *arr[20];
int top=0;
arr[0]=NULL;
while(r)
{
printf("\nAddress %u Value %d Left %u Right %u",r,r->info,r->left,r->right);
if(r->right)
{
top++;
arr[top]=r->right;
}
if(!r->left)
{
r=arr[top];top--;
}
else
r=r->left;
}
}
void in(struct tree *r)
{
struct tree *arr[20];
int top=0;
arr[0]=NULL;
while(top>-1)
{
while(r)
{
top++;arr[top]=r;
r=r->left;
}
r=arr[top];top--;
while(r)
{
printf("\nAddress %u Value %d Left %u Right %u",r,r->info,r->left,r->right);
if(r->right)
{
r=r->right;
break;
}
r=arr[top];top--;
}
}
}
void main()
{
int item,i;
struct tree *r;
clrscr();
r=NULL;
for(i=0;i<=9;i++)
{
printf("Enter the number:");
scanf("%d",&item);
create(&r,item);
}
nonpre(r);
in(r);
free(r);
getch();
}
0 comments:
Post a Comment
You are most welcome for making comments here. I will consider your view and will reply you soon.