Question: Can someone please help me?! This is for a Python-based Data Structures class. I'm trying to understand how to do this assignment, but I'm just
Can someone please help me?! This is for a Python-based Data Structures class. I'm trying to understand how to do this assignment, but I'm just not sure how to do it. Please, any and all help is greatly appreciated.
Assignment Summary: Implement a function called cnt_freq(filename) that 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. Use the built-in Python List data structure of size 256 for counting the occurrences of characters.
Start by defining a function comes_before (a, b) for Huffman trees that returns true if tree a comes before tree b. A Huffman tree a comes before Huffman tree b if the occurrence count of a is smaller than that of b. Write a function that builds a Huffman tree from a given list of the number of occurrences of characters returned by cnt_fref() and returns the root node of that tree.
Create a sorted list (can be a Python list, and you can use built-in functions) of individual Huffman trees each consisting of a single HuffmanNode node containing the character and its occurrence counts.
Implement a function named create_code(root_node) that traverses the Huffman tree that was passed as an argument and returns an array (using a Python list) of 256 strings. Traverse the tree from the root to each leaf node and adding a 0 when we go left and a 1 when we go right constructing a string of 0s and 1s. Write a function called huffman_encode(in_file, out_file) (use that exact name) that reads an input text file and writes to an output file the following:
o A header (see below for format) on the first line in the file (should end with a newline)
o Using the Huffman code, the encoded text into an output file.
Write a function called create_header(list_of_freqs) that takes as a parameter the list of freqs previously determined from cnt_freq(filename). The create_header function returns a string of the ASCII values and their associated frequencies from the input file text, separated by one space. For example, create_header(list_of_freqs) would return 97 3 98 4 99 2 for the text aaabbbbcc.
The huffman_encode function accepts two file names in that order: input file name and output file name, represented as strings.
Example:
input file:
abcd abc ab a
output file:
32 3 97 4 98 3 99 2 100 1
11011011000011011010011010011
class HuffmanNode: def __init__(self, char, freq): self.char = char # stored as an integer - the ASCII character code value self.freq = freq # the frequency 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
