Question

In: Computer Science

Can you complete the tasks inside the sample code. All the to-do task is inside the...

Can you complete the tasks inside the sample code. All the to-do task is inside the code commented out.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>


typedef struct TreeNode {
        int val; 
        struct TreeNode *left; 
        struct TreeNode *right; 
}node; 

/*Task 1 - Complete the function below,
  newNode() will return a tree node*/
node* newNode(int key){
        
}

/*Task 2 - Complete the function below to return
the size (number of elements stored) of a binary 
tree*/
int size(node* root){
         
 
}


/*Task 3 - Complete the function below to print 
a binary tree while performing an Preorder traversal.
Recall preorder traversal: 
        visit parent
        visit left subtree
        visit right subtree
you MUST use recursion  */

void printPreorder(node* r){
        
}


/*Task 4 - Complete the function below to print 
a binary tree while performing an Inorder traversal.
Recall inorder traversal: 
        visit left subtree
        visit parent
        visit right subtree
you MUST use recursion  */

void printInorder(node* r){
        
}



/*
  Task 5 - Complete the function below to clear a 
  binary tree. Perform a recursive traversal of a
  tree destroying all nodes.
*/

void clearTree(node* root){

}

/*
  Task 6 - Complete the function below to find and
  return the max depth of a binary tree. 
  Recall that max depth of a tree is the number of 
  nodes along the longest path from the root node 
  down to the farthest leaf node.
*/
int maxDepth (node* root){

}

/*
  Task 7 - Complete the function below to create 
  an ordered binary tree (bst). In a binary search 
  tree, for every node, all nodes in the left subtree
  are smaller while all nodes in the right subtree are
  larger. (You should compare the key with current value
  then decide whether to move to the left or right subtree)
*/

node* insertBST(node* root, int key) 
{ 
    
   
} 



int main(int argc, char const *argv[])
{

        node *root = newNode(5); 
        root->left = newNode(4);
        root->right = newNode(12); 
        root->right->left = newNode(13);
        root->right->right = newNode(2);
        root->left->left = newNode(8);


        printf("Size of the tree is %d\n",size(root));
        printf("Maximum depth of the tree is %d\n", maxDepth(root) );
        printPreorder(root); 
        printf("\n");
        printInorder(root); 
        printf("\n");

        node* bst; 
        int input ; 
        scanf("%d", &input); 
        while(input != -999){
                bst = insertBST(bst, input); 
                scanf("%d", &input); 
        }

        printInorder(bst);
        
        return 0;
}

Solutions

Expert Solution

Note: here all function done with comment after every logic applied for easy understanding still if you have any error feel free to ask me or post the issue.

Main.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>


typedef struct TreeNode {
        int val; 
        struct TreeNode *left; 
        struct TreeNode *right; 
}node; 

node* newNode(int key){
    
      // allocate memory to node neewNode using malloc 
        node *newNode = (node*) malloc(sizeof(node));
        newNode->val = key;
        newNode->left = NULL;
        newNode->right = NULL;
        
        return newNode;
}

int size(node* root){
    int count = 1;
    
    // if left is not null goto left node and increment count
    if (root->left != NULL) {
       count += size(root->left);
    }
    
    // if right is not null goto left node and increment count
    if (root->right != NULL) {
        count += size(root->right);
    }
    
    return count;
}

void printPreorder(node* r){
        if(r == NULL)
             return;
             
        printf("%d ",r->val);
        printPreorder(r->left);
        printPreorder(r->right);
}

void printInorder(node* r){
        if(r == NULL)
             return;
             
        printPreorder(r->left);
        printf("%d ",r->val);
        printPreorder(r->right);
}

void clearTree(node* root){
        if(root == NULL)
           return;
        clearTree(root->left);
        clearTree(root->right);
        
        // it will free each node memory of the tree and make it danglic pointer
        free(root);
}

int maxDepth (node* root){
     if(root == NULL)
             return 0;
    
    else { 
       // compute the depth of each subtree 
       int lDepth = maxDepth(root->left); 
       int rDepth = maxDepth(root->right); 
  
       // return  the max depth 
       if (lDepth > rDepth)  
           return(lDepth+1); 
       else return(rDepth+1); 
   }
}

node* insertBST(node* root, int key) 
{ 
   // If the tree is empty, return a new node 
    if (root == NULL) {
       return newNode(key); 
   }
  
    // if key is less than current node val
    if (key < root->val) 
        root->left  = insertBST(root->left, key); 
        
    // else if key is greater than current node val
    else if (key > root->val) 
        root->right = insertBST(root->right, key);    
  
    // return the (unchanged) node pointer 
    return root; 
} 



int main(int argc, char const *argv[])
{
        node *root = newNode(5); 
        root->left = newNode(4);
        root->right = newNode(12); 
        root->right->left = newNode(13);
        root->right->right = newNode(2);
        root->left->left = newNode(8);


        printf("Size of the tree is %d\n",size(root));
        printf("Maximum depth of the tree is %d\n", maxDepth(root) );
        printPreorder(root); 
        printf("\n");
        printInorder(root); 
        printf("\n");

       // intialise bst with NULL
        node* bst = NULL; 
        int input ; 
        printf("Enter key for making BST (if enter -999 stop)\n");
        scanf("%d", &input); 
        while(input != -999){
                bst = insertBST(bst, input); 
                scanf("%d", &input); 
        }

        printInorder(bst);
        
        return 0;
}

