MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / traverse_tree

Function traverse_tree

data_compression/huffman.py:54–66  ·  view source on GitHub ↗

Recursively traverse the Huffman Tree to set each Letter's bitstring dictionary, and return the list of Letters

(root: Letter | TreeNode, bitstring: str)

Source from the content-addressed store, hash-verified

52
53
54def traverse_tree(root: Letter | TreeNode, bitstring: str) -> list[Letter]:
55 """
56 Recursively traverse the Huffman Tree to set each
57 Letter's bitstring dictionary, and return the list of Letters
58 """
59 if isinstance(root, Letter):
60 root.bitstring[root.letter] = bitstring
61 return [root]
62 treenode: TreeNode = root
63 letters = []
64 letters += traverse_tree(treenode.left, bitstring + "0")
65 letters += traverse_tree(treenode.right, bitstring + "1")
66 return letters
67
68
69def huffman(file_path: str) -> None:

Callers 1

huffmanFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected