37 lines
1.5 KiB
Python
37 lines
1.5 KiB
Python
|
#
|
||
|
# 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
|
||
|
|
||
|
def generate_node(graph, sommet, father = None):
|
||
|
if sommet.left and sommet.right:
|
||
|
#pas une feuille
|
||
|
graph.node(str(sommet.identifiant), label = str(sommet.occurrence) , style = "filled", fillcolor = "red", shape="circle")
|
||
|
else:
|
||
|
#une feuille
|
||
|
graph.node(str(sommet.identifiant), label = graphviz.escape(str(sommet.occurrence)+'\n'+str(sommet.lettres)), style = "filled", fillcolor = "green")
|
||
|
# https://graphviz.readthedocs.io/en/stable/api.html#graphviz.escape
|
||
|
|
||
|
if father != None:
|
||
|
graph.edge(str(father), str(sommet.identifiant), label=str(sommet.code))
|
||
|
|
||
|
if sommet.left:
|
||
|
generate_node(graph, sommet.left, sommet.identifiant)
|
||
|
if sommet.right:
|
||
|
generate_node(graph, sommet.right, sommet.identifiant)
|
||
|
|