title: Depth-First-Search terminating prematurely
body: <p>In <a href="https://stackoverflow.com/questions/27580946/private-array-of-adjacent-node-addresses-in-c">this question</a> I asked some questions about implementing my own graph-creating data structure. Now that it is finally working I am implementing basic DFS and BFS. My BFS is working with a queue class that I also created myself. My DFS however is not working, and I can't seem to figure out why. It only works starting on the first node and finding the first adjacent node ("USA"). Any other combination returns the NULL pointer (so no 'real' run-time errors). Below is the code, where I deleted unrelated routines and content that is already working.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;string&gt;
  using namespace std;
  
  template&lt;class T&gt;
  class Node{
      T data;
      vector&lt;Node&lt;T&gt;*&gt; adjacent;
      public:
          bool visited;
          int n;
          Node(T initData) : data(initData), visited(false),n(0){}
          void addAdjacent(Node&lt;T&gt;&amp; other){//call from within GraphCl class. push_back in adjacent}
          T getData(){return data;}
          Node&lt;T&gt;* getEdge(int edgeNum){return adjacent[edgeNum];}
  };
  
  template&lt;class T&gt;
  class GraphCl{
      int n;
      vector&lt;Node&lt;T&gt;*&gt; nodes;
      public:
          GraphCl(int size=0): n(size) {//[...]}
          ~GraphCl(){//[...]}
      void addNode(T input){//push_back new node to nodes vector}
      void addEdge(int baseNode, int edgeNode){//push_back edgeNode adres in adjacent vector}
      Node&lt;T&gt;* getNodeAddress(int idx){return nodes[idx];}
  };
  
  template&lt;class T&gt;
  Node&lt;T&gt;* dfs(Node&lt;T&gt;* rootNode, T key){
      if(rootNode-&gt;visited == false){
          rootNode-&gt;visited = true;
          if (rootNode-&gt;getData() == key){
              return rootNode;
          }
          for(int i=0;i&lt;rootNode-&gt;n;i++){
              if(dfs(rootNode-&gt;getEdge(i),key) != NULL){
                  return dfs(rootNode-&gt;getEdge(i),key);
              }
          }
      }
      return NULL;
  }
  
  int main(){
      GraphCl&lt;string&gt; *myGraph = new GraphCl&lt;string&gt;();
      myGraph-&gt;addNode("USA");//0
      myGraph-&gt;addNode("Australia");//1
      myGraph-&gt;addNode("The_Netherlands");//2
      //more nodes added here ...
      myGraph-&gt;addNode("Indonesia");
      myGraph-&gt;addNode("Canada");
      myGraph-&gt;addNode("Czech_Republic");
      myGraph-&gt;addEdge(0,1);
      myGraph-&gt;addEdge(0,4);
      myGraph-&gt;addEdge(2,10);
      //more edges added here ...
      myGraph-&gt;addEdge(3,1);
      myGraph-&gt;addEdge(11,12);
      myGraph-&gt;addEdge(6,11);
      Node&lt;string&gt; *rootNode = myGraph-&gt;getNodeAddress(4);
      cout &lt;&lt; dfs&lt;string&gt;(rootNode, "USA")-&gt;getData();
      return 0;
  }
  </code></pre>
  
  <p>Is there something crucially wrong with the DFS function that one can spot immediately? Cannot figure it out.</p>