Question

In: Computer Science

In MobaXterm, with the use of the binary search tree. Implement four operations of insert, in-order...

In MobaXterm, with the use of the binary search tree. Implement four operations of insert, in-order traversal, preorder traversal, and find.

Please separate the code in the four parts and explain in detail what is happening. Also, if you can please basic C language. If not, then I understand. Thank you for your time.

The test cases are 'm', 'd', 'g', 'r', 'p', 'b', and 'x'.

Output:

Enter choice (lower case is also acceptable) --- (I)nsert, (F)ind, (Q)uit: i

Enter new node's key value: H

Enter string of up to 10 characters for 'H's data: Hello

  Pre-order traversal is: H
  Inorder traversal is: H

Enter choice (lower case is also acceptable) --- (I)nsert, (F)ind, (Q)uit: i

Enter new node's key value: W

Enter string of up to 10 characters for 'W's data: Wow

  Pre-order traversal is: H W
  Inorder traversal is: H W

Enter choice (lower case is also acceptable) --- (I)nsert, (F)ind, (Q)uit: i

Enter new node's key value: F

Enter string of up to 10 characters for 'F's data: Fine

  Pre-order traversal is: H F W
  Inorder traversal is: F H W

Enter choice (lower case is also acceptable) --- (I)nsert, (F)ind, (Q)uit: f

Enter the search key: H

  Found the string "Hello" there.

Enter choice (lower case is also acceptable) --- (I)nsert, (F)ind, (Q)uit: f

Enter the search key: x

  'x' is not in the tree


Enter choice (lower case is also acceptable) --- (I)nsert, (F)ind, (Q)uit: q

Solutions

Expert Solution

Please find the C code as following -

#include<stdio.h> 
#include<stdlib.h> 
   
struct node 
{ 
    int key; 
    char string[10];
    struct node *left, *right; 
}; 
   
// A utility function to create a new BST node 
struct node *newNode(int item,char string[]) 
{ 
    struct node *temp =  (struct node *)malloc(sizeof(struct node)); 
    temp->key = item; 
    for(int i=0; i<10;i++){
      temp->string[i] = string[i];
    }
    temp->left = temp->right = NULL; 
    return temp; 
} 
  
   
/* A utility function to insert a new node with given key in BST */
struct node* insert(struct node* node, int key, char string[]) 
{ 
    /* If the tree is empty, return a new node */
    if (node == NULL){
      return newNode(key,string); 
    }
  
    /* Otherwise, recur down the tree */
    if (key < node->key) 
        node->left  = insert(node->left, key,string); 
    else if (key > node->key) 
        node->right = insert(node->right, key,string);    
  
    /* return the (unchanged) node pointer */
    return node; 
} 
   
char* find(struct node *root, int key){
  if(root==NULL)
    return NULL;
  if(root->key == key){
    return root->string;
  }
  else if(key<root->key)
    return find(root->left,key);
  else
    return find(root->right,key);
}

// Driver Program to test above functions 
int main() 
{ 
    char choice='q',value;
    char key;
    
    struct node *root = NULL;
    while(1){
      char string[10];
      printf("\nEnter choice(lower cse is aslo acceptable)---(I)nsert,(F)ind,(Q)uit: ");
      scanf(" %c",&choice);
      if(choice=='i'||choice=='I'){
        printf("\nEnter new node's key value: ");
        //input key value of the node
        scanf(" %c",&value);
        printf("\nEnter String of up to 10 characters for ");printf(" %c's data: ",value);
        fflush(stdin);
        //input string for the node having 10 chars as maximum
        scanf("%10s",string);
        fflush(stdin);
        root = insert(root,value,string);
      }
      else if(choice=='f'||choice=='F'){
        printf("\nEnter the search key: ");
        scanf(" %c",&key);
        char *val = find(root,key);
        //if node carrying key to be searched found
        if(val!=NULL)
          printf("\nFound the string \"%s\" there.",val);
        //if node not found
        else
          printf("\n'%c' is not there.",key);
      }
      else 
        break;
    }
    return 0; 
} 

Sample output is as folllowing:-


Related Solutions

Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the...
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the template provided under the PolyLearn assignment, using the class TreeNode that is also provided. You may (should) implement helper methods that make your code easier to write, read, and understand. You will also need to write test cases of your own as you develop the methods. You may use iterative and/or recursive functions in your implementation. The following starter files are available . •...
/** * This class implements a basic Binary Search Tree that supports insert and get operations....
/** * This class implements a basic Binary Search Tree that supports insert and get operations. In * addition, the implementation supports traversing the tree in pre-, post-, and in-order. Each node * of the tree stores a key, value pair. * * @param <K> The key type for this tree. Instances of this type are used to look up key, value * pairs in the tree. * @param <V> The value type for this tree. An instance of this...
Beginning with an empty binary search tree, what binary search tree is formed when you insert...
Beginning with an empty binary search tree, what binary search tree is formed when you insert the following values in the given order – consider there alphabetical position for comparison. a. W, T, N, J, E, B, A b. W, T, N, A, B, E, J c. A, B, W, J, N, T, E d. B, T, E, A, N, W, J Alphabetical positions: A-1, B-2, E-5, J-10, N-14,T-20,W-23
Prerequisite Knowledge Understand binary search tree structure Understand binary search tree operations Understand binary search tree...
Prerequisite Knowledge Understand binary search tree structure Understand binary search tree operations Understand binary search tree worst case and best case time. Learning Outcomes Describe AVL tree structure Trace and implement AVL tree operations Explain and prove AVL tree performance
Binary Tree Create a binary search tree using the given numbers in the order they’re presented....
Binary Tree Create a binary search tree using the given numbers in the order they’re presented. State if the resulting tree is FULL and/or BALANCED. 37, 20, 18, 56, 40, 42, 12, 5, 6, 77, 20, 54
I am trying to implement a search function for a binary search tree. I am trying...
I am trying to implement a search function for a binary search tree. I am trying to get the output to print each element preceding the the target of the search. For example, in the code when I search for 19, the output should be "5-8-9-18-20-19" Please only modify the search function and also please walk me through what I did wrong. I am trying to figure this out. Here is my code: #include<iostream> using namespace std; class node {...
​Define a tree. Distinguish between a tree and a binary tree. Distinguish between a binary tree and a binary search tree.
Define a tree. Distinguish between a tree and a binary tree. Distinguish between a binary tree and a binary search tree.
c++ Binary Search Tree: find parent of a node please insert comment to explain what the...
c++ Binary Search Tree: find parent of a node please insert comment to explain what the code do. Use struct Node{ int data; Node* left; Node* right};
Implement a function to find a node in a binary search tree. Using the following class...
Implement a function to find a node in a binary search tree. Using the following class and function definition. If a node with a matching value is found, return a pointer to it. If no match is found, return nullptr. #include <iostream> class BTNode { public: int item; BTNode *left; BTNode *right; BTNode(int i, BTNode *l=nullptr, BTNode *r=nullptr):item(i),left(l),right(r){} }; BTNode *root = nullptr; BTNode *find(int item) { //implement code here return nullptr; } int main() {    root = new...
Implement a function to remove a leaf node from a binary search tree. Using the following...
Implement a function to remove a leaf node from a binary search tree. Using the following class and function definition: class BTNode { public: int item; BTNode *left; BTNode *right; BTNode(int i, BTNode *l=nullptr, BTNode *r=nullptr):item(i),left(l),right(r){} }; BTNode *root = nullptr; BTNode * remove_leaf(int item, bool &removed) { // implement } The remove function will return the node with the item if it's present in the tree. If the node is a leaf node and is removed by the function,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT