title: trie data structure and printing all the sub-strings
body: <p>I need to print all the unique sub-strings. So I build a <code>trie</code> but not able to figure out how could i print all the sub-strings. 
  For example if the input is <code>aab</code> and <code>aac</code> then I expect it to print <code>"a", "aa", "aab", "aac", "ab", "ac", "b", "c"</code>. </p>
  
  <p>What essentially I need to find out a way to get a unique substrings from set of strings. I am thinking <code>trie</code> is good way since building a <code>trie</code> would take <code>O(n)</code></p>
  
  <p>Following is my code to build a trie. </p>
  
  <pre><code>#include &lt;string&gt;
  #include &lt;iostream&gt;
  #include &lt;vector&gt;
  
  struct trie_node {
      trie_node *(next[26]);
  
      trie_node() {
          for ( int i = 0; i &lt; 26; ++i) {
              next[i] = (trie_node*)0;
          }
      }
  };
  
  trie_node *root;
  char cur_substring[2000];
  void build_trie(std::string&amp; input) {
      trie_node *ptrie = root;
      for ( std::string::iterator it = input.begin(); it != input.end(); ++it) {
          int i = *it - 'a';
          if (ptrie-&gt;next[i] == (trie_node*)0)
              ptrie-&gt;next[i] = new trie_node;
          ptrie = ptrie-&gt;next[i];
      }
  }
  
  void print_sub_strings(trie_node *p_trie, int pos) {
      for (int i = 0; i &lt; 26; i++) {
          if (p_trie-&gt;next[i] != (trie_node*)0) {
              cur_substring[pos] = i + 'a';
              print_sub_strings(p_trie-&gt;next[i], pos + 1 );
          }
      }
  }
  </code></pre>
  
  <p><strong>UPDATE 1</strong></p>
  
  <p>Based on the input I got I re-wrote my code, but it also does not seems to work.</p>
  
  <pre><code>#include &lt;string&gt;
  #include &lt;iostream&gt;
  #include &lt;vector&gt;
  
  const int ALPHABET_SIZE = 26;
  char text[2000];
  int LEN;
  
  struct trie_node_t { 
      trie_node_t*child_list[ALPHABET_SIZE]; 
      trie_node_t() {
          for(int index = 0; index &lt; ALPHABET_SIZE; index++)
              child_list[index] = (trie_node_t*)0;
      }
  };
  
  class Trie {
  public:
      Trie():m_root(new trie_node_t) {
      }
  
      ~Trie() {
          _delete(m_root);
      }
  
      void _insert(int pos) {
          int lcv, index; 
          trie_node_t* t = m_root;
          for(lcv = pos; lcv &lt; LEN; lcv++) {
              index = text[lcv] - 'a';
              if (t-&gt;child_list[index] == (trie_node_t*)0) {
                  t-&gt;child_list[index] = new trie_node_t;
              }
              t = t-&gt;child_list[index];
          }
      }
      void insert() {
          for ( int i = 0; i &lt; LEN; i++) {
              _insert(i);
          }
      }
  
      void iterate() {
          _iterate(m_root, "");
      }
  
      void _iterate(trie_node_t *t, std::string prefix) {        
          for (int i = 0; i &lt; ALPHABET_SIZE; i++) {
              if (t-&gt;child_list[i] != (trie_node_t*)0) {
                  prefix += 'a' + i;
                  std::cout &lt;&lt; prefix &lt;&lt; std::endl;
                  _iterate(t-&gt;child_list[i], prefix);
              }   
          }
      }   
  private: 
      int node_count;
      trie_node_t* m_root;
  
      void _delete (trie_node_t* t) {
          int index; 
          if (t != (trie_node_t*)0) {
              for(index = 0; index &lt; ALPHABET_SIZE; index++)
                  _delete(t-&gt;child_list[index]);
              delete t;
          }
      }    
  };
  
  int main ( int argc, char** argv) {
      Trie *pTrie =  new Trie();
  
      strcpy(text,"aab");
      LEN = strlen(text);
      pTrie-&gt;insert();
  
      strcpy(text,"aac");
      LEN = strlen(text);
      pTrie-&gt;insert();
  
      pTrie-&gt;iterate();
  }
  </code></pre>
  
  <p>Output is </p>
  
  <pre><code>a
  aa
  aab
  aabc
  aab
  aabc
  ab
  abc
  Press any key to continue . . .
  </code></pre>