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...
Build a binary search tree with the following words. Insert them in an order so that...
Build a binary search tree with the following words. Insert them in an order so that the tree has as small a depth as possible. (Consider the insertion order of the words) Print the tree after,also, any, back, because, come, day, even, first, give, how, its, look, most, new, now, only, other, our, over, than, then, these, think, two, us, use, want, way, well, work. C++
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
Implement a function that returns all the items in a binary search tree in order inside...
Implement a function that returns all the items in a binary search tree in order inside a std::vector. Use 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; std::vector<int> inorder_traversal(BTNode *node) { // implement } If the BST has no values, return a vector with no items in it. #include <iostream> #include <vector> class BTNode { public: int item; BTNode *left; BTNode...
Write a JAVA program to modify the insert and remove of a binary search tree to...
Write a JAVA program to modify the insert and remove of a binary search tree to become an AVL tree.
Binary Search Trees with Lazy Deletion Implement binary search tree class with lazy deletion that has...
Binary Search Trees with Lazy Deletion Implement binary search tree class with lazy deletion that has TreeNode as nested class in Java. Design the class, TreeNode to have following class variables: int key; // All Keys are in the range 1 to 99 TreeNode leftChild; TreeNode rightChild; boolean deleted; Your program method must have routines to do the following operations. 1. insert //Should insert a new element to a leaf node. If new element is aduplicatethen do nothing. If the...
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
In C++, dealing with Binary Search trees. Implement search, insert, removeLeaf, and removeNodeWithOneChild methods in BST.h....
In C++, dealing with Binary Search trees. Implement search, insert, removeLeaf, and removeNodeWithOneChild methods in BST.h. BST.h code below. CPP code also provided for reference, nothing to be changed on cpp code. #include #include "BSTNode.h" using namespace std; #ifndef BST_H_ #define BST_H_ class BST { public: BSTNode *root; int size; BST() { root = NULL; size = 0; } ~BST() { if (root != NULL) deepClean(root); } BSTNode *search(int key) { // complete this method } BSTNode *insert(int val) {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT