-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree.py
More file actions
107 lines (78 loc) · 2.25 KB
/
Tree.py
File metadata and controls
107 lines (78 loc) · 2.25 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import graphviz as gv
class Tree:
"""Class that represents a binary tree"""
symbol=''
C1=None
C2=None
number=-1
GraphStyle = {
'graph': {
'fontname': 'Arial',
'fontsize': '28',
'fontcolor': 'white',
'bgcolor': '#333333',
'rankdir': 'TB',
},
'nodes': {
'fontname': 'Arial',
'fontsize': '28',
'fontcolor': 'white',
'color': 'white',
'style': 'filled',
'fillcolor': '#006699',
},
'edges': {
'color': 'white',
'arrowhead': 'open',
'fontname': 'Arial',
'fontsize': '16',
'fontcolor': 'purple',
}}
def __init__(self, initial):
"""Class constructor"""
self.symbol=initial
#self.C1=Tree
#self.C2=Tree
def fill_2(self,c1,c2):
"""Sets both child nodes of the tree to the tree"""
self.C1=Tree(c1)
self.C2=Tree(c2)
def fill_1(self,c1):
"""Sets the left child of the tree"""
self.C1=Tree(c1)
def printStart(self,caption,name):
"""Start to printing the tree to an image file"""
g=gv.Graph(format='png')
#self.GraphStyle[0]['label']='Nopthing'
g.graph_attr.update(label=caption)
cont=[]
Tree.print_it(self,g,cont)
Tree.apply_styles(g)
#print(g.source)
filename = g.render(filename='img/'+name)
#print(filename)
return filename
def apply_styles(graph):
"""Apllies the visual style to the graphviz tree"""
graph.graph_attr.update(('graph' in Tree.GraphStyle and Tree.GraphStyle['graph']) or {})
graph.node_attr.update(('nodes' in Tree.GraphStyle and Tree.GraphStyle['nodes']) or {})
graph.edge_attr.update(('edges' in Tree.GraphStyle and Tree.GraphStyle['edges']) or {})
return graph
def printxt(self):
"""Prints to console the content of the tree"""
print(self.symbol)
if self.C1 is not None:
self.C1.printxt()
if self.C2 is not None:
self.C2.printxt()
def print_it(node, g, cont):
"""Generates recursively the graphviz graph of the tree"""
g.node(str(len(cont)),node.symbol)
node.number=len(cont)
cont.append('i')
if node.C1 is not None:
g.edge(str(node.number),str(len(cont)))
Tree.print_it(node.C1,g,cont)
if node.C2 is not None:
g.edge(str(node.number),str(len(cont)))
Tree.print_it(node.C2,g,cont)