We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent a4dbc2a commit 9dcc06fCopy full SHA for 9dcc06f
contents/huffman_encoding/code/python/huffman.py
@@ -29,11 +29,17 @@ def build_huffman_tree(message):
29
# insert the new tree there
30
trees.insert(index, (new_tree, new_weight))
31
32
- huffman_tree = trees[0][0]
+ # Return an empty list if the message was empty, else the tree
33
+ huffman_tree = [] if not trees else trees[0][0]
34
return huffman_tree
35
36
# constructs the mapping with recursion
37
def build_codebook(tree, code=''):
38
+ # Check whether our tree contains more than 1 element or not
39
+ if not tree:
40
+ return []
41
+ elif type(tree) != list:
42
+ return [(tree, '0')]
43
44
codebook = []
45
0 commit comments