title: Encode Huffman table with text
body: <p>I have a code which implements Huffman coding to encode texts.</p>
  
  <p>Given the following text</p>
  
  <blockquote>
    <p>abbccc</p>
  </blockquote>
  
  <p>My program generates the following table</p>
  
  <pre><code>a -&gt; 00
  b -&gt; 01
  c -&gt; 1
  </code></pre>
  
  <p>So the encoded text (bit array) is</p>
  
  <pre><code>000101111
  </code></pre>
  
  <p>The problem is: I need to encode the table together with the text and I do not know what is the recommended approach for this.</p>
  
  <p>What I have thought so far:</p>
  
  <ul>
  <li>First byte is the number N of key-value pairs in table</li>
  <li>Following N*2 bytes are the key-value pairs themselves (one byte for key, one byte for value)</li>
  <li>Remaining bits are the encoded text itself</li>
  </ul>
  
  <p>Could you suggest me some more flexible yet inexpensive (low memory usage) approach for this?</p>