E
E
Evgeny Trofimov2016-04-07 16:05:12
Programming
Evgeny Trofimov, 2016-04-07 16:05:12

How to traverse the tree, generating code for each leaf (character)?

I'm trying to write a Huffman algorithm for a text file. Used here this article on Habré.
It turned out to create a tree - I checked it on a phrase from the article - everything works correctly.
But I'm stuck on creating a code for each character - I don't quite understand how "just walk through the tree, and for each transition add 0 if we go to the left, and 1 if we go to the right."
Tell me, please, how to implement ...
Well, that is, I roughly understand this - during a recursive traversal, if you meet a leaf tree->left==NULL && tree->right==NULL, then display the symbol and its code and do a return, but I don’t quite imagine how to memorize the code ...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Mirilaczvili, 2016-04-07
@deadrime

Zeros and ones are "stored" in bytes. There and save with a bit mask. A whole octet of one byte is filled - you proceed to filling in the next one. And the bytes themselves are accumulated in the buffer and thrown onto the disk when the buffer is full.
I have not coded according to Huffman myself, but I imagine it to be so.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question