title: DFS Recursion of a Tree, need assistance with visited issue
body: <p>I'm having issues when trying to navigate a tree that contains cycles.  My code enters an infinite loop and core dumps.  My problem is that my code isnt setting the nodes to visited after actually visiting them so it just loops forever.  Any help is greatly appreciated.</p>
  
  <pre><code>#include "GraphCode.h"
  //#define EBUG
  
  /******************************************************************************
  static const string TAG = "GraphCode: ";
  
  /******************************************************************************
   * Constructor
  **/
  GraphCode::GraphCode()
  {
  }
  
  /******************************************************************************
   * Destructor
  **/
  GraphCode::~GraphCode()
  {
  }
  
  /******************************************************************************
   * Accessors and Mutators
  **/
  
  /******************************************************************************
   * General functions.
  **/
  
  /******************************************************************************
   * Function 'createGraph'.
   * We read data from the input stream and create a graph.
   *
   * Parameter:
  **/
  void GraphCode::createGraph(Scanner&amp; inStream)
  {
    int nodeCount = -10;
    double connectivity = -3.5;
  
    MyRandom myRandom;
  #ifdef EBUG
  #endif
    cout &lt;&lt; TAG &lt;&lt; "enter createGraph\n"; 
  
    if (inStream.hasNext())
    {
      nodeCount = inStream.nextInt();
      connectivity = inStream.nextDouble();
      cout &lt;&lt; TAG &lt;&lt; "Create a graph of " &lt;&lt; nodeCount
                  &lt;&lt; " nodes and " &lt;&lt; connectivity &lt;&lt; " percent connections"
                  &lt;&lt; endl;
    }
    else
    {
      cout &lt;&lt; TAG &lt;&lt; "NO DATA\n"; 
    }
  
    // first we create the vector of empty nodes
    for (int fromNum = 0; fromNum &lt; nodeCount; ++fromNum)
    {
      Node node = Node(fromNum);
      this-&gt;theGraph.push_back(node);
    }
  
    // now we have a vector so we know we can subscript
    for (int fromNum = 0; fromNum &lt; nodeCount; ++fromNum)
    {
      Node node = this-&gt;theGraph.at(fromNum);
      for (int toNum = 0; toNum &lt; nodeCount; ++toNum)
      {
        if (fromNum == toNum) continue;
        double r = myRandom.randomUniformDouble(0.0, 1.0);
        if (r &lt;= connectivity)
        {
          node.addChildSub(toNum);
        }
      }
      this-&gt;theGraph[fromNum] = node;
    }
  
  #ifdef EBUG
  #endif
    cout &lt;&lt; TAG &lt;&lt; "leave createGraph " &lt;&lt; nodeCount &lt;&lt; " " &lt;&lt; connectivity &lt;&lt; endl; 
  } // void GraphCode::createGraph(Scanner&amp; inStream)
  
  /******************************************************************************
   * Function 'descendFrom'.
  **/
  void GraphCode::descendFrom(ofstream&amp; outStream, string blanks, Node&amp; parentNode)
  {
  #ifdef EBUG
    cout &lt;&lt; blanks &lt;&lt; TAG &lt;&lt; "enter descendFrom\n" &lt;&lt; this-&gt;toStringPath(blanks) &lt;&lt; endl;
  #endif
  
    vector&lt;int&gt; listOfChildren = parentNode.getChildSubs();
    vector&lt;int&gt;::const_iterator iter;
    if (parentNode.hasBeenVisited() == false) 
    {
      path.push_back(Utils::Format(parentNode.getNodeNumber()));
      parentNode.setVisited(true);
      if (listOfChildren.empty())
      {
        outStream &lt;&lt; "Path""\n" &lt;&lt; toStringPath(blanks) &lt;&lt; endl;
        path.pop_back();
      }
  
      else
      {
        for (iter = listOfChildren.begin(); iter != listOfChildren.end(); iter++)
        {
          Node&amp; n = theGraph.at(*iter);
          descendFrom(outStream, blanks, n);
        }
       path.pop_back();
      }
    }
   #ifdef EBUG
    cout &lt;&lt; blanks &lt;&lt; TAG &lt;&lt; "leave descendFrom\n"; 
  #endif
  
  } // GraphCode::descendFrom()
  
  /******************************************************************************
   * Function 'doSearch'.
  **/
  void GraphCode::doSearch(ofstream&amp; outStream)
  {
  #ifdef EBUG
    cout &lt;&lt; TAG &lt;&lt; "enter doSearch\n"; 
  #endif
  
  Node&amp; node = theGraph.at(0);
  string blanks = "     ";
  descendFrom(outStream, blanks, node);
  
  
  #ifdef EBUG
    cout &lt;&lt; TAG &lt;&lt; "leave doSearch\n"; 
  #endif
  
  } // void GraphCode::doSearch()
  
  /******************************************************************************
   * Function 'readGraph'.
  **/
  void GraphCode::readGraph(Scanner&amp; inStream)
  {
    ScanLine scanLine;
  
  #ifdef EBUG
    cout &lt;&lt; TAG &lt;&lt; "enter readGraph\n"; 
  #endif
  
    int firstNode, lastNode;
    firstNode = inStream.nextInt();
    lastNode = inStream.nextInt();
    assert ( 0 == firstNode);
    for(int i = 0; i &lt;= lastNode; ++i)
    {
      Node node = Node(i);
      this-&gt;theGraph.push_back(node);
    }
  
    while (inStream.hasNext())
    {
      string theLine = inStream.nextLine();
  
      scanLine.openString(theLine);
      int parentNodeNum = scanLine.nextInt();
      Node node = this-&gt;theGraph.at(parentNodeNum);
      while( scanLine.hasNext())
      {
        int theChild = scanLine.nextInt();
  
        node.addChildSub(theChild);
      }
      this-&gt;theGraph.at(parentNodeNum) = node;
    }
  
  #ifdef EBUG
    cout &lt;&lt; TAG &lt;&lt; "leave readGraph " &lt;&lt; endl;
  #endif
  } // void GraphCode::readGraph(Scanner&amp; inStream)
  
  /******************************************************************************
   * Function 'toString'.
  **/
  string GraphCode::toString()
  {
    Node node;
    string s = "     ";
  #ifdef EBUG
    cout &lt;&lt; TAG &lt;&lt; "enter toString\n"; 
  #endif
    vector&lt;Node&gt;::const_iterator iter;
    for (iter = theGraph.begin(); iter != theGraph.end(); iter++)
    {
      node = *iter;
      s += "Node:  (" + node.toString() + ")" + "\n";
    }
  #ifdef EBUG
    cout &lt;&lt; TAG &lt;&lt; "leave toString\n"; 
  #endif
    return s;
  } // string GraphCode::toString()
  
  /******************************************************************************
   * Function 'toStringChildren'.
  **/
  string GraphCode::toStringChildren(string blanks, const vector&lt;int&gt;&amp; children)
  {
    string s = "";
  #ifdef EBUG
    cout &lt;&lt; TAG &lt;&lt; "enter toStringChildren\n"; 
  #endif
    Node node;
    vector&lt;int&gt;::const_iterator iter;
    for (iter = children.begin(); iter != children.end(); ++iter)
    { 
      s += Utils::Format((*iter), 6);
    }
  #ifdef EBUG
    cout &lt;&lt; TAG &lt;&lt; "leave toStringChildren\n"; 
  #endif
    return s;
  } // string GraphCode::toStringChildren()
  
  /******************************************************************************
   * Function 'toStringPath'.
  **/
  string GraphCode::toStringPath(string blanks)
  {
    string s = "     ";
  #ifdef EBUG
    cout &lt;&lt; TAG &lt;&lt; "enter toStringPath\n"; 
  #endif
    Node node;
    for (int index = 0; index &lt; this-&gt;path.size(); ++index)
    {
      s += this-&gt;path.at(index) + "\n" + blanks;
    } 
    s += "LEAF\n";
  #ifdef EBUG
    cout &lt;&lt; TAG &lt;&lt; "leave toStringPath\n"; 
  #endif
    return s;
  } // string GraphCode::toStringPath()
  </code></pre>
  
  <p>//expected output:</p>
  
  <pre><code>//PATH TO LEAF 
                             // PATH (   0:  T    1   2   6   9 XXX)
                              //From (   1:  T  XXX)
                             // LEAF
  
  //PATH TO 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
  </code></pre>
  
  <p>I have mine printing out the path correctly but I can't get the 'T' (basically calling parentNode.toString()) and the children to print on the same line as the path</p>