#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define NULL 0
struct node
{
struct node *left;
int info;
struct node *right;
};
void insert(struct node **,int);
void inorder(struct node *);
void postorder(struct node *);
void preorder(struct node *);
void main()
{
struct node *ptr;
int n,i,item,ch;
ptr=NULL;
clrscr();
while(1)
{
printf("\n BINARY search tree");
printf("\n 1.creation of bst");
printf("\n 2.preorder traversal");
printf("\n 3.inorder traversal");
printf("\n 4.postorder traversal");
printf("\n 5.exit");
printf("\n enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("enter number of terms to add to tree");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nenter the element");
scanf("%d",&item);
insert(&ptr,item);
}
break;
case 2:printf("preorder traversal");
preorder(ptr);
break;
case 3:printf("inorder traversal");
inorder(ptr);
break;
case 4:printf("postorder traversal");
postorder(ptr);
break;
case 5:exit(0);
}
}
}
void insert(struct node **p,int item)
{
if((*p)==NULL)
{
printf("\n node created");
(*p)=malloc(sizeof(struct node));
(*p)->left=NULL;
(*p)->right=NULL;
(*p)->info=item;
return;
}
else
if (item<(*p)->info)
{
printf("\n directed to left link");
insert(&((*p)->left),item);
}
else
{
printf("directed to right link");
insert(&((*p)->right),item);
}
return;
}
void inorder(struct node *p)
{
if(p!=NULL)
{
inorder(p->left);
printf("\n%d",p->info);
inorder(p->right);
}
else
return;
}
void preorder(struct node *p)
{
if(p!=NULL)
{
printf("\n%d",p->info);
preorder(p->left);
preorder(p->right);
}
else
return;
}
void postorder(struct node *p)
{
if(p!=NULL)
{
postorder(p->left);
postorder(p->right);
printf("\n %d",p->info);
}
else
return;
}
#include<conio.h>
#include<stdlib.h>
#define NULL 0
struct node
{
struct node *left;
int info;
struct node *right;
};
void insert(struct node **,int);
void inorder(struct node *);
void postorder(struct node *);
void preorder(struct node *);
void main()
{
struct node *ptr;
int n,i,item,ch;
ptr=NULL;
clrscr();
while(1)
{
printf("\n BINARY search tree");
printf("\n 1.creation of bst");
printf("\n 2.preorder traversal");
printf("\n 3.inorder traversal");
printf("\n 4.postorder traversal");
printf("\n 5.exit");
printf("\n enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("enter number of terms to add to tree");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nenter the element");
scanf("%d",&item);
insert(&ptr,item);
}
break;
case 2:printf("preorder traversal");
preorder(ptr);
break;
case 3:printf("inorder traversal");
inorder(ptr);
break;
case 4:printf("postorder traversal");
postorder(ptr);
break;
case 5:exit(0);
}
}
}
void insert(struct node **p,int item)
{
if((*p)==NULL)
{
printf("\n node created");
(*p)=malloc(sizeof(struct node));
(*p)->left=NULL;
(*p)->right=NULL;
(*p)->info=item;
return;
}
else
if (item<(*p)->info)
{
printf("\n directed to left link");
insert(&((*p)->left),item);
}
else
{
printf("directed to right link");
insert(&((*p)->right),item);
}
return;
}
void inorder(struct node *p)
{
if(p!=NULL)
{
inorder(p->left);
printf("\n%d",p->info);
inorder(p->right);
}
else
return;
}
void preorder(struct node *p)
{
if(p!=NULL)
{
printf("\n%d",p->info);
preorder(p->left);
preorder(p->right);
}
else
return;
}
void postorder(struct node *p)
{
if(p!=NULL)
{
postorder(p->left);
postorder(p->right);
printf("\n %d",p->info);
}
else
return;
}
No comments:
Post a Comment