|
1 |
| -# Árvore Huffman |
2 |
| -class node: |
| 1 | +# Huffman Tree |
| 2 | +class Node: |
3 | 3 | def __init__(self, freq, symbol, left=None, right=None):
|
4 |
| - # Frequência do Símbolo |
| 4 | + # Symbol frequency |
5 | 5 | self.freq = freq
|
6 | 6 |
|
7 |
| - # Símbolo (caracter) |
| 7 | + # Symbol (character) |
8 | 8 | self.symbol = symbol
|
9 | 9 |
|
10 |
| - # nó à esquerda do nó atual |
| 10 | + # Left node of the current node |
11 | 11 | self.left = left
|
12 | 12 |
|
13 |
| - # nó à direita do nó atual |
| 13 | + # Right node of the current node |
14 | 14 | self.right = right
|
15 | 15 |
|
16 |
| - # direção da árvore (0/1) |
| 16 | + # Tree direction (0/1) |
17 | 17 | self.huff = ""
|
18 | 18 |
|
19 | 19 |
|
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) |
23 | 26 |
|
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 |
32 | 30 | if node.left:
|
33 |
| - printNodes(node.left, newVal) |
| 31 | + print_nodes(node.left, new_val) |
34 | 32 | if node.right:
|
35 |
| - printNodes(node.right, newVal) |
| 33 | + print_nodes(node.right, new_val) |
36 | 34 |
|
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 |
39 | 37 | if not node.left and not node.right:
|
40 |
| - print(f"{node.symbol} -> {newVal}") |
| 38 | + print(f"{node.symbol} -> {new_val}") |
41 | 39 |
|
42 | 40 |
|
43 |
| -# caracteres para à árvore huffman |
| 41 | +# Characters for the Huffman tree |
44 | 42 | chars = ["a", "b", "c", "d", "e", "f"]
|
45 | 43 |
|
46 |
| -# frequência dos caracteres |
| 44 | +# Frequencies of the characters |
47 | 45 | freq = [5, 9, 12, 13, 16, 45]
|
48 | 46 |
|
49 |
| -# lista contendo os nós não utilizados |
| 47 | +# List containing the unused nodes |
50 | 48 | nodes = []
|
51 | 49 |
|
52 | 50 | 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 |
55 | 53 | for x in range(len(chars)):
|
56 |
| - nodes.append(node(freq[x], chars[x])) |
| 54 | + nodes.append(Node(freq[x], chars[x])) |
57 | 55 |
|
58 | 56 | 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 |
61 | 59 | nodes = sorted(nodes, key=lambda x: x.freq)
|
62 | 60 |
|
63 |
| - # Seleciona os dois nós menores |
| 61 | + # Select the two smallest nodes |
64 | 62 | left = nodes[0]
|
65 | 63 | right = nodes[1]
|
66 | 64 |
|
67 |
| - # Atribui um valor direcional à estes nós |
68 |
| - # (direita ou esquerda) |
| 65 | + # Assign a directional value to these nodes |
| 66 | + # (left or right) |
69 | 67 | left.huff = 0
|
70 | 68 | right.huff = 1
|
71 | 69 |
|
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) |
75 | 73 |
|
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 |
78 | 76 | nodes.remove(left)
|
79 | 77 | nodes.remove(right)
|
80 |
| - nodes.append(newNode) |
| 78 | + nodes.append(new_node) |
81 | 79 |
|
82 |
| - # Árvore Huffman pronta! |
83 |
| - printNodes(nodes[0]) |
| 80 | + # Huffman Tree ready! |
| 81 | + print_nodes(nodes[0]) |
0 commit comments