@@ -18,7 +18,7 @@ def __init__(self, freq, left, right):
18
18
19
19
20
20
def parse_file (file_path ):
21
- """
21
+ """
22
22
Read the file and build a dict of all letters and their
23
23
frequences, then convert the dict into a list of Letters.
24
24
"""
@@ -29,15 +29,10 @@ def parse_file(file_path):
29
29
if not c :
30
30
break
31
31
chars [c ] = chars [c ] + 1 if c in chars .keys () else 1
32
- letters = []
33
- for char , freq in chars .items ():
34
- letter = Letter (char , freq )
35
- letters .append (letter )
36
- letters .sort (key = lambda l : l .freq )
37
- return letters
32
+ return sorted ([Letter (c , f ) for c , f in chars .items ()], key = lambda l : l .freq )
38
33
39
34
def build_tree (letters ):
40
- """
35
+ """
41
36
Run through the list of Letters and build the min heap
42
37
for the Huffman Tree.
43
38
"""
@@ -51,7 +46,7 @@ def build_tree(letters):
51
46
return letters [0 ]
52
47
53
48
def traverse_tree (root , bitstring ):
54
- """
49
+ """
55
50
Recursively traverse the Huffman Tree to set each
56
51
Letter's bitstring, and return the list of Letters
57
52
"""
@@ -64,9 +59,9 @@ def traverse_tree(root, bitstring):
64
59
return letters
65
60
66
61
def huffman (file_path ):
67
- """
62
+ """
68
63
Parse the file, build the tree, then run through the file
69
- again, using the list of Letters to find and print out the
64
+ again, using the list of Letters to find and print out the
70
65
bitstring for each letter.
71
66
"""
72
67
letters_list = parse_file (file_path )
0 commit comments