title: Decoding Huffman Tree
body: <p>I am implementing a function that takes in a tree and an encoded string.
  Example:</p>
  
  <pre><code>decode(*Huffmantree, "10010101010")
  </code></pre>
  
  <p>I want this function to return decoded string for the encoded string in the input relative to the Huffman tree input.</p>
  
  <p>The code I have so far:</p>
  
  <pre><code>string decode(NodePtr root, string encoded_str)
  {
      string temp = "";
      for (int i = 0 ; i &lt; encoded_str.size() ; i++)
      {
          if (root-&gt;is_leaf() == true)
          {
              temp[i] = root-&gt;letter;
              //cout &lt;&lt; root-&gt;letter;
          }
          if (root-&gt;left != NULL)
          {
              encoded_str.
          }
          if(root-&gt;right != NULL)
          {
  
          }
      }
      return temp;
  }
  </code></pre>
  
  <p><strong>I am having trouble implementing what happens when either left or right is not NULL, i.e. when I have to continue to the next node.</strong></p>
  
  <p>Any ideas?</p>
  
  <p>edit:</p>
  
  <pre><code>string decode(NodePtr root, string encoded_str)
  {
      string temp = "";
      int i;
      for( i = 0 ; i &lt; encoded_str.size() ; i++)
      {
  
      if(root == NULL)
      {
          cout&lt;&lt;"error in string"&lt;&lt;endl;
          return temp;
          //cout&lt;&lt;root-&gt;letter;
      }
      temp[i] = root-&gt;letter;
      if(encoded_str[i] == '0')
      {
          root = root-&gt;left;
      }
      else
      {
          root = root-&gt;right;
      }
      }
  //    for (int i = 0 ; i &lt; temp.size(); i++)
  //    {
  //        cout&lt;&lt;temp[i];
  //    }
  //    cout&lt;&lt;endl;
      temp[i]='/0';
      return temp;
  }
  </code></pre>