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>...

60.1K

Verified Solution

Question

Programming

Please comments this C++ code and show screenshots of theoutputs

main.cpp

#include
#include
#include
#include\"BST.h\"
#include\"BST.cpp\"
using namespace std;

std::vector tokenize(char line[])
{
   std::vector tok;
       std::string word = \"\";
       for (int i = 0; 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 tokens = tokenize(line);
   cout << \"********** TOKENS **********\" <   for (auto token : tokens)
   {
       cout << token <   }
   cout << \"****************************\" <

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

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

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

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

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

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

BST.CPP

#include \"BST.h\"

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

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

template
void BST::preOrder()
{
   preOrder(root);
}

template
void BST::postOrder()
{
   postOrder(root);
}

template
void BST::inOrder()
{
   inOrder(root);
}

template
vector BST::getPreOrder()
{
   vector temp;
   getPreOrder(root, temp);
   return temp;
}

template
vector BST::getPostOrder()
{
   vector temp;
   getPostOrder(root, temp);
   return temp;
}

template
vector BST::getInOrder()
{
   vector temp;
   getInOrder(root, temp);
   return temp;
}

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

template
int BST::height()
{
   return HeightL(root);
}

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

template
BST::~BST()
{
   DestroyRecursive(root);
   root = nullptr;
}

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

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

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

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

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

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

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

template
int BST::HeightL(BNode* P) {
   if (P == NULL)
       return 0;
   else
   {
       /* compute the depth of eachsubtree */
       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
void BST::CLeaves(BNode* P, int& s){
   if (P) {
       if (IsLeaf(P))
           s++;
       CLeaves(P->LChild, s);
       CLeaves(P->RChild, s);
   }
}

BST.h

#pragma once
#include
#include
#include

using namespace std;

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

template
class BST
{
public:

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

private:

   BNode* root;

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

Enter a line: this is a line and it is with spaces and withoutsemicolons
********** 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
****************************

Answer & Explanation Solved by verified expert
4.3 Ratings (853 Votes)
maincpp include include include includeBSTh includeBSTcpp    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students