title: How can i make this algorithm more memory efficient?
body: <p>I have been working on this piece of code for a couple of days and it seems
  to work just fine for search trees of relative small size, but working with higher ones gives segmentation fault.</p>
  
  <p>I have tried to find the specific place where such error comes from by using printing flags, and it seems to break at the is_goal() function. But the code of that function can be assumed as correct.</p>
  
  <p>So the problems seems to be a memory problem, that's why I have the queue in the heap, and also the Nodes that I am creating. But it still crashes.</p>
  
  <p>Are there any more memory saving tricks that can be taking into account that I am not using? Or maybe there is a better way to save such nodes?</p>
  
  <p>It is important to note that I need to use BFS (I can't use A* to make the search tree smaller).</p>
  
  <p>Also if you need to know, the map is a hash table where I save the coloring of the nodes so I don't have duplicates when exploring (This is because the hash saves depending on the state information and not the Node information). </p>
  
  <p>EDIT: i have been told to indicate the goal to accomplish, such is to find the goal state in the search tree, the goal is to be be able to iterate over
  big problems like rubik 3x3x3, n-puzzle 4x4 5x5, tower of hanoi and such.To do so, the memory used has to be minimal since the search tree of such problems are really big, the final objective is to compare this algorithm with others like a*, dfid, ida*, and so on. I hope this is enough information.</p>
  
  <pre><code>  Node* BFS(state_t start ){
  
      state_t state, child;  
      int d, ruleid;
      state_map_t *map = new_state_map();
      ruleid_iterator_t iter;
      std::queue&lt;Node*&gt; *open = new std::queue&lt;Node*&gt;();
  
      state_map_add( map, &amp;start, 1); 
  
      Node* n = new Node(start);
      open-&gt;push(n);
  
      while( !open-&gt;empty() ) {
          n = open-&gt;front();
          state = n-&gt;get_state();
          open-&gt;pop();
  
          if (is_goal(&amp;state) == 1) return n;
  
          init_fwd_iter( &amp;iter, &amp;state ); 
  
          while( ( ruleid = next_ruleid( &amp;iter ) ) &gt;= 0 ) {
              apply_fwd_rule( ruleid, &amp;state, &amp;child );
  
  
              const int *old_child_c = state_map_get( map, &amp;child );
              if ( old_child_c == NULL ) {
                  state_map_add( map, &amp;child, 1 );
                  Node* nchild = new Node(child); 
                  open-&gt;push(nchild);
              }
  
          }
  
      }
  
      return NULL;
  }  
  </code></pre>