huffman-py/huffman_py/gui/generate_node.py

37 lines
1.4 KiB
Python
Raw Normal View History

2023-05-25 00:39:32 +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/>.
#
import graphviz
2023-05-25 02:05:35 +02:00
def generate_node(graph, node, father = None):
if node.left and node.right:
# not a leaf
graph.node(str(node.identifier), label = str(node.occurrence) , style = "filled", fillcolor = "red", shape="circle")
2023-05-25 00:39:32 +02:00
else:
2023-05-25 02:05:35 +02:00
# a leaf
graph.node(str(node.identifier), label = graphviz.escape(str(node.occurrence)+'\n'+str(node.char)), style = "filled", fillcolor = "green")
2023-05-25 00:39:32 +02:00
# https://graphviz.readthedocs.io/en/stable/api.html#graphviz.escape
if father != None:
2023-05-25 02:05:35 +02:00
graph.edge(str(father), str(node.identifier), label=str(node.code))
2023-05-25 00:39:32 +02:00
2023-05-25 02:05:35 +02:00
if node.left:
generate_node(graph, node.left, node.identifier)
if node.right:
generate_node(graph, node.right, node.identifier)
2023-05-25 00:39:32 +02:00