Question

In: Computer Science

Develop a BST data type that supports: insert, search, remove and then Create a visualization for...

Develop a BST data type that supports: insert, search, remove and then Create a visualization for the BST data type you developed

Solutions

Expert Solution

Hope you will like the answer if you have any query ask me in comment section I will reply your query as soon as possible and please upvote me for the efforts given to this answer

#include <bits/stdc++.h>
using namespace std;

class Node{
  public:
    int data;
    Node *left;
    Node *right;
  
  public:
    Node(int x){
      data = x;
      left = NULL;
      right = NULL;
    }

};

Node *insert_BST(Node *tree, int val)
{
  if(tree == NULL){
    cout<<val <<" is inserted into BST successfully"<<endl;
    return (new Node(val));
  }
  if(tree->data >= val)
    tree->left = insert_BST(tree->left, val);
  else
    tree->right = insert_BST(tree->right, val);

  return tree;
}

int search_BST(Node *tree, int val)
{
  if(tree == NULL){
    cout<<val<<" is not present in the tree\n";  
    return 0;
  }

  string ret_val;
  if(tree->data == val)
    cout<<val<<" is present in the tree\n";  
  else if(tree->data > val)
    search_BST(tree->left, val);
  else
     search_BST(tree->right, val);

  return 0;

}


Node* delete_BST(Node *tree, int val)
{
  if(tree == NULL){
    cout<<"value is not present in BST"<<endl;
    return tree;
  }

  if(tree->data > val)
    tree->left = delete_BST(tree->left, val);
  else if(tree->data < val)
    tree->right = delete_BST(tree->right, val);

  else{
    //if left child of the node is empty
    if(tree->left == NULL){
      Node *temp = tree->right;
      free(tree);
      tree= temp;
    }
    //if right child of the node is empty
    else if(tree->right == NULL){
      Node *temp = tree->left;
      tree = temp;
      free(temp);
    }
    //if both left and right child exists for the node
    else{
      Node *temp = tree->left;
      while(temp->right->right!=NULL){   // traverse until you don't reach, the second last right node of the left child of node to be deleted 
        temp = temp->right;
      }
      tree->data = temp->right->data;       // update the value to be deleted with the value of the rightmost node 
      Node *temp2 = temp->right->left;      // pointer to the left child of the last right node
      free(temp->right);                    // delete the last node
      temp->right = temp2;                  // assign the left child of last right node to second last right node
    }

  }
  return tree;
}

void inorder(Node *tree) 
{ 
    if (tree != NULL) 
    { 
        inorder(tree->left); 
        cout<<tree->data<<" "; 
        inorder(tree->right); 
    }
}

int main(){

  Node *tree;
  
  tree = new Node(10);

  tree->left = new Node(5);
  tree->left->left = new Node(2);
  tree->left->right = new Node(8);
  tree->left->left->left = new Node(1);
  tree->left->left->right = new Node(4);
  tree->left->right->left = new Node(7);

  tree->right = new Node(17);
  tree->right->left = new Node(16);
  tree->right->right = new Node(18);


  insert_BST(tree, 9);
  cout<<"inorder traversal of the current tree is: ";
  inorder(tree);
  cout<<endl;

  search_BST(tree,9);

  tree = delete_BST(tree,5);
  cout<<"deleted 5 successfully \n";
  search_BST(tree,5);
  cout<<"inorder traversal of the current tree is: ";
  inorder(tree);
  cout<<endl;

  return 0;
}

Visualization

So let see in main function so we are inserting the elements in the tree the first element we insert is 5,28,1,4,7,17,16,18,9

So bst will look like this

After that we are inserting the element 9 in the above binary tree so the binary tree will be look like

After we are using the search function which tell the function is present in the tree or not after searching the node search(9) is present in tree

then after we print the tree


Related Solutions

Show how to augment the ordinary Binary Search Tree (BST) data structure so that it supports...
Show how to augment the ordinary Binary Search Tree (BST) data structure so that it supports an efficient procedure which, on input (x, k) where x is the root of a BST and k an integer, output the k-th smallest number store in the BST. Let n denote the total number of elements stored in the BST, what is the running time of your efficient procedure? How does your modification of the BST data structure affect the performance of other...
/** * 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...
I want to create a d3 map visualization with timeseries data which pulls data from a...
I want to create a d3 map visualization with timeseries data which pulls data from a mysql table. The map will change data every second which would jump from data which contain x datetime to data which contain x+1 datetime (for example January 1 data to January 2). How can I do this? I already know how to set up a Flask API using Python which connects my visual to the database. I just need the SQL Query to change...
/** * Interface for a set data type that supports adding, removing and contains operations. *...
/** * Interface for a set data type that supports adding, removing and contains operations. * @param <T> the type parameter for the elements of the set */ interface SetADT<T> { /** * Add a new element to the set. * @param el the new element to add to the set * @return true if the element has been added to the set, false if it was already in the set * @throws IllegalArgumentException if the new element passed to...
A deque (short for double-ended queue, but pronounced “deck”) is an abstract data type that supports...
A deque (short for double-ended queue, but pronounced “deck”) is an abstract data type that supports adding and removing at both the front and back. So, at a bare minimum, a deque has four operations: addFront(), removeFront(), addBack(), removeBack(). Suppose you have a deque D containing the numbers (1, 2, 3, 4, 5, 6, 7, 8), in this order. Suppose further that you have an initially empty queue Q. Give pseudo-code description of a method that uses only D and...
Create three MySQL database tables and write SQL scripts to read, insert, and delete data. The...
Create three MySQL database tables and write SQL scripts to read, insert, and delete data. The first database table will contain the names of at least four movies. The second table will be a list of actors who appear in the movies. The third table will be an associative table that describes the relationship between the actors and their movies (which actors appear in which movies). Actors and movies have a “many-to-many relationship,” meaning an actor can be in multiple...
Create three MySQL database tables and write SQL scripts to read, insert, and delete data. The...
Create three MySQL database tables and write SQL scripts to read, insert, and delete data. The first database table will contain the names of at least four movies. The second table will be a list of actors who appear in the movies. The third table will be an associative table that describes the relationship between the actors and their movies (which actors appear in which movies). Actors and movies have a “many-to-many relationship,” meaning an actor can be in multiple...
The code to create a Search/Filter Data with Javascript or html from html page.
The code to create a Search/Filter Data with Javascript or html from html page.
Create C# code that can search a text file and output the data at the line...
Create C# code that can search a text file and output the data at the line number inputted and amount of entries needed. Example of call in command window: Search16s filename.txt 273   10 Where 273 is the line number to start the output from, and 10 is the number of sequences that the program should output. The number of sequences entered on call should always be a odd number or give an error in console. The output should also display...
Create a Binary Search Tree for the following data and do In-order, Preorder and Post-order traversal...
Create a Binary Search Tree for the following data and do In-order, Preorder and Post-order traversal of the tree. 50, 60, 25, 40, 30, 70, 35, 10, 55, 65, 5 Write an algorithm to delete a node in Singly Linked List                            [12 Write an algorithm of Binary Search                                                              [10] Write a program in ‘C’ to generate Fibonacci series using recursion            [8]
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT