Question

In: Computer Science

Please comments this C++ code and show screenshots of the outputs main.cpp #include<iostream> #include<vector> #include<string> #include"BST.h"...

Please comments this C++ code and show screenshots of the outputs

main.cpp

#include<iostream>
#include<vector>
#include<string>
#include"BST.h"
#include"BST.cpp"
using namespace std;

std::vector<std::string> tokenize(char line[])
{
   std::vector<std::string> tok;
       std::string word = "";
       for (int i = 0; i < strlen(line); i++)
       {
           if (i == strlen(line) - 1)
           {
               word += line[i];
               tok.push_back(word);
               return tok;
           }
           else if (line[i] == ' ')
           {
               tok.push_back(word);
               word = "";
               continue;
           }
           word += line[i];
       }
       return tok;
}
int main()
{
   char line[100];
   cout << "Enter a line: ";
   cin.getline(line, sizeof(line));

   vector<string> tokens = tokenize(line);
   cout << "********** TOKENS **********" << endl;
   for (auto token : tokens)
   {
       cout << token << endl;
   }
   cout << "****************************" << endl << endl;

   BST<string> T1;
   for (auto token : tokens)
       T1.insert(token);

   cout << "************ T1 ************" << endl;
   T1.postOrder();
   cout << "****************************" << endl;
   cout << "T1 Leaf Nodes = " << T1.CLeaves() << endl;
   cout << "T1 Height = " << T1.height() << endl;
   cout << "****************************" << endl << endl;

   BST<string> T2;
   vector<string> T2x = T1.getPostOrder();
   for (auto token : T2x)
       T2.insert(token);

   cout << "************ T2 ************" << endl;
   T2.preOrder();
   cout << "****************************" << endl;
   cout << "T2 Leaf Nodes = " << T2.CLeaves() << endl;
   cout << "T2 Height = " << T2.height() << endl;
   cout << "****************************" << endl << endl;

   BST<string> T3;
   vector<string> T3x = T2.getPreOrder();
   for (auto token : T3x)
       T3.insert(token);

   cout << "************ T3 ************" << endl;
   T3.inOrder();
   cout << "****************************" << endl;
   cout << "T3 Leaf Nodes = " << T3.CLeaves() << endl;
   cout << "T3 Height = " << T3.height() << endl;
   cout << "****************************" << endl << endl;
   return 0;
}

BST.CPP

#include "BST.h"

template <typename gen>
void BST<gen>::insert(gen x)
{
   insert(root, x);
}

template <typename gen>
bool BST<gen>::remove(string value)
{
   return remove(NULL, root, value);
}

template <typename gen>
void BST<gen>::preOrder()
{
   preOrder(root);
}

template <typename gen>
void BST<gen>::postOrder()
{
   postOrder(root);
}

template <typename gen>
void BST<gen>::inOrder()
{
   inOrder(root);
}

template <typename gen>
vector<string> BST<gen>::getPreOrder()
{
   vector<string> temp;
   getPreOrder(root, temp);
   return temp;
}

template <typename gen>
vector<string> BST<gen>::getPostOrder()
{
   vector<string> temp;
   getPostOrder(root, temp);
   return temp;
}

template <typename gen>
vector<string> BST<gen>::getInOrder()
{
   vector<string> temp;
   getInOrder(root, temp);
   return temp;
}

template <typename gen>
int BST<gen>::CLeaves()
{
   int s = 0;
   CLeaves(root, s);
   return s;
}

template <typename gen>
int BST<gen>::height()
{
   return HeightL(root);
}

template <typename gen>
void BST<gen>::DestroyRecursive(BNode<gen>* node)
{
   if (node)
   {
       DestroyRecursive(node->LChild);
       DestroyRecursive(node->RChild);
       delete node;
   }
}

template <typename gen>
BST<gen>::~BST()
{
   DestroyRecursive(root);
   root = nullptr;
}

template <typename gen>
void BST<gen>::insert(BNode<gen>*& t, gen x) {
   if (!t) { t = new BNode<gen>; t->data = x; t->LChild = t->RChild = nullptr; }
   else if (x < t->data)
       insert(t->LChild, x);
   else
       insert(t->RChild, x);
}

template <typename gen>
void BST<gen>::preOrder(BNode<gen>* t) {
   if (t) {
       cout << t->data << "\n";
       preOrder(t->LChild);
       preOrder(t->RChild);
   }
}

template <typename gen>
void BST<gen>::postOrder(BNode<gen>* t) {
   if (t) {
       postOrder(t->LChild);
       postOrder(t->RChild);
       cout << t->data << "\n";
   }
}

template <typename gen>
void BST<gen>::inOrder(BNode<gen>* t) {
   if (t) {
       inOrder(t->LChild);
       cout << t->data << "\n";
       inOrder(t->RChild);
   }
}

template <typename gen>
void BST<gen>::getPreOrder(BNode<gen>* t, vector<string>& temp) {
   if (t) {
       temp.push_back(t->data);
       getPreOrder(t->LChild, temp);
       getPreOrder(t->RChild, temp);
   }
}

template <typename gen>
void BST<gen>::getPostOrder(BNode<gen>* t, vector<string>& temp) {
   if (t) {
       getPostOrder(t->LChild, temp);
       getPostOrder(t->RChild, temp);
       temp.push_back(t->data);
   }
}

template <typename gen>
void BST<gen>::getInOrder(BNode<gen>* t, vector<string>& temp) {
   if (t) {
       getInOrder(t->LChild, temp);
       temp.push_back(t->data);
       getInOrder(t->RChild, temp);
   }
}

template <typename gen>
int BST<gen>::HeightL(BNode<gen>* P) {
   if (P == NULL)
       return 0;
   else
   {
       /* compute the depth of each subtree */
       int lDepth = HeightL(P->LChild);
       int rDepth = HeightL(P->RChild);

       /* use the larger one */
       if (lDepth > rDepth)
           return(lDepth + 1);
       else return(rDepth + 1);
   }
}

template <typename gen>
void BST<gen>::CLeaves(BNode<gen>* P, int& s) {
   if (P) {
       if (IsLeaf(P))
           s++;
       CLeaves(P->LChild, s);
       CLeaves(P->RChild, s);
   }
}

BST.h

#pragma once
#include<iostream>
#include<vector>
#include<Windows.h>

using namespace std;

template <typename gen>
struct BNode
{
   gen data;
   BNode<gen>* RChild;
   BNode<gen>* LChild;
   BNode()
   {
       RChild = nullptr;
       LChild = nullptr;
   }
};

template <class gen>
class BST
{
public:

   BST()
   {
       root = nullptr;
   }
   void insert(gen x);
   bool remove(string value);
   void preOrder();
   void postOrder();
   void inOrder();
   vector<string> getPreOrder();
   vector<string> getPostOrder();
   vector<string> getInOrder();
   int CLeaves();
   int height();
   void DestroyRecursive(BNode<gen>* node);
   ~BST();

private:

   BNode<gen>* root;

   void insert(BNode<gen>*& t, gen x);
   void preOrder(BNode<gen>* t);
   void postOrder(BNode<gen>* t);
   void inOrder(BNode<gen>* t);
   void getPreOrder(BNode<gen>* t, vector<string>& temp);
   void getPostOrder(BNode<gen>* t, vector<string>& temp);
   void getInOrder(BNode<gen>* t, vector<string>& temp);
   int HeightL(BNode<gen>* P);
   bool IsLeaf(BNode<gen>* P)
   {
       return (!(P->LChild) && !(P->RChild));
   }
   void CLeaves(BNode<gen>* P, int& s);
};
text.file

Enter a line: this is a line and it is with spaces and without semicolons
********** TOKENS **********
this
is
a
line
and
it
is
with
spaces
and
without
semicolons
****************************

************ T1 ************
and
and
a
is
it
semicolons
spaces
line
is
without
with
this
****************************
T1 Leaf Nodes = 4
T1 Height = 5
****************************

************ T2 ************
and
a
and
is
it
is
semicolons
line
spaces
without
with
this
****************************
T2 Leaf Nodes = 4
T2 Height = 9
****************************

************ T3 ************
a
and
and
is
is
it
line
semicolons
spaces
this
with
without
****************************
T3 Leaf Nodes = 4
T3 Height = 9
****************************

Solutions

Expert Solution

main.cpp

#include<iostream>
#include<vector>
#include<string>
#include"BST.h"
#include"BST.cpp"
using namespace std;

std::vector<std::string> tokenize(char line[])
{
   std::vector<std::string> tok;
       std::string word = "";
       for (int i = 0; i < strlen(line); i++)
       {
           if (i == strlen(line) - 1)
           {
               word += line[i];
               tok.push_back(word);
               return tok;
           }
           else if (line[i] == ' ')
           {
               tok.push_back(word);
               word = "";
               continue;
           }
           word += line[i];
       }
       return tok;
}
int main()
{
   char line[100];
   cout << "Enter a line: ";
   cin.getline(line, sizeof(line));

   vector<string> tokens = tokenize(line);
   cout << "********** TOKENS **********" << endl;
   for (auto token : tokens)
   {
       cout << token << endl;
   }
   cout << "****************************" << endl << endl;

   BST<string> T1;
   for (auto token : tokens)
       T1.insert(token);

   cout << "************ T1 ************" << endl;
   T1.postOrder();
   cout << "****************************" << endl;
   cout << "T1 Leaf Nodes = " << T1.CLeaves() << endl;
   cout << "T1 Height = " << T1.height() << endl;
   cout << "****************************" << endl << endl;

   BST<string> T2;
   vector<string> T2x = T1.getPostOrder();
   for (auto token : T2x)
       T2.insert(token);

   cout << "************ T2 ************" << endl;
   T2.preOrder();
   cout << "****************************" << endl;
   cout << "T2 Leaf Nodes = " << T2.CLeaves() << endl;
   cout << "T2 Height = " << T2.height() << endl;
   cout << "****************************" << endl << endl;

   BST<string> T3;
   vector<string> T3x = T2.getPreOrder();
   for (auto token : T3x)
       T3.insert(token);

   cout << "************ T3 ************" << endl;
   T3.inOrder();
   cout << "****************************" << endl;
   cout << "T3 Leaf Nodes = " << T3.CLeaves() << endl;
   cout << "T3 Height = " << T3.height() << endl;
   cout << "****************************" << endl << endl;
   return 0;
}

BST.CPP

#include "BST.h"

template <typename gen>
void BST<gen>::insert(gen x)
{
   insert(root, x);
}

template <typename gen>
bool BST<gen>::remove(string value)
{
   return remove(NULL, root, value);
}

template <typename gen>
void BST<gen>::preOrder()
{
   preOrder(root);
}

template <typename gen>
void BST<gen>::postOrder()
{
   postOrder(root);
}

template <typename gen>
void BST<gen>::inOrder()
{
   inOrder(root);
}

template <typename gen>
vector<string> BST<gen>::getPreOrder()
{
   vector<string> temp;
   getPreOrder(root, temp);
   return temp;
}

template <typename gen>
vector<string> BST<gen>::getPostOrder()
{
   vector<string> temp;
   getPostOrder(root, temp);
   return temp;
}

template <typename gen>
vector<string> BST<gen>::getInOrder()
{
   vector<string> temp;
   getInOrder(root, temp);
   return temp;
}

template <typename gen>
int BST<gen>::CLeaves()
{
   int s = 0;
   CLeaves(root, s);
   return s;
}

template <typename gen>
int BST<gen>::height()
{
   return HeightL(root);
}

template <typename gen>
void BST<gen>::DestroyRecursive(BNode<gen>* node)
{
   if (node)
   {
       DestroyRecursive(node->LChild);
       DestroyRecursive(node->RChild);
       delete node;
   }
}

template <typename gen>
BST<gen>::~BST()
{
   DestroyRecursive(root);
   root = nullptr;
}

template <typename gen>
void BST<gen>::insert(BNode<gen>*& t, gen x) {
   if (!t) { t = new BNode<gen>; t->data = x; t->LChild = t->RChild = nullptr; }
   else if (x < t->data)
       insert(t->LChild, x);
   else
       insert(t->RChild, x);
}

