diff --git a/u9/huffman_tree.py b/u9/huffman_tree.py index 9313b39..d02d8ae 100644 --- a/u9/huffman_tree.py +++ b/u9/huffman_tree.py @@ -32,4 +32,14 @@ class HuffmanTree: # zoznam listov nodes = [] for ch in freq: - nodes.append(HuffmanNode(ch, freq[ch])) \ No newline at end of file + nodes.append(HuffmanNode(ch, freq[ch])) + # spajame dva uzly s najmensou frekvenciou + while len(nodes) > 1: + nodes.sort(key=lambda x: x.frequency) + left = nodes.pop(0) + right = nodes.pop(0) + parent = HuffmanNode(None, left.frequency + right.frequency, left, right) + nodes.append(parent) + + self.root = nodes[0] + self.make_codes(self.root, "") \ No newline at end of file