tanszek:oktatas:techcomm:huffman_codes
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
tanszek:oktatas:techcomm:huffman_codes [2024/10/07 07:24] – created knehez | tanszek:oktatas:techcomm:huffman_codes [2024/10/07 07:53] (current) – [Steps in Huffman Encoding] knehez | ||
---|---|---|---|
Line 34: | Line 34: | ||
2.) **Merge the two nodes** with the lowest frequency. Create a new node with these two nodes as children, and the frequency of the new node is the sum of the two children' | 2.) **Merge the two nodes** with the lowest frequency. Create a new node with these two nodes as children, and the frequency of the new node is the sum of the two children' | ||
+ | |||
+ | Start with the frequencies: | ||
+ | |||
+ | {{: | ||
+ | |||
+ | First, combine the two lowest frequency nodes. Let’s combine C and H (both have frequency 1): | ||
+ | |||
+ | {{: | ||
+ | |||
+ | Combine the two lowest frequency nodes: F and G | ||
+ | |||
+ | {{: | ||
+ | |||
+ | Combine the two lowest frequency nodes: D and E | ||
+ | |||
+ | {{: | ||
+ | |||
+ | Merging two nodes with lowest frequency: | ||
+ | |||
+ | {{: | ||
+ | |||
+ | Merging two nodes with lowest frequency | ||
+ | |||
+ | {{: | ||
+ | |||
+ | Merging two nodes with lowest frequency | ||
+ | |||
+ | {{: | ||
+ | |||
+ | Final tree: | ||
+ | |||
+ | {{: | ||
+ | |||
+ | === Step 3: Assign Binary Codes === | ||
+ | |||
+ | After building the tree, assign binary codes to each character by traversing the tree. Typically, moving left adds a 0, and moving right adds a 1. | ||
+ | |||
+ | ^Character^Binary Code^ | ||
+ | |A|0| | ||
+ | |B|100| | ||
+ | |C|1100| | ||
+ | |D|1110| | ||
+ | |E|1111| | ||
+ | |F|1010| | ||
+ | |G|1011| | ||
+ | |H|1101| | ||
+ | |||
+ | Huffman encoded result: | ||
+ | |||
+ | < | ||
tanszek/oktatas/techcomm/huffman_codes.1728285848.txt.gz · Last modified: 2024/10/07 07:24 by knehez