Question

In: Computer Science

C++ problem - Implement and add the advanced functionalities to the ADT of the BST made...

C++ problem - Implement and add the advanced functionalities to the ADT of the BST made with the fundamental functionalities:

Visit - Description: It will display each of the data stored in the BST depending on the input parameter:Preorder
Inorder
Bidder
Level by level

Input - An integer (1-4)
Exit - Nothing
Precondition - A valid BST
Postcondition - Nothing

Height - Description:Will return the height of the BST
Entry - Nothing
Output - An integer with which to indicate the height of the BST
Precondition - A valid BST
Postcondition - Nothing

Ancestors - Description: It will display the ancestors of a data

Entry - The data of which you want to know the ancestors
Exit - Nothing
Precondition - A valid BST
Postcondition - Nothing

whatlevelamI - Description: It will return an integer that indicates the level in which a data is located, or -1 in case it is not in the BST

Input - A piece of information to find your height
Output - Integer indicating the level of the data in the BST, or -1 if it is not found
Precondition - A valid BST
Postcondition - Nothing

This is the declaration of the MyBST class that must be used

#ifndef MYBST_H

#define MYBST_H

struct MyNodeBST{

    int data;

    MyNodeBST *left,

              *right;

    MyNodeBST(int data){

        this->data=data;

        this->left=this->right=nullptr;

    }

};

class MyBST{

    private:

        int size;

        MyNodeBST* root;

        bool search(int data,MyNodeBST* current);

        void preorder(MyNodeBST* current);

        void inorder(MyNodeBST* current);

        void postorder(MyNodeBST* current);

    public:

        MyBST();

        int length();

        bool isEmpty();

        bool search(int data);

        bool insert(int data);

        bool remove(int data);

        void preorder();

        void inorder();

        void postorder();

        void level();

        void visit(int type);

        int height();

        void ancestors(int data);

        int whatLevelAmI(int data);

  

};

#endif

Solutions

