Extract transmission cost computation as a separate function

* src/encode.c (transmission_cost): New function.
Optimize Huffman algorithm

* src/encode.c (build_tree): Rewrite loop body to minimize number of
  comparisons and branches.