Question

In: Computer Science

I am giving you some code - with basically the main function and calling a a...

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;
}

Solutions

Expert Solution

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;
}


Related Solutions

I am using NetBeans IDE Java to code and I am seeking comment to the code...
I am using NetBeans IDE Java to code and I am seeking comment to the code as well (for better understanding). Magic squares. An n x n matrix that is filled with the numbers 1, 2, 3, …, n2 is a magic square if the sum of the elements in each row, in each column, and in the two diagonals is the same value. Write a program that reads in 16 values from the keyboard, displays them in a 4...
This much like the single linked list assignment. I am giving you the majority of the...
This much like the single linked list assignment. I am giving you the majority of the code and left a couple of functions for you to complete. I had intended to try to do some video clips of my own lecture but I am not going to have time to get those completed (at least not at a quality I want). You might take a look at these sites for more help outside just zyBooks. #include <stdio.h> #include <stdlib.h> struct...
I am trying to write a python code to create a function that returns all possible...
I am trying to write a python code to create a function that returns all possible dimensions (reshaping) for an input matrix, can someone explains the math and write a python code with explaining all the steps and give me a test cases, and please explain if there is more than one approach. so if we have 1*8 matrix we should return 1*8, 2*4,4*2,8*1 def reshaping(M) should return a n ORDERED list AND the all possible reshaped matrices
I am given this starter code and I am suppose to debug it and all of...
I am given this starter code and I am suppose to debug it and all of that and it will not work after I change ">" to "<=" and then i'm not sure after that what else I have to do. Could someone help me solve this? // Start with a penny // double it every day // how much do you have in a 30-day month? public class DebugSix1 {    public static void main(String args[])    {      ...
Develop the following code for quiz (i am getting some errors)in python in such a manner...
Develop the following code for quiz (i am getting some errors)in python in such a manner that it shoulde be having extra 3 attempts(for wrong answrs) for all questions if the user entered wrong answer · for ex: If the user entered correct answer for first question #then 3 attempts will be carried to next questions. If the user entered 3 times wrong answer in 1st question itself means it sholud display as no more attempts and you got o...
I am struggling with this java code ublic class HW2_Q4 {    public static void main(String[]...
I am struggling with this java code ublic class HW2_Q4 {    public static void main(String[] args) {        // Define the input string. Note that I could have written it all on a        // single line, but I broke it up to match the question.        String input = "Book , Cost , Number\n"                    + "Hamlet , 24.95 , 10\n"                    + "King Lear , 18.42...
C++ Problem. I am providing the code. Just Please provide the new function and highlight it....
C++ Problem. I am providing the code. Just Please provide the new function and highlight it. implement the functions replaceAt, seqSearch, and remove. Test your new function too in main. Also, Test Old functions in main. Show the output. Also, modify the functions accordingly which have "See Programming Exercise 22". main.cpp : #include <iostream> using namespace std; #include "arrayListTypetempl.h" int main(){    arrayListType<int> intList;    arrayListType<char> charList;       intList.insertEnd(5);    intList.insertEnd(3);    intList.insertEnd(4);    intList.insertEnd(55);       charList.insertEnd('a');   ...
I am having an issue with the code. The issue I am having removing the part...
I am having an issue with the code. The issue I am having removing the part when it asks the tuter if he would like to do teach more. I want the program to stop when the tuter reaches 40 hours. I believe the issue I am having is coming from the driver. Source Code: Person .java File: public abstract class Person { private String name; /** * Constructor * @param name */ public Person(String name) { super(); this.name =...
I am trying to make a robot move (theoretically), some of my code does not work...
I am trying to make a robot move (theoretically), some of my code does not work in python. I am getting some errors. How can I fix it? This is my code: #!/usr/bin/env python.3 import time import sys import random #Provide a Menu for the User def main(): print("To control LED press 1") print("To drive press 2") print("To change motor speed press 3") print("To control the Servo press 4") while True: option = int(input("Enter the numbers 1, 2, 3, or...
The nurse calls the physician using the SBAR communication approach. The nurse states, I am calling...
The nurse calls the physician using the SBAR communication approach. The nurse states, I am calling about the patient Mrs. Smith, on Two east, University Hospital. I am concerned because her blood pressure is 210/98 and she is complaining of a headache. She was admitted yesterday and was alert, but becoming lethargic, will follow commands and respond appropriately. She is pale, extremities are cool. O2 saturations is 92% room air. She had atenolol one hour ago with no change in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT