title: Deleting all from trie tree
body: <p>I always seem to get in trouble when I'm deleting all nodes from a tree. I am trying to release all the memory I allocated when creating a trie tree.</p>
  
  <p>I am suppose to create a function remove_all</p>
  
  <p>Is it enough to delete just the "root"</p>
  
  <p>something like this:</p>
  
  <pre><code>void PrefixStringSet::remove_all(NodePtr node)
      {
           delete root;
  
      }
  </code></pre>
  
  <p>Or do I have to delete each node with something like this:</p>
  
  <pre><code>void PrefixStringSet::remove_all(NodePtr node)
  {
       if(!root)
     {
         return;
     }
     remove_all(root-&gt;children);
  
  
     delete root;
  }
  </code></pre>
  
  <p>Obviously neither of these are working or I wouldn't be here :). </p>
  
  <p>Other question. Do I have to call the remove_all function in my main function if my destructor is implemented like this</p>
  
  <pre><code>PrefixStringSet::~PrefixStringSet()
  {
      remove_all(root);
  }
  </code></pre>
  
  <p>Or does the destructor automatically delete the trees/nodes I create?</p>
  
  <p><strong>Edit</strong></p>
  
  <pre><code>struct TrieNode
  {
      TrieNode(bool present = false);
      bool is_leaf();
  
      bool present;
      TrieNode* children[ALPHABET_SIZE];
  };
  
  class PrefixStringSet
  {
      public:
          // Creates an empty prefix string set.
          PrefixStringSet();
  
          ~PrefixStringSet();
  
          bool insert(string s);
  
          bool contains(string s);
  
      private:
          NodePtr root;
          void remove_all(NodePtr node);
  };
      typedef TrieNode* NodePtr;
  </code></pre>