Implementation for huffman encoding