Anda di halaman 1dari 10

#include<stdio.

h>
#include<stdlib.h>
#include<ctype.h>
typedef struct tree
{
struct tree *left;
int data;
struct tree *right;
}node;
node *root=NULL,*leaf,*temp;
int count=0;
int search(node*,int);
void insert();
void delete();
void display(node*);
int main(void)
{
int ch;
do
{
printf("\n********MENU********\n");
printf("1-->INSERT\n2-->DELETE\n3-->DISPLAY\n4-->EXIT\n\nENTER YOUR CHOI
CE : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
leaf=root;
display(leaf);
break;
case 4:
exit(0);
default:
printf("Invalid Choice. Please enter from 1 to 4\n");
}
}while(ch!=4);
}
void insert()
{
int ch,parent,flag=0;
char child;
do
{
temp=(node*)malloc(sizeof(node));
if(root==NULL)
{
printf("Enter root value of the tree : ");
scanf("%d",&temp->data);
root=temp;
root->left=NULL;
root->right=NULL;

//count++;
}
else
{
count=0;
leaf=root;
printf("Enter parent node's value to create its child : ");
scanf("%d",&parent);
search(leaf,parent);
if(count==0)
printf("No such parent node found\n");
else if(count==1)
{
do
{
printf("Enter 'L' for Left Child or 'R' for Right Child : ")
;
getchar();
scanf("%c",&child);
child=toupper(child);
if(child!='L' && child!='R')
printf("Invalid Choice. Enter 'L' or 'R'\n");
}while(child!='L' && child!='R');
if((child=='L' && leaf->left==NULL) || (child=='R' && leaf->righ
t==NULL))
flag=1;
if(flag==1)
{
printf("Enter value of leaf : ");
scanf("%d",&temp->data);
temp->left=NULL;
temp->right=NULL;
if(child=='L')
{
leaf->left=temp;
leaf=leaf->left;
}
else
{
leaf->right=temp;
leaf=leaf->right;
}
//count++;
}
else
printf("Node is not empty\n");
flag=0;
}
}
printf("Press 0 to continue. Press any other key to terminate\n");
scanf("%d",&ch);
}while(ch==0);
}
void delete()
{
node *ptr;
int ch,parent;
if(root==NULL)
{

printf("The Binary Tree is empty\n");


return;
}
do
{
leaf=root;
printf("Enter node's value to delete : ");
scanf("%d",&parent);
if(search(leaf,parent)==0)
printf("No such node found\n");
else
{
if(leaf->left!=NULL || leaf->right!=NULL)
{
printf("Entered node is a parent node. Deleting this node will a
lso delete its children.\nPress 0 to continue. Press any other key to terminate\
n");
scanf("%d",&ch);
if(ch==0)
{
printf("Entered node is deleted\n");
leaf->left=leaf->right=NULL;
free(leaf);
}
else
printf("Entered node is not deleted\n");
}
else
{
printf("Entered node is deleted\n");
free(leaf);
}
}
printf("Press 0 to delete more nodes. Press any other key to terminate\n
");
scanf("%d",&ch);
}while(ch==0);
}
void display(node *leaf)
{
if(root==NULL)
{
printf("The Binary Tree is empty\n");
return;
}
if(leaf!=NULL)
{
printf("%d\t",leaf->data);
display(leaf->left);
display(leaf->right);
}
}
int search(node *leaf,int parent)
{
if(leaf!=NULL)
{
if(leaf->data==parent)
{

count=1;
return 1;
}
else
{
search(leaf->left,parent);
search(leaf->right,parent);
}
}
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
typedef struct tree
{
struct tree *left;
int data;
struct tree *right;
}node;
node *root=NULL,*leaf,*temp;
int count=0;
int search(node*,int);
void insert();
void delete();
void display(node*);
int main(void)
{
int ch;
do
{
printf("\n********MENU********\n");
printf("1-->INSERT\n2-->DELETE\n3-->DISPLAY\n4-->EXIT\n\nENTER YOUR CHOI
CE : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
leaf=root;
display(leaf);
break;
case 4:
exit(0);
default:
printf("Invalid Choice. Please enter from 1 to 4\n");
}
}while(ch!=4);
}
void insert()
{
int ch,parent,flag=0;

char child;
do
{
temp=(node*)malloc(sizeof(node));
if(root==NULL)
{
printf("Enter root value of the tree : ");
scanf("%d",&temp->data);
root=temp;
root->left=NULL;
root->right=NULL;
//count++;
}
else
{
count=0;
leaf=root;
printf("Enter parent node's value to create its child : ");
scanf("%d",&parent);
search(leaf,parent);
if(count==0)
printf("No such parent node found\n");
else if(count==1)
{
do
{
printf("Enter 'L' for Left Child or 'R' for Right Child : ")
;
getchar();
scanf("%c",&child);
child=toupper(child);
if(child!='L' && child!='R')
printf("Invalid Choice. Enter 'L' or 'R'\n");
}while(child!='L' && child!='R');
if((child=='L' && leaf->left==NULL) || (child=='R' && leaf->righ
t==NULL))
flag=1;
if(flag==1)
{
printf("Enter value of leaf : ");
scanf("%d",&temp->data);
temp->left=NULL;
temp->right=NULL;
if(child=='L')
{
leaf->left=temp;
leaf=leaf->left;
}
else
{
leaf->right=temp;
leaf=leaf->right;
}
//count++;
}
else
printf("Node is not empty\n");
flag=0;
}
}

printf("Press 0 to continue. Press any other key to terminate\n");


scanf("%d",&ch);
}while(ch==0);
}
void delete()
{
node *ptr;
int ch,parent;
if(root==NULL)
{
printf("The Binary Tree is empty\n");
return;
}
do
{
leaf=root;
printf("Enter node's value to delete : ");
scanf("%d",&parent);
if(search(leaf,parent)==0)
printf("No such node found\n");
else
{
if(leaf->left!=NULL || leaf->right!=NULL)
{
printf("Entered node is a parent node. Deleting this node will a
lso delete its children.\nPress 0 to continue. Press any other key to terminate\
n");
scanf("%d",&ch);
if(ch==0)
{
printf("Entered node is deleted\n");
leaf->left=leaf->right=NULL;
free(leaf);
}
else
printf("Entered node is not deleted\n");
}
else
{
printf("Entered node is deleted\n");
free(leaf);
}
}
printf("Press 0 to delete more nodes. Press any other key to terminate\n
");
scanf("%d",&ch);
}while(ch==0);
}
void display(node *leaf)
{
if(root==NULL)
{
printf("The Binary Tree is empty\n");
return;
}
if(leaf!=NULL)
{
printf("%d\t",leaf->data);

display(leaf->left);
display(leaf->right);
}
}
int search(node *leaf,int parent)
{
if(leaf!=NULL)
{
if(leaf->data==parent)
{
count=1;
return 1;
}
else
{
search(leaf->left,parent);
search(leaf->right,parent);
}
}
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
typedef struct tree
{
struct tree *left;
int data;
struct tree *right;
}node;
node *root=NULL,*leaf,*temp;
int count=0;
int search(node*,int);
void insert();
void delete();
void display(node*);
int main(void)
{
int ch;
do
{
printf("\n********MENU********\n");
printf("1-->INSERT\n2-->DELETE\n3-->DISPLAY\n4-->EXIT\n\nENTER YOUR CHOI
CE : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
leaf=root;
display(leaf);
break;

case 4:
exit(0);
default:
printf("Invalid Choice. Please enter from 1 to 4\n");
}
}while(ch!=4);
}
void insert()
{
int ch,parent,flag=0;
char child;
do
{
temp=(node*)malloc(sizeof(node));
if(root==NULL)
{
printf("Enter root value of the tree : ");
scanf("%d",&temp->data);
root=temp;
root->left=NULL;
root->right=NULL;
//count++;
}
else
{
count=0;
leaf=root;
printf("Enter parent node's value to create its child : ");
scanf("%d",&parent);
search(leaf,parent);
if(count==0)
printf("No such parent node found\n");
else if(count==1)
{
do
{
printf("Enter 'L' for Left Child or 'R' for Right Child : ")
;
getchar();
scanf("%c",&child);
child=toupper(child);
if(child!='L' && child!='R')
printf("Invalid Choice. Enter 'L' or 'R'\n");
}while(child!='L' && child!='R');
if((child=='L' && leaf->left==NULL) || (child=='R' && leaf->righ
t==NULL))
flag=1;
if(flag==1)
{
printf("Enter value of leaf : ");
scanf("%d",&temp->data);
temp->left=NULL;
temp->right=NULL;
if(child=='L')
{
leaf->left=temp;
leaf=leaf->left;
}
else

{
leaf->right=temp;
leaf=leaf->right;
}
//count++;
}
else
printf("Node is not empty\n");
flag=0;
}
}
printf("Press 0 to continue. Press any other key to terminate\n");
scanf("%d",&ch);
}while(ch==0);
}
void delete()
{
node *ptr;
int ch,parent;
if(root==NULL)
{
printf("The Binary Tree is empty\n");
return;
}
do
{
leaf=root;
printf("Enter node's value to delete : ");
scanf("%d",&parent);
if(search(leaf,parent)==0)
printf("No such node found\n");
else
{
if(leaf->left!=NULL || leaf->right!=NULL)
{
printf("Entered node is a parent node. Deleting this node will a
lso delete its children.\nPress 0 to continue. Press any other key to terminate\
n");
scanf("%d",&ch);
if(ch==0)
{
printf("Entered node is deleted\n");
leaf->left=leaf->right=NULL;
free(leaf);
}
else
printf("Entered node is not deleted\n");
}
else
{
printf("Entered node is deleted\n");
free(leaf);
}
}
printf("Press 0 to delete more nodes. Press any other key to terminate\n
");
scanf("%d",&ch);
}while(ch==0);
}

void display(node *leaf)


{
if(root==NULL)
{
printf("The Binary Tree is empty\n");
return;
}
if(leaf!=NULL)
{
printf("%d\t",leaf->data);
display(leaf->left);
display(leaf->right);
}
}
int search(node *leaf,int parent)
{
if(leaf!=NULL)
{
if(leaf->data==parent)
{
count=1;
return 1;
}
else
{
search(leaf->left,parent);
search(leaf->right,parent);
}
}
return 0;
}
v

Anda mungkin juga menyukai