Expert Solution

  1.  * C++ Program To Implement BST
  2.  */
  3. # include <iostream>
  4. # include <cstdlib>
  5. using namespace std;
  6. /*
  7.  * Node Declaration
  8.  */
  9. struct node
  10. {
  11.     int info;
  12.     struct node *left;
  13.     struct node *right;
  14. }*root;
  15.  
  16. /*
  17.  * Class Declaration
  18.  */
  19. class BST
  20. {
  21.     public:
  22.         void find(int, node **, node **);    
  23.         void insert(node *, node *);
  24.         void del(int);
  25.         void case_a(node *,node *);
  26.         void case_b(node *,node *);
  27.         void case_c(node *,node *);
  28.         void preorder(node *);
  29.         void inorder(node *);
  30.         void postorder(node *);
  31.         void display(node *, int);
  32.         BST()
  33.         {
  34.             root = NULL;
  35.         }
  36. };
  37. /*
  38.  * Main Contains Menu
  39.  */
  40. int main()
  41. {
  42.     int choice, num;
  43.     BST bst;
  44.     node *temp;
  45.     while (1)
  46.     {
  47.         cout<<"-----------------"<<endl;
  48.         cout<<"Operations on BST"<<endl;
  49.         cout<<"-----------------"<<endl;
  50.         cout<<"1.Insert Element "<<endl;
  51.         cout<<"2.Delete Element "<<endl;
  52.         cout<<"3.Inorder Traversal"<<endl;
  53.         cout<<"4.Preorder Traversal"<<endl;
  54.         cout<<"5.Postorder Traversal"<<endl;
  55.         cout<<"6.Display"<<endl;
  56.         cout<<"7.Quit"<<endl;
  57.         cout<<"Enter your choice : ";
  58.         cin>>choice;
  59.         switch(choice)
  60.         {
  61.         case 1:
  62.             temp = new node;
  63.             cout<<"Enter the number to be inserted : ";
  64.             cin>>temp->info;
  65.             bst.insert(root, temp);
  66.         case 2:
  67.             if (root == NULL)
  68.             {
  69.                 cout<<"Tree is empty, nothing to delete"<<endl;
  70.                 continue;
  71.             }
  72.             cout<<"Enter the number to be deleted : ";
  73.             cin>>num;
  74.             bst.del(num);
  75.             break;
  76.         case 3:
  77.             cout<<"Inorder Traversal of BST:"<<endl;
  78.             bst.inorder(root);
  79.             cout<<endl;
  80.             break;
  81.         case 4:
  82.             cout<<"Preorder Traversal of BST:"<<endl;
  83.             bst.preorder(root);
  84.             cout<<endl;
  85.             break;
  86.         case 5:
  87.             cout<<"Postorder Traversal of BST:"<<endl;
  88.             bst.postorder(root);
  89.             cout<<endl;
  90.             break;
  91.         case 6:
  92.             cout<<"Display BST:"<<endl;
  93.             bst.display(root,1);
  94.             cout<<endl;
  95.             break;
  96.         case 7:
  97.             exit(1);
  98.         default:
  99.             cout<<"Wrong choice"<<endl;
  100.         }
  101.     }
  102. }
  103.  
  104. /*
  105.  * Find Element in the Tree
  106.  */
  107. void BST::find(int item, node **par, node **loc)
  108. {
  109.     node *ptr, *ptrsave;
  110.     if (root == NULL)
  111.     {
  112.         *loc = NULL;
  113.         *par = NULL;
  114.         return;
  115.     }
  116.     if (item == root->info)
  117.     {
  118.         *loc = root;
  119.         *par = NULL;
  120.         return;
  121.     }
  122.     if (item < root->info)
  123.         ptr = root->left;
  124.     else
  125.         ptr = root->right;
  126.     ptrsave = root;
  127.     while (ptr != NULL)
  128.     {
  129.         if (item == ptr->info)
  130.         {
  131.             *loc = ptr;
  132.             *par = ptrsave;
  133.             return;
  134.         }
  135.         ptrsave = ptr;
  136.         if (item < ptr->info)
  137.             ptr = ptr->left;
  138.         else
  139.             ptr = ptr->right;
  140.     }
  141.     *loc = NULL;
  142.     *par = ptrsave;
  143. }
  144.  
  145. /*
  146.  * Inserting Element into the Tree
  147.  */
  148. void BST::insert(node *tree, node *newnode)
  149. {
  150.     if (root == NULL)
  151.     {
  152.         root = new node;
  153.         root->info = newnode->info;
  154.         root->left = NULL;
  155.         root->right = NULL;
  156.         cout<<"Root Node is Added"<<endl;
  157.         return;
  158.     }
  159.     if (tree->info == newnode->info)
  160.     {
  161.         cout<<"Element already in the tree"<<endl;
  162.         return;
  163.     }
  164.     if (tree->info > newnode->info)
  165.     {
  166.         if (tree->left != NULL)
  167.         {
  168.             insert(tree->left, newnode);     
  169.         }
  170.         else
  171.         {
  172.             tree->left = newnode;
  173.             (tree->left)->left = NULL;
  174.             (tree->left)->right = NULL;
  175.             cout<<"Node Added To Left"<<endl;
  176.             return;
  177.         }
  178.     }
  179.     else
  180.     {
  181.         if (tree->right != NULL)
  182.         {
  183.             insert(tree->right, newnode);
  184.         }
  185.         else
  186.         {
  187.             tree->right = newnode;
  188.             (tree->right)->left = NULL;
  189.             (tree->right)->right = NULL;
  190.             cout<<"Node Added To Right"<<endl;
  191.             return;
  192.         }       
  193.     }
  194. }
  195.  
  196. /*
  197.  * Delete Element from the tree
  198.  */
  199. void BST::del(int item)
  200. {
  201.     node *parent, *location;
  202.     if (root == NULL)
  203.     {
  204.         cout<<"Tree empty"<<endl;
  205.         return;
  206.     }
  207.     find(item, &parent, &location);
  208.     if (location == NULL)
  209.     {
  210.         cout<<"Item not present in tree"<<endl;
  211.         return;
  212.     }
  213.     if (location->left == NULL && location->right == NULL)
  214.         case_a(parent, location);
  215.     if (location->left != NULL && location->right == NULL)
  216.         case_b(parent, location);
  217.     if (location->left == NULL && location->right != NULL)
  218.         case_b(parent, location);
  219.     if (location->left != NULL && location->right != NULL)
  220.         case_c(parent, location);
  221.     free(location);
  222. }
  223.  
  224. /*
  225.  * Case A
  226.  */
  227. void BST::case_a(node *par, node *loc )
  228. {
  229.     if (par == NULL)
  230.     {
  231.         root = NULL;
  232.     }
  233.     else
  234.     {
  235.         if (loc == par->left)
  236.             par->left = NULL;
  237.         else
  238.             par->right = NULL;
  239.     }
  240. }
  241.  
  242. /*
  243.  * Case B
  244.  */
  245. void BST::case_b(node *par, node *loc)
  246. {
  247.     node *child;
  248.     if (loc->left != NULL)
  249.         child = loc->left;
  250.     else
  251.         child = loc->right;
  252.     if (par == NULL)
  253.     {
  254.         root = child;
  255.     }
  256.     else
  257.     {
  258.         if (loc == par->left)
  259.             par->left = child;
  260.         else
  261.             par->right = child;
  262.     }
  263. }
  264.  
  265. /*
  266.  * Case C
  267.  */
  268. void BST::case_c(node *par, node *loc)
  269. {
  270.     node *ptr, *ptrsave, *suc, *parsuc;
  271.     ptrsave = loc;
  272.     ptr = loc->right;
  273.     while (ptr->left != NULL)
  274.     {
  275.         ptrsave = ptr;
  276.         ptr = ptr->left;
  277.     }
  278.     suc = ptr;
  279.     parsuc = ptrsave;
  280.     if (suc->left == NULL && suc->right == NULL)
  281.         case_a(parsuc, suc);
  282.     else
  283.         case_b(parsuc, suc);
  284.     if (par == NULL)
  285.     {
  286.         root = suc;
  287.     }
  288.     else
  289.     {
  290.         if (loc == par->left)
  291.             par->left = suc;
  292.         else
  293.             par->right = suc;
  294.     }
  295.     suc->left = loc->left;
  296.     suc->right = loc->right;
  297. }
  298.  
  299. /*
  300.  * Pre Order Traversal
  301.  */
  302. void BST::preorder(node *ptr)
  303. {
  304.     if (root == NULL)
  305.     {
  306.         cout<<"Tree is empty"<<endl;
  307.         return;
  308.     }
  309.     if (ptr != NULL)
  310.     {
  311.         cout<<ptr->info<<"  ";
  312.         preorder(ptr->left);
  313.         preorder(ptr->right);
  314.     }
  315. }
  316. /*
  317.  * In Order Traversal
  318.  */
  319. void BST::inorder(node *ptr)
  320. {
  321.     if (root == NULL)
  322.     {
  323.         cout<<"Tree is empty"<<endl;
  324.         return;
  325.     }
  326.     if (ptr != NULL)
  327.     {
  328.         inorder(ptr->left);
  329.         cout<<ptr->info<<"  ";
  330.         inorder(ptr->right);
  331.     }
  332. }
  333.  
  334. /*
  335.  * Postorder Traversal
  336.  */
  337. void BST::postorder(node *ptr)
  338. {
  339.     if (root == NULL)
  340.     {
  341.         cout<<"Tree is empty"<<endl;
  342.         return;
  343.     }
  344.     if (ptr != NULL)
  345.     {
  346.         postorder(ptr->left);
  347.         postorder(ptr->right);
  348.         cout<<ptr->info<<"  ";
  349.     }
  350. }
  351.  
  352. /*
  353.  * Display Tree Structure
  354.  */
  355. void BST::display(node *ptr, int level)
  356. {
  357.     int i;
  358.     if (ptr != NULL)
  359.     {
  360.         display(ptr->right, level+1);
  361.         cout<<endl;
  362.         if (ptr == root)
  363.             cout<<"Root->:  ";
  364.         else
  365.         {
  366.             for (i = 0;i < level;i++)
  367.                 cout<<"       ";
  368.         }
  369.         cout<<ptr->info;
  370.         display(ptr->left, level+1);
  371.     }
  372. }

Output:-


Related Solutions

In C++, Design and implement an ADT that represents a triangle. The data for the ADT...
In C++, Design and implement an ADT that represents a triangle. The data for the ADT should include the three sides of the triangle but could also include the triangle’s three angles. This data should be in the private section of the class that implements the ADT. Include at least two initialization operations: one that provides default values for the ADT’s data, and another that sets this data to client-supplied values. These operations are the class’s constructors. The ADT also...
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
The question is about C++ The main should test all functionalities of the playlist implement a...
The question is about C++ The main should test all functionalities of the playlist implement a linked list to store songs of a playlist. A song (which is your node) is going to include the following data: - int Song ID - string Song Title - string Author Title - double Length Don't forget to include the next pointer that will lead to the next song. Your playlist (which is your linkedlist) is going to include the following functionalities: -...
1. Implement the graph ADT using the adjacency list structure. 2. Implement the graph ADT using...
1. Implement the graph ADT using the adjacency list structure. 2. Implement the graph ADT using the adjacency matrix structure. LANGUAGE IS IN JAVA Comment for any questions Data structures and algorithms
This is C++ programming. Use separate compilation to implement a polynomial ADT that manipulates polynomials in...
This is C++ programming. Use separate compilation to implement a polynomial ADT that manipulates polynomials in a single variable x (e.g., p = 4 x^5 + 7 x^3 – x^2 + 9 ). For this problem, consider only polynomials whose exponents are non-negative integers. You are required to identify a proper data representation schema to store such polynomials and hide such data from external users of this ADT. Additionally, your ADT will at least include the following member functions: One...
C++ PROGRAMMING In the linked-list based bag implementation, we demonstrated the functionalities, such as, add, remove,...
C++ PROGRAMMING In the linked-list based bag implementation, we demonstrated the functionalities, such as, add, remove, and list. This assignment is to extend the functionalities of the bag with other operations average, min, and max, You need to extend the Bag class (under Wk 2, BagLinked_List.cpp) with the following methods: -int Bag::min( ), is to find minimum of items of the bag. -int Bag::max( ), is to find maximum of items of the bag -float Bag::ave( ), is to find...
Implement a function named printRange that, given the pointer to the root of a BST, a...
Implement a function named printRange that, given the pointer to the root of a BST, a low key value, and a high key value, prints in sorted order all records whose key values fall between the two given keys (inclusive). Function printRange should visit as few nodes in the BST as possible. Here is the start code (in Java 8) for this problem. Input Format Three lines. The first line includes the number of keys to be put in the...
Implement a Bag ADT using Dynamic Array structure as underlying data storage for Bag ADT. RESTRICTIONS:...
Implement a Bag ADT using Dynamic Array structure as underlying data storage for Bag ADT. RESTRICTIONS: Not allowed to use ANY built-in Python data structures and their methods. You must solve by importing the DynamicArray class and using class methods to write solution. Also not allowed to directly access any variables of the DynamicArray class (like self.size, self.capacity and self.data in part 1). All work must be done by only using class methods. Below is the Bag ADT starter code...
Add a clone method to the BST program. This method returns a deep copy of a...
Add a clone method to the BST program. This method returns a deep copy of a tree. Coding Language C++ Use this main method to test your code: int main() { Tree T; T.insert(50); T.insert(20); T.insert(80); T.insert(60); T.insert(90); T.display(); Tree T1 = T; Tree T2 = T.clone(); cout << "\n\nT1 and T2 before insert(999)\n=====================\n\n"; T1.inOrder(); T2.inOrder(); T.insert(999); cout << "\n\nT1 and T2 after insert(999)\n=====================\n\n"; T1.inOrder(); T2.inOrder(); system("pause"); // may need to remove this on some systems return 0; } #include...
3.1 Implement the stack ADT using array (4 marks) 3.1.1 Implement the pop() operation in the...
3.1 Implement the stack ADT using array 3.1.1 Implement the pop() operation in the stack (1 mark) Implement a stack class named Stack2540Array using array. The starter code is as follows. The instance variables and most operations are provided. You need to implement the pop operation. Make sure that your program checks whether the stack is empty in the pop operation. import java . io .*; import java . util .*; public class Stack2540Array { int CAPACITY = 128; int...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT