Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MAKE SURE THIS IS procedural code C++ Write a C++ program that creates a tree fo

ID: 3683213 • Letter: M

Question

MAKE SURE THIS IS procedural code C++

Write a C++ program that creates a tree for the following string data (stored in an input file – sample shown below), where:

1) the leftmost string goes into a node;

2) the middle string goes into the node’s left child; and,

3) the rightmost string goes into the node’s right child.

However, the string “null” in the middle position means the node has no left child, and the string “null” in the rightmost position means the node has no right child.

The first line contains the root node’s string in the leftmost position, and for every non-“null” string, there must be a line with that string in the leftmost position. Use the following data in the input file:

dog over brown

over red jumped

red The quick

The null null

quick null null

jumped fox null

fox null null

brown the lazy

the null null

lazy null null

This data produces the following tree:

Note that the string “The” is not the same as the string “the”.

It is recommended that you load the tree using preorder searches while reading the input file. After the tree is loaded your program should print out the result of a postorder traversal with a space between each string and a newline at the end.

It would also be helpful if you could add comments explaining whats going on! Thanks!

ALSO if code is CORRECT I will paypal you $5. Just give me your paypal email to send it to.

dog over brown red jumped the lazy The ul

Explanation / Answer

Nodes are printed level wise , _ indicates empty child

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <math.h>

using namespace std;

int main() {
   //change max index
   string tree[50];
   for (int i = 0; i < 50; i++) {
           tree[i] = ""; //indicates empyt node
   }

   string nodelines[50];
   for (int i = 0; i < 50; i++)
       nodelines[i] = "";

   //each node is reprsented by index
   //(0) - is root node
   //(1) left chidl, (2) - right child so on
   //(3) - left child of first level child-(1), (4) - right child of (2),
   //(5) - is left child of first level node - (2), (6) - right child of node(2)

   //if null then it indicates no child
   //read data line by line

   string line;
   ifstream myfile("D:/ravi/Cheg/treenodes.txt");

   string word;

   int i = 0;
   int j = 0;
   int nlines = 0;
   string nodes[3];

   int noOfnodes = 0;
   if (myfile.is_open()) {
       while (myfile.good()) {
           if (getline(myfile, line)) {

               if(line.empty())
                   continue;
               istringstream iss(line, istringstream::in);

               int count = 0;
               while (iss >> word && count < 3) {
               //   cout << word << " -- ";
                   nodes[count %3] = word;
                   count++;
               }
               //cout <<endl;
               if (nlines == 0) { //firstline
                   //root node
                   tree[i++]= nodes[0];
                   if (nodes[1].compare("null") != 0) {
                       tree[i++] = nodes[1];
                   }

                   if (nodes[2].compare("null") != 0) {
                       tree[i++] = nodes[2];
                   }

               } else { //other nodes
                   //find the root node in this line exist already in the tree
                   int k;
                   int level = 0;
                   int index = 0;
                   for(k = 0; k < 50; k++) {
                       //if found
                       if(tree[k].compare("") == 0)
                           continue;
                       if(tree[k].compare(nodes[0]) == 0) {
                           //find index
                           level = 0;
                           while(level < k) {
                               if(k >= pow(2,level) && k < pow(2,level+1)) {
                                   index = k + pow(2,level+1);
                                   break;
                               }
                               level++;
                           }
                           if (nodes[1].compare("null") != 0) {
                               //here index need to be computed
                               tree[index++] = nodes[1];
                           }

                           if (nodes[2].compare("null") != 0) {
                               tree[index++] = nodes[2];
                           }
                           break;
                       }
                   }

               }
               //cout << " Line no# " <<nlines <<"-------------------" <<endl;

               nlines++;
           }

       }
   }

   for (int i = 0; i < nlines * 3; i++) {
       if(tree[i].compare("") != 0)
           cout << tree[i] << " ";
       else
           cout << "_ " ;

   }
   return 0;
}


--output-----------

dog over brown red jumped _ the lazy quick _ _ _ fox _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _