Skip to content

Commit 6f218aa

Browse files
changed file compressao_huffman.py
1 parent a92aa30 commit 6f218aa

File tree

1 file changed

+40
-42
lines changed

1 file changed

+40
-42
lines changed

src/python/compressao_huffman.py

+40-42
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,81 @@
1-
# Árvore Huffman
2-
class node:
1+
# Huffman Tree
2+
class Node:
33
def __init__(self, freq, symbol, left=None, right=None):
4-
# Frequência do Símbolo
4+
# Symbol frequency
55
self.freq = freq
66

7-
# Símbolo (caracter)
7+
# Symbol (character)
88
self.symbol = symbol
99

10-
# nó à esquerda do nó atual
10+
# Left node of the current node
1111
self.left = left
1212

13-
# nó à direita do nó atual
13+
# Right node of the current node
1414
self.right = right
1515

16-
# direção da árvore (0/1)
16+
# Tree direction (0/1)
1717
self.huff = ""
1818

1919

20-
# Função utilitária para imprimir
21-
# códigos huffman para todos os símbolos
22-
# na nova árvore huffman que sera criada
20+
# Utility function to print
21+
# Huffman codes for all symbols
22+
# in the newly created Huffman tree
23+
def print_nodes(node, val=""):
24+
# Huffman code for the current node
25+
new_val = val + str(node.huff)
2326

24-
25-
def printNodes(node, val=""):
26-
# código huffman para o nó atual
27-
newVal = val + str(node.huff)
28-
29-
# se o nó não pertence á ponta da
30-
# árvore então caminha dentro do mesmo
31-
# até a ponta
27+
# If the node does not belong to the
28+
# leaf of the tree, then traverse inside
29+
# until reaching the leaf
3230
if node.left:
33-
printNodes(node.left, newVal)
31+
print_nodes(node.left, new_val)
3432
if node.right:
35-
printNodes(node.right, newVal)
33+
print_nodes(node.right, new_val)
3634

37-
# Se o nó estiver na ponta da árore
38-
# então exibe o código huffman
35+
# If the node is at the leaf of the tree,
36+
# then display the Huffman code
3937
if not node.left and not node.right:
40-
print(f"{node.symbol} -> {newVal}")
38+
print(f"{node.symbol} -> {new_val}")
4139

4240

43-
# caracteres para à árvore huffman
41+
# Characters for the Huffman tree
4442
chars = ["a", "b", "c", "d", "e", "f"]
4543

46-
# frequência dos caracteres
44+
# Frequencies of the characters
4745
freq = [5, 9, 12, 13, 16, 45]
4846

49-
# lista contendo os nós não utilizados
47+
# List containing the unused nodes
5048
nodes = []
5149

5250
if __name__ == "__main__":
53-
# convertendo caracteres e frequência em
54-
# nós da árvore huffman
51+
# Converting characters and frequencies into
52+
# Huffman tree nodes
5553
for x in range(len(chars)):
56-
nodes.append(node(freq[x], chars[x]))
54+
nodes.append(Node(freq[x], chars[x]))
5755

5856
while len(nodes) > 1:
59-
# Ordena todos os nós de forma ascendente
60-
# baseado em sua frequência
57+
# Sort all nodes in ascending order
58+
# based on their frequency
6159
nodes = sorted(nodes, key=lambda x: x.freq)
6260

63-
# Seleciona os dois nós menores
61+
# Select the two smallest nodes
6462
left = nodes[0]
6563
right = nodes[1]
6664

67-
# Atribui um valor direcional à estes nós
68-
# (direita ou esquerda)
65+
# Assign a directional value to these nodes
66+
# (left or right)
6967
left.huff = 0
7068
right.huff = 1
7169

72-
# Combina os 2 nós menores para um novo nó pai
73-
# para eles.
74-
newNode = node(left.freq + right.freq, left.symbol + right.symbol, left, right)
70+
# Combine the two smallest nodes into a new parent node
71+
# for them.
72+
new_node = Node(left.freq + right.freq, left.symbol + right.symbol, left, right)
7573

76-
# remove os 2 nós e adiciona o nó pai
77-
# como um novo só sobre os outros
74+
# Remove the two nodes and add the parent node
75+
# as a new node on top of the others
7876
nodes.remove(left)
7977
nodes.remove(right)
80-
nodes.append(newNode)
78+
nodes.append(new_node)
8179

82-
# Árvore Huffman pronta!
83-
printNodes(nodes[0])
80+
# Huffman Tree ready!
81+
print_nodes(nodes[0])

0 commit comments

Comments
 (0)