From a472ce155ca32397e1992b737086c75f24e94623 Mon Sep 17 00:00:00 2001 From: va270fv Date: Sun, 15 Nov 2026 14:10:07 +0000 Subject: [PATCH] news --- u9/huffman_tree.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/u9/huffman_tree.py b/u9/huffman_tree.py index 6fcf1a9..9313b39 100644 --- a/u9/huffman_tree.py +++ b/u9/huffman_tree.py @@ -9,4 +9,27 @@ class HuffmanNode: class HuffmanTree: def __init__(self, data): self.root = None - self.char_codes = {} \ No newline at end of file + self.char_codes = {} + + if not data: + return + + # spocitame krat znakov + freq = {} + for ch in data: + if ch in freq: + freq[ch] += 1 + else: + freq[ch] = 1 + + # ak je len jeden typ + if len(freq) == 1: + ch = data[0] + self.root = HuffmanNode(ch, freq[ch]) + self.char_codes[ch] = "0" + return + + # zoznam listov + nodes = [] + for ch in freq: + nodes.append(HuffmanNode(ch, freq[ch])) \ No newline at end of file