Question
C++ Help I am trying to read from a binary tree that is from an external file properly and then print out the message in
C++ Help
I am trying to read from a binary tree that is from an external file properly and then print out the message in the correct order on a single line. However, my code ended up with bad runtimes, any debugging assistance will be appreciated.
--------------------------------------------------------------------------------------------------------------------------
This is what I have so far...
#include #include #include #include
using namespace std; class node { public: string data; node* left; node* right; }; // TODO: put your recursive functions here void do_preorder(node* root) { if (root != NULL) { // do something with root->data do_preorder(root->left); do_preorder(root->right); } } void print_preorder(node* root) { if (root != NULL) { cout << root->data << " "; print_preorder(root->left); print_preorder(root->right); } } // This program is simple enough to do the user interface in main()
int main() { int numLine = 0; string input_filename, text; vector a; node* root; cout << "What is the file you wish to read?" << endl; cin >> input_filename; ifstream fin(input_filename); if (!fin) { cout << "Error opening \"" << input_filename << "\" for reading." << endl; return -1; } while(!fin.eof()) { getline(fin, text); a.push_back(text); for (int i = 0; i < 94; i++) { root ->data = a[i]; count(root); if(a[i]=="LR"||a[i]=="R"||a[i]=="L"){ do_preorder(root); } if(i!=count(root)&&a[i]=="N"){continue;} else{break;} } }
// TODO: call your recursive function to read in the tree fin.close(); // TODO: call your recursive function to print out the tree return 0; }
--------------------------------------------------------------------------------------------------------------------------
This is the file we are trying to read from, named "a.txt" (and L, R, and LR are the branches for each string, N is no branches after the current string)...
Give LR We LR reprehend. LR a LR visions LR have LR have LR If R shadows L we N is LR Think LR offended, N all L and L this, L but N That LR mended, N you N these L but R While L here L slumber'd N appear. LR did N more LR this LR And N No L and LR weak N idle R theme, N but L yielding N not L Gentles, LR dream, N do N If LR honest LR we LR pardon, L you L If N as LR And, L will R mend. N I R an L am N Puck, N 'scape LR have LR we N to L unearned R luck R Now N serpent's LR the N tongue, N amends LR will R make N ere R liar LR Puck LR the L long; R Else N a N all. L night LR So, LR call. N good N unto R you N Robin LR if LR your LR me N hands, N be LR we N friends, R And N shall R restore R amends. N
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started