Skip to content

enhancement #803

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 16, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions compression/huffman.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def __init__(self, freq, left, right):


def parse_file(file_path):
"""
"""
Read the file and build a dict of all letters and their
frequences, then convert the dict into a list of Letters.
"""
Expand All @@ -29,15 +29,10 @@ def parse_file(file_path):
if not c:
break
chars[c] = chars[c] + 1 if c in chars.keys() else 1
letters = []
for char, freq in chars.items():
letter = Letter(char, freq)
letters.append(letter)
letters.sort(key=lambda l: l.freq)
return letters
return sorted([Letter(c, f) for c, f in chars.items()], key=lambda l: l.freq)

def build_tree(letters):
"""
"""
Run through the list of Letters and build the min heap
for the Huffman Tree.
"""
Expand All @@ -51,7 +46,7 @@ def build_tree(letters):
return letters[0]

def traverse_tree(root, bitstring):
"""
"""
Recursively traverse the Huffman Tree to set each
Letter's bitstring, and return the list of Letters
"""
Expand All @@ -64,9 +59,9 @@ def traverse_tree(root, bitstring):
return letters

def huffman(file_path):
"""
"""
Parse the file, build the tree, then run through the file
again, using the list of Letters to find and print out the
again, using the list of Letters to find and print out the
bitstring for each letter.
"""
letters_list = parse_file(file_path)
Expand Down