def _preserve(node=node): if node.is_leaf: preserved.append('1') preserved.append(pad_to_full_byte(bit_string_for(node.char))) else: preserved.append('0') _preserve(node.left_child) _preserve(node.right_child)
def preserve_tree(node): # TODO: this could be a method on HuffmanTree, but I don't know if it should be preserved = [] def _preserve(node=node): if node.is_leaf: preserved.append('1') preserved.append(pad_to_full_byte(bit_string_for(node.char))) else: preserved.append('0') _preserve(node.left_child) _preserve(node.right_child) _preserve() preserved = ''.join(preserved) encoded_tree_size = pad_to_full_byte(bit_string_for(len(preserved)), byte_length=16) return encoded_tree_size + preserved