title: Making a tree of n children to store directories of computer
body: <p>I am making a tree of n children to store directories of computer. Now, concept is simply make a tree (that would not be a BT of course) and each node will have children as well. Consider the code below then I will explain the problem.
  First Consider this:</p>
  
  <pre><code>C/users/DeadCoder/Movies/Batman.
  </code></pre>
  
  <p>Now In my <code>main.cpp</code> I have this all C, users, DeadCoder, Movies, Batman in a vector and then I send two pairs in insert Func. if <code>root==NULL</code>; it would just insert C. Next time C and users would go. It would find C and then insert users occordingly. Let's now see the code .</p>
  
  <pre><code>template &lt;class T&gt;
  
  struct Node;
  
  template &lt;class T&gt;
  class tree
  {
      Node&lt;T&gt; *root;
  
  public:
  
      tree();
      ~tree();
      int insert(T str, T str1);
      Node&lt;T&gt; *getRoot();
      Node&lt;T&gt; *search(T item, Node&lt;T&gt; *tempPtr);
  };
  
  template &lt;class T&gt;
  struct Node{
  
      T n;
      Node&lt;T&gt; *sibling;
      tree&lt;T&gt; children; // SEE my each node has children.
      Node(T N){
          this-&gt;n = N;
          this-&gt;sibling = NULL;
      }
  
  };
  </code></pre>
  
  <p>// In .cpp FILE;
  // Initilaizer</p>
  
  <pre><code>template &lt;class T&gt;
  tree&lt;T&gt;::tree() // Constructor Initialization.
  {
      root=NULL;
  }
  </code></pre>
  
  <p>// Insert Function.</p>
  
  <pre><code>template &lt;class T&gt;
  int tree&lt;T&gt;::insert(T push, T find)
  {
  
      Node&lt;T&gt; *rPtr = root;
      if (rPtr==NULL){
              //ROOT is NULL. C needs to be inserted which is in find.
          Node&lt;T&gt; *pusPtr = new Node&lt;T&gt;(find);              
  
          root = pushPtr;
          root-&gt;sibling=NULL;
          return 0;
      }
      else if(rPtr!=NULL){
          Node&lt;T&gt; *pushPtr = new Node&lt;T&gt;(push);
          Node&lt;T&gt; *temp2 =  search(find, root);   
          Node&lt;T&gt; *temp = temp2-&gt;children.getRoot(); // say it LINE_40.
          if (temp==NULL){ 
              temp = pushPtr;
              temp-&gt;sibling=NULL;
              return 1;
  
          }
          // children are already present.
          else if(temp!=NULL){
  
              // You don't need to know code for this part.
              }
      }//if.
  </code></pre>
  
  <p>}</p>
  
  <p>// Search Function.</p>
  
  <pre><code>template &lt;class T&gt;
  Node&lt;T&gt; *tree&lt;T&gt;::search(T data, treeNode&lt;T&gt;* N)
  {
      if (N-&gt;n==data){ // where n represent directory.
          return N; // data found. 
      }//if....
      else{
  
          Node&lt;T&gt; *child = N-&gt;children.getRoot(); 
  // This is where i get Segmentation fault,
  // because child is ==NULL; but you see in LINE_40 I did insert the child for C.
  
  
      if(child!=NULL){ // say it line 80.
          search(data, child);
      }//if...
      if(child-&gt;sibling!=NULL){
          search(data, child-&gt;sibling);
      }   
     }
  
  
  }// search....
  </code></pre>
  
  <p>PROBLEM: <code>C</code> inserted. <code>Users</code> inserted. Now in search function at Line 80, it comes to find the child for C. and it should be Users as I have inserted it in LINE 40. BUT Instead it says child==NULL. I have been debugging for hours and I don't know why it says so. I hope Everybody gets the problem.
  Now I really need to know why it is regarding C child to be NULL, It has to be users. Can anyOne see what is the problem???? HELP !!!!</p>