Question: Python3 Huffman Implementation help, write code underneath functions class HuffmanNode: def __init__(self, char, freq): self.char = char # stored as an integer - the ASCII
Python3 Huffman Implementation help, write code underneath functions
| class HuffmanNode: | |
| def __init__(self, char, freq): | |
| self.char = char # stored as an integer - the ASCII character code value | |
| self.freq = freq # the freqency associated with the node | |
| self.left = None # Huffman tree (node) to the left | |
| self.right = None # Huffman tree (node) to the right | |
| def set_left(self, node): | |
| self.left = node | |
| def set_right(self, node): | |
| self.right = node | |
| def comes_before(a, b): | |
| """Returns True if tree rooted at node a comes before tree rooted at node b, False otherwise""" | |
| def combine(a, b): | |
| """Creates and returns a new Huffman node with children a and b, with the "lesser node" on the left | |
| The new node's frequency value will be the sum of the a and b frequencies | |
| The new node's char value will be the lesser of the a and b char ASCII values""" | |
| def cnt_freq(filename): | |
| """Opens a text file with a given file name (passed as a string) and counts the | |
| frequency of occurrences of all the characters within that file""" | |
| def create_huff_tree(char_freq): | |
| """Create a Huffman tree for characters with non-zero frequency | |
| Returns the root node of the Huffman tree""" | |
| def create_code(node): | |
| """Returns an array (Python list) of Huffman codes. For each character, use the integer ASCII representation | |
| as the index into the arrary, with the resulting Huffman code for that character stored at that location""" | |
| def create_header(freqs): | |
| """Input is the list of frequencies. Creates and returns a header for the output file | |
| Example: For the frequency list asscoaied with "aaabbbbcc, would return 97 3 98 4 99 2 """ | |
| def huffman_encode(in_file, out_file): | |
| """Takes inout file name and output file name as parameters | |
| Uses the Huffman coding process on the text from the input file and writes encoded text to output file | |
| Take not of special cases - empty file and file with only one unique character"""
|
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
