news
This commit is contained in:
parent
1aa37a2ed8
commit
a472ce155c
@ -9,4 +9,27 @@ class HuffmanNode:
|
||||
class HuffmanTree:
|
||||
def __init__(self, data):
|
||||
self.root = None
|
||||
self.char_codes = {}
|
||||
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]))
|
||||
Loading…
Reference in New Issue
Block a user