Related Solutions

java code: adds a new regular task, delete a task , show all tasks, and show...
java code: adds a new regular task, delete a task , show all tasks, and show regular tasks, mark a task as important (possibly through ID), complete task, show all completed tasks, show important tasks. I also need a UML diagram for the code update: The "task" is like something in a to do list. example) task 1. name: get carrots important: no completed: yes. you can run my code as an example if needed
For the set of tasks given below, do the following: Task Task Time(seconds) Immediate Predecessor A...
For the set of tasks given below, do the following: Task Task Time(seconds) Immediate Predecessor A 45 - B 11 A C 9 B D 50 - E 26 D F 11 E G 12 C H 10 C I 9 F, G, H J 10 I 193 b. Determine the minimum and maximum cycle times in seconds for a desired output of 500 units in a seven-hour day. (Round your answers to 1 decimal place.)    The minimum cycle...
Please write shell scripts in Kali Linux to complete the following tasks. Task A - Take...
Please write shell scripts in Kali Linux to complete the following tasks. Task A - Take your UIN Write a script like below that reads and displays the MIDAS ID if the following requirements are satisfied: Only lower case letter [a-z] and numeric character[0-9] are allowed MIDAS ID must between 4 to 8 digits Test your script with the following examples: Your MIDAS ID in lower case Your MIDAS ID w.one upper case A string less than 4 digits A...
I have to complete all //to do comments for the following code: /** * A ShoppingBasket...
I have to complete all //to do comments for the following code: /** * A ShoppingBasket holds zero or more Products and can provide information * about the Products. One can add Products to a ShoppingBasket during its * lifetime, reset the ShoppingBasket, create a copy which contains Products of * at least a certain value, and make various queries to the ShoppingBasket. * (Thus, the number of Products that will be stored by a ShoppingBasket object * is not...
IF YOU CAN NOT COMPLETE IT ALL DO NOT ANSWER THE QUESTIONS (THANKS) Develop a tentative...
IF YOU CAN NOT COMPLETE IT ALL DO NOT ANSWER THE QUESTIONS (THANKS) Develop a tentative plan of care for a client who has bipolar disorder. Include: -Nursing Diagnosis - Assessment Data (objective and subjective ) - Goal and outcome' - Nursing Intervention - Rationale - Outcome evaluation and replanning Please type your answer do not scan thanks
I need to complete this C++ program. The instructions are in the comments inside the code...
I need to complete this C++ program. The instructions are in the comments inside the code below: ------------------------------------------------------------------------- Original string is: this is a secret! Encypted string is: uijt!jt!b!tfdsfu" Decrypted string is: this is a secret! //Encoding program //Pre-_____? //other necessary stuff here int main() { //create a string to encrypt using a char array cout<< "Original string is: "<<string<<endl; encrypt(string); cout<< "Encrypted string is: "<<string<<endl; decrypt(string); cout<<"Decrypted string is: "<<string<<endl; return 0; } void encrypt(char e[]) { //Write implementation...
(In C) Note: Can you create an example code of these tasks. use any variables you...
(In C) Note: Can you create an example code of these tasks. use any variables you wish to use. postfix expressions: (each individual text (T), (F), (NOT), etc is a token) F T NOT T F NOT T T T AND F T F NAND (1) Create stack s. (2) For each token, x, in the postfix expression: If x is T or F push it into the stack s. (T = true, F = false) Else if x is...
Complete the code below by finishing up the tasks detailed in the comments. ArrayList<String> fruits =...
Complete the code below by finishing up the tasks detailed in the comments. ArrayList<String> fruits = new ArrayList<>(); Scanner sc = new Scanner(System.in); String fruit = "?"; while (!fruit.isEmpty()) { // prompt the user to enter a fruit // read the fruit using "sc", saving it into variable "fruit" // convert the fruit entered to lowercase // if the fruit is NOT empty and it is a new fruit, add it to ArrayList "fruits" } // display all fruits
All Tasks are commented in the code: public class MandelbrotUtil { private MandelbrotUtil() { } /**...
All Tasks are commented in the code: public class MandelbrotUtil { private MandelbrotUtil() { } /** * Return the number of iterations needed to determine if z(n + 1) = z(n) * z(n) + c * remains bounded where z(0) = 0 + 0i. z(n + 1) is bounded if its magnitude * is less than or equal to 2. Returns 1 if the magnitude of c * is greater than 2. Returns max if the magnitude * of z(n...
1. You can use any materials from the course to complete this task. 2. You are...
1. You can use any materials from the course to complete this task. 2. You are applying for a job and the posting is below: Position: Executive Administrative Assistant at Sinclair Community College. Requires high school diploma or equivalent and some college. Primary duties are customer service, professional writing, research, and basic computer skills. Apply by submitting a cover letter and resume to Natalie Rindler, Human Resource Generalist, 444 West Third Street, Dayton, OH 45402, no later than the posted...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT