In: Computer Science
I am giving you some code - with basically the main function and calling a a handful of functions (one I think I've kept so you can see an example - inserting new data into the binary tree) and then displaying the tree, etc.... so basically I want you to supply the code for the corresponding functions to be called.
#include<stdio.h>
#include<stdlib.h>
/* set the Tree Structure */
struct tree {
int data;
struct tree *left;
struct tree *right;
} *root = NULL, *node = NULL, *temp = NULL;
/* Function for INSERT NEW data into the Tree */
struct tree* insert(int key,struct tree *leaf) {
if(leaf == 0) {
struct tree *temp;
temp = (struct tree *)malloc(sizeof(struct tree));
temp->data = key;
temp->left = 0;
temp->right = 0;
printf("Data inserted!\n");
return temp;
}
else {
if(key < leaf->data)
leaf->left = insert(key,leaf->left);
else
leaf->right = insert(key,leaf->right);
}
return leaf;
}
/* Function for SEARCH data from Tree */
struct tree* search(int key,struct tree *leaf)
*****COMPLETE THIS PORTION OF THE CODE FOR THE SEARCH FUNCTION*****
/* Function for FIND MINimum value from the Tree */
struct tree* minvalue(struct tree *node)
*****COMPLETE THIS PORTION OF THE CODE FOR THE FIND THE MINIMUM VALUE FUNCTION*****
/* Function for FIND MAXimum value from the Tree */
struct tree* maxvalue(struct tree *node)
*****COMPLETE THIS PORTION OF THE CODE FOR THE FIND THE MAXIMUM VALUE FUNCTION*****
/* Function for PRINT binary tree in Preorder format */
void preorder(struct tree *leaf) {
*****COMPLETE THIS PORTION OF THE CODE FOR THE PRINT PREORDER FUNCTION*****
/* Function for PRINT binary tree in Inorder format */
void inorder(struct tree *leaf)
*****COMPLETE THIS PORTION OF THE CODE FOR THE PRINT INORDER FUNCTION*****
/* Function for PRINT binary tree in Postorder format */
void postorder(struct tree *leaf)
*****COMPLETE THIS PORTION OF THE CODE FOR THE PRINT POSTORDER FUNCTION*****
/* Function for DELETE node from the Tree */
struct tree* delete(struct tree *leaf, int key)
*****COMPLETE THIS PORTION OF THE CODE FOR THE DELETE A NODE FROM THE TREE FUNCTION*****
int main() {
int key, choice;
while(choice != 7) {
printf("1. Insert\n2. Search\n3. Delete\n4. Display\n5. Min Value\n6. Max Value\n7. Exit\n");
printf("Enter your choice:\n");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("\nEnter the value to insert:\n");
scanf("%d", &key);
root = insert(key, root);
break;
case 2:
printf("\nEnter the value to search:\n");
scanf("%d", &key);
search(key,root);
break;
case 3:
printf("\nEnter the value to delete:\n");
scanf("%d", &key);
delete(root,key);
break;
case 4:
printf("Preorder:\n");
preorder(root);
printf("Inorder:\n");
inorder(root);
printf("Postorder:\n");
postorder(root);
break;
case 5:
if(minvalue(root) == NULL)
printf("Tree is empty!\n");
else
printf("Minimum value is %d\n", minvalue(root)->data);
break;
case 6:
if(maxvalue(root) == NULL)
printf("Tree is empty!\n");
else
printf("Maximum value is %d\n", maxvalue(root)->data);
break;
case 7:
printf("Bye Bye!\n");
exit(0);
break;
default:
printf("Invalid choice!\n");
}
}
return 0;
}
Completed code
#include<stdio.h>
#include<stdlib.h>
/* set the Tree Structure */
struct tree {
int data;
struct tree *left;
struct tree *right;
} *root = NULL, *node = NULL, *temp = NULL;
/* Function for INSERT NEW data into the Tree */
struct tree* insert(int key,struct tree *leaf) {
if(leaf == 0) {
struct tree *temp;
temp = (struct tree *)malloc(sizeof(struct tree));
temp->data = key;
temp->left = 0;
temp->right = 0;
printf("Data inserted!\n");
return temp;
}
else {
if(key < leaf->data)
leaf->left = insert(key,leaf->left);
else
leaf->right = insert(key,leaf->right);
}
return leaf;
}
/* Function for SEARCH data from Tree */
struct tree* search(int key,struct tree *leaf)
{
if(leaf==NULL)
{printf("\n-- key is not found--\n");
return leaf;}
if(key<leaf->data)
leaf->left=search(key,leaf->left);
else
{ if(key>leaf->data)
leaf->right=search(key,leaf->right);
else
{ if(key==leaf->data)
printf("\n----key is found----\n");
}
}
return leaf;
}
/* Function for FIND MINimum value from the Tree */
struct tree* minvalue(struct tree *node)
{
if(node->left==NULL)
return node;
minvalue(node->left);
}
/* Function for FIND MAXimum value from the Tree */
struct tree* maxvalue(struct tree *node)
{
if(node->right==NULL)
return node;
minvalue(node->right);
}
/* Function for PRINT binary tree in Preorder format */
void preorder(struct tree *leaf)
{
if(leaf==NULL)
return;
printf(" %d ",leaf->data);
preorder(leaf->left);
preorder(leaf->right);
}
/* Function for PRINT binary tree in Inorder format */
void inorder(struct tree *leaf)
{
if(leaf==NULL)
return ;
inorder(leaf->left);
printf(" %d ",leaf->data);
inorder(leaf->right);
}
/* Function for PRINT binary tree in Postorder format */
void postorder(struct tree *leaf)
{
if(leaf==NULL)
return ;
postorder(leaf->left);
postorder(leaf->right);
printf(" %d ",leaf->data);
}
/* Function for DELETE node from the Tree */
struct tree* del(struct tree *leaf, int key)
{ struct tree *tmp,*succ;
if(leaf==NULL){
printf("\n key is not
found\n");
return leaf;
}
else if(key<leaf->data)
leaf->left=del(leaf->left,key);
else if(key>leaf->data)
leaf->right=del(leaf->right,key);
else if(leaf->left!=NULL &&
leaf->right!=NULL)
{
succ=leaf->right;
while(succ->right)
succ=succ->left;
leaf->data=succ->data;
leaf->right=del(leaf->right,succ->data);
}
else{
tmp=leaf;
if(leaf->left!=NULL)
leaf=leaf->left;
else if(leaf->right!=NULL)
leaf=leaf->right;
else
leaf=NULL;
free(tmp);
printf("\nsucessfull deleted----\n");
}
return leaf;
}
int main() {
int key, choice;
while(choice != 7) {
printf("\n1. Insert\n2. Search\n3. Delete\n4. Display\n5. Min
Value\n6. Max Value\n7. Exit\n");
printf("Enter your choice:\n");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("\nEnter the value to insert:\n");
scanf("%d", &key);
root = insert(key, root);
break;
case 2:
printf("\nEnter the value to search:\n");
scanf("%d", &key);
search(key,root);
break;
case 3:
printf("\nEnter the value to delete:\n");
scanf("%d", &key);
del(root,key);
break;
case 4:
printf("\nPreorder:-");
preorder(root);
printf("\nInorder:-");
inorder(root);
printf("\nPostorder:-");
postorder(root);
break;
case 5:
if(minvalue(root) == NULL)
printf("Tree is empty!\n");
else
printf("Minimum value is %d\n", minvalue(root)->data);
break;
case 6:
if(maxvalue(root) == NULL)
printf("Tree is empty!\n");
else
printf("Maximum value is %d\n", maxvalue(root)->data);
break;
case 7:
printf("Bye Bye!\n");
exit(0);
break;
default:
printf("Invalid choice!\n");
}
}
return 0;
}