1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
import uuid, json
import graphviz as gv
from pprint import pformat
class Nodo:
def __init__(self, dato = None):
self.dato = dato
self.hijos = []
def print(self, n = 0):
s = ' ' * n + 'Nodo:' + "\n"
s += ' ' * n + "dato = " + str(self.dato) + "\n"
s += ' ' * n + "hijos =\n"
for h in self.hijos:
s += h.print(n + 1)
s += "\n"
return s
def render(self, dot: gv.Digraph, parent: str):
name = uuid.uuid1().hex
fdato = pformat(self.dato, indent=2).replace('\n', '\l')
dot.node(name, fdato)
if parent:
dot.edge(parent, name)
for h in self.hijos:
h.render(dot, name)
def __str__(self):
return self.print()
class Arbol:
def __init__(self, raiz: Nodo = Nodo()):
self.raiz = raiz
def render(self, filename, view = False):
dot = gv.Digraph()
dot.attr(rankdir='LR')
dot.attr('node', fontname='monospace')
dot.attr('node', shape='box')
self.raiz.render(dot, None)
dot.render(filename, view = view)
def __str__(self):
if self.raiz:
return str(self.raiz)
return "None"
|