# # 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 . # def calculate_code(node, value = '', the_codes = None): # Create code Dict if it doesn't exist yet (before recursion) if the_codes == None: the_codes = dict() # handle the case with a single node (0 as default value) if(not node.left and not node.right): the_codes[node.char] = "0" return the_codes # actual node code newValue = value + str(node.code) if(node.left): calculate_code(node.left, newValue, the_codes) if(node.right): calculate_code(node.right, newValue, the_codes) if(not node.left and not node.right): the_codes[node.char] = newValue return the_codes