Sei sulla pagina 1di 9

Q5.Program to insert node in binary tree and display nodes of binary tree using traversal.

#include<stdio.h>
#include<conio.h>
struct node
{
int num;
struct node *left;
struct node *right;
};
typedef struct node node;
node *P=NULL;
node *insert(node *P,int digit);
void preorder(node *P);
void inorder(node *P);
void postorder(node *P);
int count=1;
node *insert(node *P,int digit)
{
if(P==NULL)
{
node *tmp;
tmp=(struct node*)malloc(sizeof(node));
tmp->num=digit;
tmp->left=NULL;
tmp->right=NULL;
count++;
return tmp;
}
else if(count%2==0)
P->left=insert(P->left,digit);
else
P->right=insert(P->right,digit);
return(P);
}
void preorder(node *P)
{
if(P!=NULL)
{
printf("%d\n",P->num);
preorder(P->left);
preorder(P->right);
}
}
void inorder(node *P)
{
if(P!=NULL)
{
inorder(P->left);
printf("%d\n",P->num);
inorder(P->right);
}
}
void postorder(node *P)
{
if(P!=NULL)
{
postorder(P->left);
postorder(P->right);
printf("%d\n",P->num);
}
}
void main()
{
int c,digit;
char ch;
clrscr();
one:
printf("\nChoose any one:\n1.Insert a node in the Binary Tree\n2.Display Binary Tree in
Preorder\n3.Display Binary Tree in Inorder\n4.Display Binary Tree in Postorder\n5.Exit");
scanf("%d",&c);
switch(c)
{
case 1:printf("\nEnter a digit.To quit enter 0.\n");
scanf("%d",&digit);
while(digit!=0)
{
P=insert(P,digit);
scanf("%d",&digit);
}
goto one;
break;
case 2:printf("\nPreorder Traversing Tree\n");
preorder(P);
goto one;
break;
case 3:printf("\nInorder Traversing Tree\n");
inorder(P);
goto one;
break;
case 4:printf("\nPostorder Traversing Tree\n");
postorder(P);
goto one;
break;
case 5:exit(0);
break;
default:goto two;
break;
}
two:
printf("\nWrong choice");
printf("\nDo you want to continue?\nPress:\ny for Yes\nn for ?No");
scanf("%d",&c);
scanf("%c",&ch);
if(ch=='y')
goto one;
getch();
}

OUTPUT:
Q6.Program to show various functions of Binary Search Tree.

#include<stdio.h>
#include<conio.h>
struct node
{
int num;
struct node *left;
struct node *right;
}*temp=NULL;
typedef struct node node;
node *P=NULL;
node *insert(node *P,int digit)
{
node *temp;
if(P==NULL)
{
temp=(struct node *)malloc(sizeof(struct node));
temp->num=digit;
temp->left=NULL;
temp->right=NULL;
return temp;
}
else
if(digit<P->num)
P->left=insert(P->left,digit);
else
if(digit>P-> num)
P->right=insert(P->right,digit);
else if(digit==P->num)
{
printf("Duplicate node : program exited");
exit(0);
}
return(P);
}
void search(node *P,int digit)
{
if(P==NULL)
printf("\nThe number does not exist.");
else
if(digit==P->num)
printf("\nValue %d found",digit);
else
if(digit<P->num)
search(P->left,digit);
else
search(P->right,digit);
}
void display(node *P)
{
if(P!=NULL)
{
display(P->left);
printf("%d\n",P->num);
display(P->right);
}
}
node *minvalue(node *P)
{
if(P==NULL)
return NULL;
if(P->left)
return minvalue(P->left);
else
return P;
}
node *delnode(int digit,node *P)
{
if(P==NULL)
return P;
else if(digit< P->num)
P->left=delnode(digit,P->left);
else if(digit> P->num)
P->right=delnode(digit,P->right);
else
{
if((P->right==NULL)&&(P->left==NULL))
{
free(P);
P=NULL;
return P;
}
else if(P->left==NULL)
{
temp=P;
P=P->right;
free(temp);
return P;
}
else if(P->right==NULL)
{
temp=P;
P=P->left;
free(temp);
return P;
}
else
{
temp=minvalue(P->right);
P->num=temp->num;
P->right=delnode(temp->num,P->right);
}
}
printf("\nElement deleted successfully");
}
void main()
{
int c,digit;
char ch;
clrscr();
one:
printf("\nChoose any one:\n1.Insert a node in Binary Search Tree\n2.Search a node in
Binary Search Tree\n3.Display the Binary Search Tree\n4.Delete a node from Binary Search
Tree\n5.Exit");
scanf("%d",&c);
switch(c)
{
case 1:printf("\nEnter integer. To quit press 0\n");
scanf("%d",&digit);
while(digit!=0)
{
P=insert(P,digit);
scanf("%d",&digit);
}
goto one;
break;
case 2:printf("\nEnter the number to be searched");
scanf("%d",&digit);
search(P,digit);
goto one;
break;
case 3:printf("\nThe elements of Binary Search Tree are:\n");
display(P);
goto one;
break;
case 4:printf("\nEnter the element to be deleted");
scanf("%d",&digit);
delnode(digit,P);
goto one;
break;
case 5:exit(0);
break;
default:goto two;
}
two:
printf("\nWrong choice");
printf("\nDo you want to continue?\nPress:\ny for Yes\nnfor No");
scanf("%d",&c);
scanf("%c",&ch);
if(ch=='y')
goto one;
getch();
}
OUTPUT:

Potrebbero piacerti anche