Skip to content

Improves readability and processing time #4510

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
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: 9 additions & 8 deletions compression/huffman.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Letter:
def __init__(self, letter, freq):
self.letter = letter
self.freq = freq
self.bitstring = ""
self.bitstring = {}

def __repr__(self):
return f"{self.letter}:{self.freq}"
Expand Down Expand Up @@ -51,10 +51,10 @@ def build_tree(letters):
def traverse_tree(root, bitstring):
"""
Recursively traverse the Huffman Tree to set each
Letter's bitstring, and return the list of Letters
Letter's bitstring dictionary, and return the list of Letters
"""
if type(root) is Letter:
root.bitstring = bitstring
root.bitstring[root.letter] = bitstring
return [root]
letters = []
letters += traverse_tree(root.left, bitstring + "0")
Expand All @@ -65,20 +65,21 @@ def traverse_tree(root, bitstring):
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 letters dictionary to find and print out the
bitstring for each letter.
"""
letters_list = parse_file(file_path)
root = build_tree(letters_list)
letters = traverse_tree(root, "")
print(f"Huffman Coding of {file_path}: ")
letters = {
k: v for letter in traverse_tree(root, "") for k, v in letter.bitstring.items()
}
print(f"Huffman Coding of {file_path}: ")
with open(file_path) as f:
while True:
c = f.read(1)
if not c:
break
le = list(filter(lambda l: l.letter == c, letters))[0]
print(le.bitstring, end=" ")
print(letters[c], end=" ")
print()


Expand Down