template <typename gen>
void BST<gen>::preOrder(BNode<gen>* t) {
   if (t) {
       cout << t->data << "\n";
       preOrder(t->LChild);
       preOrder(t->RChild);
   }
}

template <typename gen>
void BST<gen>::postOrder(BNode<gen>* t) {
   if (t) {
       postOrder(t->LChild);
       postOrder(t->RChild);
       cout << t->data << "\n";
   }
}

template <typename gen>
void BST<gen>::inOrder(BNode<gen>* t) {
   if (t) {
       inOrder(t->LChild);
       cout << t->data << "\n";
       inOrder(t->RChild);
   }
}

template <typename gen>
void BST<gen>::getPreOrder(BNode<gen>* t, vector<string>& temp) {
   if (t) {
       temp.push_back(t->data);
       getPreOrder(t->LChild, temp);
       getPreOrder(t->RChild, temp);
   }
}

template <typename gen>
void BST<gen>::getPostOrder(BNode<gen>* t, vector<string>& temp) {
   if (t) {
       getPostOrder(t->LChild, temp);
       getPostOrder(t->RChild, temp);
       temp.push_back(t->data);
   }
}

template <typename gen>
void BST<gen>::getInOrder(BNode<gen>* t, vector<string>& temp) {
   if (t) {
       getInOrder(t->LChild, temp);
       temp.push_back(t->data);
       getInOrder(t->RChild, temp);
   }
}

template <typename gen>
int BST<gen>::HeightL(BNode<gen>* P) {
   if (P == NULL)
       return 0;
   else
   {
       /* compute the depth of each subtree */
       int lDepth = HeightL(P->LChild);
       int rDepth = HeightL(P->RChild);

       /* use the larger one */
       if (lDepth > rDepth)
           return(lDepth + 1);
       else return(rDepth + 1);
   }
}

template <typename gen>
void BST<gen>::CLeaves(BNode<gen>* P, int& s) {
   if (P) {
       if (IsLeaf(P))
           s++;
       CLeaves(P->LChild, s);
       CLeaves(P->RChild, s);
   }
}

BST.h

#pragma once
#include<iostream>
#include<vector>
#include<Windows.h>

using namespace std;

template <typename gen>
struct BNode
{
   gen data;
   BNode<gen>* RChild;
   BNode

template <typename gen>
struct BNode
{
   gen data;
   BNode<gen>* RChild;
   BNode

template <typename gen2>
struct BNode
{
   gen data;
   BNode<gen>* RChild;
   BNode

template <typename gen1>
struct BNode
{
   gen data;
   BNode<gen>* RChild;
   BNode
x=3


Related Solutions

OPERATING SYSTEMS HOMEWORK: PLEASE CODE IN JAVA with comments & POST SCREENSHOTS OF OUTPUTS SCAN This...
OPERATING SYSTEMS HOMEWORK: PLEASE CODE IN JAVA with comments & POST SCREENSHOTS OF OUTPUTS SCAN This algorithm is performed by moving the R/W head back-and-forth to the innermost and outermost track. As it scans the tracks from end to end, it process all the requests found in the direction it is headed. This will ensure that all track requests, whether in the outermost, middle or innermost location, will be traversed by the access arm thereby finding all the requests. This...
C++ code Why my code is not compiling? :( #include <iostream> #include <iomanip> #include <string> using...
C++ code Why my code is not compiling? :( #include <iostream> #include <iomanip> #include <string> using namespace std; const int CWIDTH = 26; int main() {    int choice;    double convertFoC, converCtoF;    double starting, endvalue, incrementvalue;    const int CWIDTH = 13;    do {       cin >> choice;    switch (choice)    {        cin >> starting;    if (starting == 28){       cout << "Invalid range. Try again.";    }    while (!(cin >> starting)){       string  garbage;       cin.clear();       getline(cin, garbage);       cout << "Invalid data Type, must be a number....
Please show fully functioning Java code and screenshots of outputs. Please separate by 1a and 1b....
Please show fully functioning Java code and screenshots of outputs. Please separate by 1a and 1b. #1. Design a Java JProduct class for a product which implements both cloneable and comparable interfaces The class should have the following private member variables: m_id: an integer that holds the product ID m_name: a string that holds the product name m_wholesaleprice: a double that holds the wholesale price m_retailers: a String array that holds all retailers who sell the product and the class...
Complete the following TODO: parts of the code in C++ #include <iostream> #include <string> #include <limits>...
Complete the following TODO: parts of the code in C++ #include <iostream> #include <string> #include <limits> #include <vector> using namespace std; // // CLASS: NODE // class Node{ public: int value = 0; // our node holds an integer Node *next = nullptr; // our node has a pointer to the next Node Node(int i){ // contructor for our Node class value = i; // store a copy of argument "i" in "value" next = nullptr; // be sure next...
C++ CODE ONLY Using the following code. #include <iostream> #include <string> #include <climits> #include <algorithm> using...
C++ CODE ONLY Using the following code. #include <iostream> #include <string> #include <climits> #include <algorithm> using namespace std; // M x N matrix #define M 5 #define N 5 // Naive recursive function to find the minimum cost to reach // cell (m, n) from cell (0, 0) int findMinCost(int cost[M][N], int m, int n) {    // base case    if (n == 0 || m == 0)        return INT_MAX;    // if we're at first cell...
C++ finish the AVL Tree code: #include "AVLNode.h" #include "AVLTree.h" #include <iostream> #include <string> using namespace...
C++ finish the AVL Tree code: #include "AVLNode.h" #include "AVLTree.h" #include <iostream> #include <string> using namespace std; AVLTree::AVLTree() { root = NULL; } AVLTree::~AVLTree() { delete root; root = NULL; } // insert finds a position for x in the tree and places it there, rebalancing // as necessary. void AVLTree::insert(const string& x) { // YOUR IMPLEMENTATION GOES HERE } // remove finds x's position in the tree and removes it, rebalancing as // necessary. void AVLTree::remove(const string& x) {...
C++ Download the attached program and complete the functions. (Refer to comments) main.cpp ~ #include #include...
C++ Download the attached program and complete the functions. (Refer to comments) main.cpp ~ #include #include #define END_OF_LIST -999 using namespace std; /* * */ int exercise_1() { int x = 100; int *ptr; // Assign the pointer variable, ptr, to the address of x. Then print out // the 'value' of x and the 'address' of x. (See Program 10-2) return 0; } int exercise_2() { int x = 100; int *ptr; // Assign ptr to the address of...
Data Structure using C++ Complete and test the Sudoku problem main.cpp #include <iostream> #include <cmath> #include...
Data Structure using C++ Complete and test the Sudoku problem main.cpp #include <iostream> #include <cmath> #include "sudoku.h" using namespace std; int main() { cout << "See Programming Exercise 18." << endl; } sudoku.cpp #include <iostream> #include "sudoku.h" using namespace std; sudoku::sudoku() { cout << "Write the definition. ." << endl; } sudoku::sudoku(int g[][9]) { cout << "Write the definition." << endl; } void sudoku::initializeSudokuGrid() { cout << "Write the definition." << endl; } void sudoku::initializeSudokuGrid(int g[][9]) { cout << "Write...
For C++ Consider the following code defining classes for assets and office equipment: #include<string> #include<iostream> using...
For C++ Consider the following code defining classes for assets and office equipment: #include<string> #include<iostream> using namespace std; // class definition for asset class Asset{ protected: int value; // value of asset in cents public: Asset(int value); // constructor int get_value(); // get the value }; Asset::Asset(int val){ // implementation of constructor value=val; } int Asset::get_value(){ // implementation of get_value return value; } // abstract class for office equipment class OfficeEquipment:public Asset{ public: OfficeEquipment(int val); // constructor virtual void use_item()...
C++ Given Code: #include <iostream> #include <string> using namespace std; int main() { //declare variables to...
C++ Given Code: #include <iostream> #include <string> using namespace std; int main() { //declare variables to store user input bool cont = true; //implement a loop so that it will continue asking until the user provides a positive integer // the following provides ONLY part of the loop body, which you should complete { cout <<"How many words are in your message? \n"; cout <<"Enter value: "; // get user input integer here    cout <<"\nInvalid value. Please Re-enter a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT