#!/usr/bin/env python3 import subprocess class Noeud(object): def __init__(self,p=0,left=None,right=None,code='',name=''): self.left = left self.right= right if left != None and right != None : self.p = left.p + right.p self.name =left.name+right.name else: self.p = p self.name = name self.code = code def __lt__(self,other): return self.p 2: queue.sort() print(queue) l=queue.pop(0) r=queue.pop(0) queue.append(Noeud(left=l,right=r)) root= Noeud(left=queue[0],right=queue[1]) return root root_node= create_tree(table_noeud) print(root_node) def gen_code(node,prefix=''): if node.left != None: node.code = prefix gen_code(node.left,prefix+'0') gen_code(node.right,prefix+'1') else: node.code = prefix print(node.name,node.code) gen_code(root_node) def draw_tree(node): if len(node.name) == 1: # feuille desc ='N{} [label="{}:{}", fontcolor=blue, fontsize=16, width=2, shape=box];\n'.format(node.code, node.name, node.code) else: desc = 'N{} [label="{}"];\n'.format(node.code,node.code) desc += draw_tree(node.left) desc += draw_tree(node.right) desc += 'N{}:n -> N{}:e;\n'.format(node.code,node.left.code) desc += 'N{}:s -> N{}:e;\n'.format(node.code,node.right.code) return desc with open('graph.dot','w') as f: f.write('digraph G {\n ') f.write(' splines=ortho \n') f.write('rankdir=RL;\n') f.write(draw_tree(root_node)) f.write('{rank =same; N' + '; N'.join([n.code for n in table_noeud]) +';}\n') f.write('}') subprocess.call('dot -Tpng graph.dot -o graph.png', shell=True) print('done')