omaonfpna
This commit is contained in:
parent
1763708d88
commit
83f56bca46
@ -56,6 +56,31 @@ class HuffmanTree:
|
|||||||
return
|
return
|
||||||
self.make_codes(node.left, path + "0")
|
self.make_codes(node.left, path + "0")
|
||||||
self.make_codes(node.right, path + "1")
|
self.make_codes(node.right, path + "1")
|
||||||
|
def encode(self, text):
|
||||||
|
# zakodujeme text na nuly a jednotky
|
||||||
|
res = ""
|
||||||
|
for ch in text:
|
||||||
|
res += self.char_codes[ch]
|
||||||
|
return res
|
||||||
|
|
||||||
|
def decode(self, bits):
|
||||||
|
if not bits:
|
||||||
|
return "
|
||||||
|
if self.root.left is None and self.root.right is None:
|
||||||
|
return self.root.data * len(bits)
|
||||||
|
|
||||||
|
# dekodujeme bity naspat na text
|
||||||
|
res = ""
|
||||||
|
curr = self.root
|
||||||
|
for b in bits:
|
||||||
|
if b == "0":
|
||||||
|
curr = curr.left
|
||||||
|
else:
|
||||||
|
curr = curr.right
|
||||||
|
|
||||||
|
if curr.left is None and curr.right is None:
|
||||||
|
res += curr.data
|
||||||
|
curr = self.root
|
||||||
|
return res
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user