-2

I'm creating a basic Huffman encoding/decoding tool. I've found this question which helped me implement a header that stores my generated huffman tree in binary form. I can also use the tree to encode/decode a text into a binary file as well. So the program actually works, but I still have a problem.

Currently the header and the encoded binary are in separate files because I cannot figure out a way to put them into the same file in a way that makes it easy for me to read the header at the start of the decoding procedure. Hard coding in some "end of header" character seems like a rather hacky way to do this, not to mention that there is the possibility that the some initial bits of the terminating character might be read in as part of the encoded tree in the header, causing the entire tree to get corrupted.

Although my program works with separate header and body files, I'd like to merge them. Any ideas on how I can do this?

Community
  • 1
  • 1
Pomacanthidae
  • 207
  • 1
  • 3
  • 8

1 Answers1

2

You don't need to do anything special to merge your header (the tree) and the content (the Huffman-encoded text).

If you look in the answer in the question you posted, here, and examine the algorithm for decoding (the ReadNode(BitReader reader) pseudo-code function there) you can see that the algorithm stops reading the tree just because it reads it all - not because it reaches an EOF character or something of the like.

It doesn't need to search for an EOF because it recursively calls itself only for nodes that have children (0-bits). Once the algorithm has reaches all leaves there is no more recursive calling, so the reader will be positioned exactly in the right place for you to start reading the content (just after reading the whole header, with no additional "end-of-header" indications.

Community
  • 1
  • 1
et_l
  • 1,868
  • 17
  • 29