generated from projeto-de-algoritmos/RepositorioTemplate
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhuffman.py
37 lines (31 loc) · 960 Bytes
/
huffman.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import heapq
import os
class HuffmanCoding:
def __init__(self, path):
self.path = path
self.heap = []
self.codes = {}
self.reverse_mapping = {}
class HeapNode:
def __init__(self, char, freq):
self.char = char
self.freq = freq
self.left = None
self.right = None
# definir comparadores
def __lt__(self, outro):
return self.freq < outro.freq
def __eq__(self, outro):
if (outro == None):
return False
if (not isinstance(outro, HeapNode)):
return False
return self.freq == outro.freq
# funções para comprensão:
def cria_frequencia(self, texto):
frequencia = {}
for caracter in texto:
if not caracter in frequencia:
frequencia[caracter] = 0
frequencia[caracter] += 1
return frequencia