title: Building a General Tree - creating too many brothers?
body: <p>I'm trying to construct a general tree with c++. the data structure i'm using is a Node class that has 2 pointers. pointer Next points to the first son of that node and pointer bros that points to the next brother of the node.</p>
  
  <p>I'm having a problem with my code that for some unknown reason creates an infinite number of brothers for a node and i have no idea why,
  Was hoping you can take a look and maybe suggest where i went wrong.
  Thank you!</p>
  
  <p>Node Class:</p>
  
  <pre><code>class Node { 
  private:
      int capacity;
      int price;
      int height;
      int num_of_leafs; // represents the total number of leafs under this branch
      Node* bros;     
  
  public:
      Node* next;
  
      Node(){
          capacity = 0;          
          price = 0;
          height = 0;
          next = NULL;
          bros = NULL;
      }
      Node(int capacity_){
          capacity = capacity_;  
          price = 0;
          height = 0;
          next = NULL;
          bros = NULL;
      }                
  
      //should return true if this node has no children, false otherwise.
      bool isLeaf()
      { 
          if (this-&gt;next == NULL) { return 1; }
          else { return 0; }
      }
  
      // This methoed counts the number of leafs sons of this node.
      int Count_Leafs()
      {
          int num = 0;
          if (this-&gt;isLeaf()) { return num; }
          else {
              Node* iter = this-&gt;next;
              while ( iter != NULL )
              {
                  if ( iter-&gt;isLeaf() ) { num++ ; } 
                  iter = iter-&gt;bros;
              }
          }
          return num;
      }
  
      //this method adds a child to this node.
      void addChild(Node* child)
      {
          if ( child-&gt;height != 0 )
          {
              cout &lt;&lt; "Node already in tree" ;
              return; }
          if( this-&gt;next == NULL ) 
          { 
              this-&gt;next = child;
          }
          else {
              Node* iter = this-&gt;next;
              while ( iter-&gt;getNextBro() != NULL ) { iter = iter-&gt;getNextBro(); }
              iter-&gt;setNextBro(child);
          }
          child-&gt;height = this-&gt;height + 1;
      }
          // this methoed checks if current node has brothers
      bool hasBro()
      {
          if ( this-&gt;getNextBro() == NULL ) { return 0;}
          else { return 1;}
      }
          Node* getNextBro()
      {
          return bros;
      }
  
      void setNextBro(Node* next)
      {
          this-&gt;bros = next;
      }
  
      int getHeight()
      {
          return this-&gt;height;
      }
  
      int getNumBros()
      {
          Node* iter = this-&gt;bros;
          int num=0;
          while ( iter != NULL )
          {
              num++;
              iter = this-&gt;getNextBro();
          }
          return num;
      }
  </code></pre>
  
  <p>And the tree creation:</p>
  
  <pre><code>    Node* s8 = new Node(8);
  Node* s5 = new Node(5);
  Node* s6 = new Node(6);
  for(int i=0; i &lt; 2 ; i++){
      s6-&gt;addChild(new Node());      
  }
  
  Node* s7 = new Node(7);
  Node* s2 = new Node(2);
  for(int i=0; i &lt; 3 ; i++){
      s2-&gt;addChild(new Node());
  }
  
  Node* s3 = new Node(3);
  Node* s2_2 = new Node(2);  
  s2_2-&gt;addChild(new Node());
  
  Node* s4 = new Node(4);
   for(int i=0; i &lt; 5 ; i++){
      s4-&gt;addChild(new Node());
  }
  
  Node* s1 = new Node(1);
   for(int i=0; i &lt; 2 ; i++){
      s1-&gt;addChild(new Node());      
  }
  Node* s2_3 = new Node(2);
   for(int i=0; i &lt; 4 ; i++){
      s2_3-&gt;addChild(new Node());
  }
  Node* s2_4 = new Node(2);  
  for(int i=0; i &lt; 3 ; i++){
      s2_4-&gt;addChild(new Node());
  }
  s8-&gt;addChild(s5);
  cout &lt;&lt; "S5 bros: " &lt;&lt; s5-&gt;getNumBros() &lt;&lt; "\n";
  cout &lt;&lt; "S6 bros: " &lt;&lt; s6-&gt;getNumBros() &lt;&lt; "\n";
  s8-&gt;addChild(s6);    
  cout &lt;&lt; "S5 bros: " &lt;&lt; s5-&gt;getNumBros() &lt;&lt; "\n";
  s5-&gt;addChild(s7);
  cout &lt;&lt; "S5 bros: " &lt;&lt; s5-&gt;getNumBros() &lt;&lt; "\n";
  s5-&gt;addChild(s2);
  s6-&gt;addChild(s3);
  s6-&gt;addChild(s2_2);
  s7-&gt;addChild(s4);
  s7-&gt;addChild(s1);
  s3-&gt;addChild(s2_3);
  s3-&gt;addChild(s2_4);
  </code></pre>
  
  <p>Thank you!</p>