c++ - DFS Recursion of a Tree, need assistance with visited issue -


i'm having issues when trying navigate tree contains cycles. code enters infinite loop , core dumps. problem code isnt setting nodes visited after visiting them loops forever. appreciated.

#include "graphcode.h" //#define ebug  /****************************************************************************** static const string tag = "graphcode: ";  /******************************************************************************  * constructor **/ graphcode::graphcode() { }  /******************************************************************************  * destructor **/ graphcode::~graphcode() { }  /******************************************************************************  * accessors , mutators **/  /******************************************************************************  * general functions. **/  /******************************************************************************  * function 'creategraph'.  * read data input stream , create graph.  *  * parameter: **/ void graphcode::creategraph(scanner& instream) {   int nodecount = -10;   double connectivity = -3.5;    myrandom myrandom; #ifdef ebug #endif   cout << tag << "enter creategraph\n";     if (instream.hasnext())   {     nodecount = instream.nextint();     connectivity = instream.nextdouble();     cout << tag << "create graph of " << nodecount                 << " nodes , " << connectivity << " percent connections"                 << endl;   }   else   {     cout << tag << "no data\n";    }    // first create vector of empty nodes   (int fromnum = 0; fromnum < nodecount; ++fromnum)   {     node node = node(fromnum);     this->thegraph.push_back(node);   }    // have vector know can subscript   (int fromnum = 0; fromnum < nodecount; ++fromnum)   {     node node = this->thegraph.at(fromnum);     (int tonum = 0; tonum < nodecount; ++tonum)     {       if (fromnum == tonum) continue;       double r = myrandom.randomuniformdouble(0.0, 1.0);       if (r <= connectivity)       {         node.addchildsub(tonum);       }     }     this->thegraph[fromnum] = node;   }  #ifdef ebug #endif   cout << tag << "leave creategraph " << nodecount << " " << connectivity << endl;  } // void graphcode::creategraph(scanner& instream)  /******************************************************************************  * function 'descendfrom'. **/ void graphcode::descendfrom(ofstream& outstream, string blanks, node& parentnode) { #ifdef ebug   cout << blanks << tag << "enter descendfrom\n" << this->tostringpath(blanks) << endl; #endif    vector<int> listofchildren = parentnode.getchildsubs();   vector<int>::const_iterator iter;   if (parentnode.hasbeenvisited() == false)    {     path.push_back(utils::format(parentnode.getnodenumber()));     parentnode.setvisited(true);     if (listofchildren.empty())     {       outstream << "path""\n" << tostringpath(blanks) << endl;       path.pop_back();     }      else     {       (iter = listofchildren.begin(); iter != listofchildren.end(); iter++)       {         node& n = thegraph.at(*iter);         descendfrom(outstream, blanks, n);       }      path.pop_back();     }   }  #ifdef ebug   cout << blanks << tag << "leave descendfrom\n";  #endif  } // graphcode::descendfrom()  /******************************************************************************  * function 'dosearch'. **/ void graphcode::dosearch(ofstream& outstream) { #ifdef ebug   cout << tag << "enter dosearch\n";  #endif  node& node = thegraph.at(0); string blanks = "     "; descendfrom(outstream, blanks, node);   #ifdef ebug   cout << tag << "leave dosearch\n";  #endif  } // void graphcode::dosearch()  /******************************************************************************  * function 'readgraph'. **/ void graphcode::readgraph(scanner& instream) {   scanline scanline;  #ifdef ebug   cout << tag << "enter readgraph\n";  #endif    int firstnode, lastnode;   firstnode = instream.nextint();   lastnode = instream.nextint();   assert ( 0 == firstnode);   for(int = 0; <= lastnode; ++i)   {     node node = node(i);     this->thegraph.push_back(node);   }    while (instream.hasnext())   {     string theline = instream.nextline();      scanline.openstring(theline);     int parentnodenum = scanline.nextint();     node node = this->thegraph.at(parentnodenum);     while( scanline.hasnext())     {       int thechild = scanline.nextint();        node.addchildsub(thechild);     }     this->thegraph.at(parentnodenum) = node;   }  #ifdef ebug   cout << tag << "leave readgraph " << endl; #endif } // void graphcode::readgraph(scanner& instream)  /******************************************************************************  * function 'tostring'. **/ string graphcode::tostring() {   node node;   string s = "     "; #ifdef ebug   cout << tag << "enter tostring\n";  #endif   vector<node>::const_iterator iter;   (iter = thegraph.begin(); iter != thegraph.end(); iter++)   {     node = *iter;     s += "node:  (" + node.tostring() + ")" + "\n";   } #ifdef ebug   cout << tag << "leave tostring\n";  #endif   return s; } // string graphcode::tostring()  /******************************************************************************  * function 'tostringchildren'. **/ string graphcode::tostringchildren(string blanks, const vector<int>& children) {   string s = ""; #ifdef ebug   cout << tag << "enter tostringchildren\n";  #endif   node node;   vector<int>::const_iterator iter;   (iter = children.begin(); iter != children.end(); ++iter)   {      s += utils::format((*iter), 6);   } #ifdef ebug   cout << tag << "leave tostringchildren\n";  #endif   return s; } // string graphcode::tostringchildren()  /******************************************************************************  * function 'tostringpath'. **/ string graphcode::tostringpath(string blanks) {   string s = "     "; #ifdef ebug   cout << tag << "enter tostringpath\n";  #endif   node node;   (int index = 0; index < this->path.size(); ++index)   {     s += this->path.at(index) + "\n" + blanks;   }    s += "leaf\n"; #ifdef ebug   cout << tag << "leave tostringpath\n";  #endif   return s; } // string graphcode::tostringpath() 

//expected output:

//path leaf                             // path (   0:  t    1   2   6   9 xxx)                             //from (   1:  t  xxx)                            // leaf  //path leaf                                //path (   0:  t    1   2   6   9 xxx)                               //from (   2:  t    3   4   5 xxx)                               //from (   3:  t  xxx)                               //leaf  //my output:   //path    //  0    //  1    //  leaf  //path     // 0     // 2     // 3     // leaf 

i have mine printing out path correctly can't 't' (basically calling parentnode.tostring()) , children print on same line path

void node::setvisited(bool what) {   = visited;  } 

that assignment backwards. meant

visited = what; 

Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -