41 lines
1.6 KiB
Python
Raw Normal View History

2023-05-25 00:39:59 +02:00
#
# Sam Hadow - Huffman-py
# Copyright (C) 2023
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
def calcul_code(node, value = '', les_codes = None):
# on crée le dict des codes s'il n'existe pas encore (lors du premier passage dans la fonction)
if les_codes == None:
les_codes = dict()
# gérer le cas où on a qu'un seul noeud, on lui attribuera le code 0 par défaut
# à noter que dans ce cas là l'algorithme d'Huffman pour la compression est peu utile
if(not node.left and not node.right):
les_codes[node.lettres] = "0"
return les_codes
#code pour le sommet actuel
newValue = value + str(node.code)
if(node.left):
calcul_code(node.left, newValue, les_codes)
if(node.right):
calcul_code(node.right, newValue, les_codes)
if(not node.left and not node.right):
les_codes[node.lettres] = newValue
return les_codes