Question

help please!!!

In this assignment, we are to make the provided testBST.cpp, an integer BST into a generic template BST to host the following data types:

  1. Integer,
  2. String,
  3. Frac, and
  4. FeetInches.
#include "Frac.h"
#include "FeetInches.h"
....

    
template<class T> void test(vector<T> list);

int main() {
    vector<int> intPattern {3,8,1,4,6,25,15,16};
    test(intPattern);

    vector<string> strPattern {
        "how", "many", "apples", "did", "you", "buy"};
    test(strPattern);
}

// test Driver
template<class T>
void test(vector<T> list) {
    BST<T> *b = new BST<T>;
    
    cout << "Insertion sequence for new tree: \n   ";
    bool first = true;
    for(auto x:list) {
        b->insert(x);
        if(first) first = false;
        else cout << ", ";
        cout << x;
    }

    cout << "\n Listing Tree nodes in order: \n   ";
    b->display();            
    cout << "\n Show the Original Tree diagram:\n";
    b->printInOrder();
    cout << "\n Check whether last inserted Node, (" 
    << list[list.size()-1] << ") exists? " 
    << ( (b->find(list[list.size()-1]))?"YES":"NO" ) 
    << "\n Remove fist inserted Node\n   ";
    b->display();
    cout << "\n Show the final result in Diagram:\n";
    b->printInOrder();
}

Test Run Example:

The example shown here contains only Integer and String BST created by combining the starters.  

Your completed work needs to show BST test on all four types: Integer, String, Frac and FeetInches.

Running /home/ubuntu/workspace/somsc200/Week14/testTemplateBST.cpp Insertion sequence for new tree 3, 8, 1, 4, 6, 25, 15, 16

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <vector>
using namespace std;

//lets implement a basic templated tree
template <class T>
class Node {
public:
   T data;
   Node *left, *right, *parent;

   Node() {
       left = right = parent = NULL;
   };

   Node(T &value) {
       data = value;
   };

   ~Node() {
   };
  
   void operator= (const Node<T> &other) {
       data = other.data;
   };

   bool operator< (T &other) {
       return (data < other);
   };
};

//Our tree will contain the Node defined above
template <class T>
class Tree {
public:
   Tree() {
       root = NULL;
   };

   //We will define these all as virtuals for inherited trees (like a Huffman Tree and AVL Tree shown later)
   virtual void insert(T &value) {
       insertNode(root,NULL,value);
   };
   virtual Node<T>* find(T &value) {
       return searchNode(root,value);
   };
   virtual bool remove(T &value) {
       return deleteNode(root,value);
   };
   virtual bool operator< (Tree<T> &other) {
       return (root->data < other.first()->data);
   };
   void operator= (Tree<T> &other) {
       equalNode(root,other.first());
   };
   Node<T>*& first() {
       return root;
   };
   virtual void display()
   {
       displayTree(root);
       cout<<endl;
   }
   virtual void printInorder()
   {
       displayTree(root);
       cout<<endl;
   }


protected:
   //this will be our root node and private functions
   Node<T> *root;
   void displayTree(Node<T> * ptr )
   {
       if(ptr)
   {
       displayTree(ptr->left);
       cout<<ptr->data<<" ";
       displayTree(ptr->right);
   }
   }
   void equalNode(Node<T>*& node, Node<T>* value) {
       if(value != NULL) {
           node = new Node<T>();
           *node = *value;
           if(value->left != NULL)
               equalNode(node->left, value->left);
           if(value->right != NULL)
               equalNode(node->right, value->right);
       }
   }
  
   void insertNode(Node<T> *&node, Node<T> *parent, T &value) {
       if(node == NULL) {
           node = new Node<T>();
           *node = value;
           node->parent = parent;
       } else if(value < node->data) {
           insertNode(node->left,node,value);
       } else
           insertNode(node->right,node,value);
   };
   Node<T>* searchNode(Node<T> *node, T &value) {
       if(node == NULL)
           return NULL;
       else if(value == node->data)
           return node;
       else if(value < node->data)
           return searchNode(node->left,value);
       else
           return searchNode(node->right,value);
   };
   Node<T>* FindMax(Node<T>* root)
{
if(root==NULL)
return NULL;

while(root->right != NULL)
{
root = root->right;
}
return root;
}
   Node<T>* deleteNode(Node<T>* root,T data)
{
if(root==NULL) return root;
else if(data<root->data)
root->left = deleteNode(root->left, data);
else if (data> root->data)
root->right = deleteNode(root->right, data);
else
{
//No child
if(root->right == NULL && root->left == NULL)
{
delete root;
root = NULL;   
}
//One child
else if(root->right == NULL)
{
Node<T>* temp = root;
root= root->left;
delete temp;
}
else if(root->left == NULL)
{
Node<T>* temp = root;
root= root->right;
delete temp;
}
//two child
else
{
Node<T>* temp = FindMax(root->left);
root->data = temp->data;
root->left = deleteNode(root->left, temp->data);
}
}
return root;
};

};
template<class T> void test(vector<T> list);

int main() {
vector<int> intPattern {3,8,1,4,6,25,15,16};
test(intPattern);
vector<string> strPattern {
"how", "many", "apples", "did", "you", "buy"};
test(strPattern);
}

// test Driver
template<class T>
void test(vector<T> list) {
Tree<T> *b = new Tree<T>;
  
cout << "Insertion sequence for new tree: \n ";
bool first = true;
for(auto x:list) {
b->insert(x);
if(first) first = false;
else cout << ", ";
cout << x;
}
cout << "\n Listing Tree nodes in order: \n ";
b->display();
cout << "\n Show the Original Tree diagram:\n";
b->printInorder();
cout << "\n Check whether last inserted Node, ("
<< list[list.size()-1] << ") exists? "
<< ( (b->find(list[list.size()-1]))?"YES":"NO" )
<< "\n Remove fist inserted Node\n ";
b->display();
cout << "\n Show the final result in Diagram:\n";
b->printInorder();
}

/* output:

Insertion sequence for new tree: 
   3, 8, 1, 4, 6, 25, 15, 16
 Listing Tree nodes in order: 
   1 3 4 6 8 15 16 25 

 Show the Original Tree diagram:
1 3 4 6 8 15 16 25 

 Check whether last inserted Node, (16) exists? YES
 Remove fist inserted Node
   1 3 4 6 8 15 16 25 

 Show the final result in Diagram:
1 3 4 6 8 15 16 25 
Insertion sequence for new tree: 
   how, many, apples, did, you, buy
 Listing Tree nodes in order: 
   apples buy did how many you 

 Show the Original Tree diagram:
apples buy did how many you 

 Check whether last inserted Node, (buy) exists? YES
 Remove fist inserted Node
   apples buy did how many you 

 Show the final result in Diagram:
apples buy did how many you 

*/

//Please provide header files for footinch and fact

Add a comment
Know the answer?
Add Answer to:
help please!!! In this assignment, we are to make the provided testBST.cpp, an integer BST into a generic template BST to host the following data types: Integer, String, Frac, and FeetInches. #inclu...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • True or false? (a) An insertion in an AVL tree with n nodes requires Θ (log(n))...

    True or false? (a) An insertion in an AVL tree with n nodes requires Θ (log(n)) rotations. (b) A set of numbers are inserted into an empty BST in sorted order and inserted into an empty AVL tree in random order. Listing all elements in sorted order from the BST is O (n), while listing them in sorted order from the AVL tree is O (log(n)). (c) If items are inserted into an empty BST in sorted order, then the...

  • Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to com...

    Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...

  • C++ Binary Search Tree question. I heed help with the level 2 question please, as level...

    C++ Binary Search Tree question. I heed help with the level 2 question please, as level 1 is already completed. I will rate the answer a 100% thumbs up. I really appreciate the help!. Thank you! searching.cpp #include <getopt.h> #include <iostream> #include <sstream> #include <stdlib.h> #include <unistd.h> using namespace std; // global variable for tree operations // use to control tree maintenance operations enum Mode { simple, randomised, avl } mode; // tree type // returns size of tree //...

  • C++ Binary Search Tree question. I heed help with the level 2 question please, as level...

    C++ Binary Search Tree question. I heed help with the level 2 question please, as level 1 is already completed. I will rate the answer a 100% thumbs up. I really appreciate the help!. Thank you! searching.cpp #include <getopt.h> #include <iostream> #include <sstream> #include <stdlib.h> #include <unistd.h> using namespace std; // global variable for tree operations // use to control tree maintenance operations enum Mode { simple, randomised, avl } mode; // tree type // returns size of tree //...

  • Template Deque Class (C++) In this assignment, we will use a given test menu for the template Deq...

    Template Deque Class (C++) In this assignment, we will use a given test menu for the template Deque Class (a Linked List based Double Ended Queue, Deque) that you have put together. Recommended Steps testDeque.cpp : // C++ implementation of doubly linked list Deque doubly linked list #include <bits/stdc++.h> using namespace std; class Timer { // To replace with the full timer class definition // inside this folder: LearnCpp9_18_timeSortArray.cpp }; // Node of a doubly linked list template<class T> class...

  • Can someone please help me with the following: Implement a Binary Search Tree that can be...

    Can someone please help me with the following: Implement a Binary Search Tree that can be used to store data values that are strings. The data values will be stored in nodes in a binary search tree. Each node will have a unique integer key. Provide functions to add, get and remove elements to/from the tree. Also provide functions to print the elements (both the key and the data values) in forward (dump) and reverse (dump_rev) order. To help in...

  • in c++ please include all of the following " template class, template function, singly linked list,...

    in c++ please include all of the following " template class, template function, singly linked list, the ADT stack, copy constructor, operator overloading, "try catch"and pointers Modify the code named "Stack using a Singly Linked List" to make the ADT Stack that is a template class has the code of the destructor in which each node is directly deleted without using any member function. As each node is deleted, the destructor displays the address of the node that is being...

  • %%%%% c++ assignment %%%%%% ///////// please test your program and check for errors. //////// you should...

    %%%%% c++ assignment %%%%%% ///////// please test your program and check for errors. //////// you should use the file bellow For this assignment you will write a program that creates a tree of "plants". Each plant is the result of an exeperiment. I have provided the main driver program that is responsible for running each of the experiments but you will need to create a tree for storing the results. The objective of this assignment is to learn how to...

  • Please create a class in Java that completes the following conditions MorseTree.java /* This program will...

    Please create a class in Java that completes the following conditions MorseTree.java /* This program will read in letters followed by their morse code * and insert them properly in a tree based on the amount of symbols * it has and whether they are left or right descendent. Finally * it prints a few certain nodes. */ import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class MorseTree {    public static void main(String[] args) throws FileNotFoundException {        //...

  • In this assignment you’ll implement a data structure called a trie, which is used to answer...

    In this assignment you’ll implement a data structure called a trie, which is used to answer queries regarding the characteristics of a text file (e.g., frequency of a given word). This write-up introduces the concept of a trie, specifies the API you’re expected to implement, and outlines submission instructions as well as the grading rubric. Please carefully read the entire write-up before you begin coding your submission. Tries A trie is an example of a tree data structure that compactly...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT