-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrees_class.py
144 lines (108 loc) · 3.48 KB
/
Trees_class.py
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 23 13:00:12 2018
@author: Tirtha
"""
class Tree:
"""
Recursive definition of Tree class plus various helper functions/methods
"""
def __init__(self, value, children):
"""
Class initializer: Produces a single root node having a specific string value;
children is a list of references to the root of the children branches.
Note th children are trees themselves, hence the recursion.
"""
self._value = value
self._children = children
def strep(self):
"""
Generates a string representation of the tree
"""
text = ""
text+=str(self._value)
for child in self._children:
text+='['
text+=child.strep()
text+=']'
return text
def get_value(self):
return self._value
def children(self):
for child in self._children:
yield child
def num_nodes(self):
result = 1
for child in self._children:
result+=child.num_nodes()
return result
def num_leaves(self):
if len(self._children)==0:
return 1
else:
result=0
for child in self._children:
result += child.num_leaves()
return result
def height(self):
height=0
for child in self._children:
height = max(height,child.height()+1)
return height
tree_a = Tree('a',[])
tree_b = Tree('b',[])
tree_cab = Tree('c',[tree_a,tree_b])
print (tree_a.strep())
print(tree_b.get_value())
print(tree_cab.strep())
tree_4 = Tree('d',[tree_cab,tree_b,tree_a])
print (tree_4.strep())
print(tree_4.num_nodes())
print(tree_4.num_leaves())
print(tree_4.height())
OPERATORS = {
"+":lambda x,y:x+y,
"-":lambda x,y:x-y,
"*":lambda x,y:x*y,
"/":lambda x,y:x/y,
"%":lambda x,y:x%y,
"^":lambda x,y:x**y
}
class Arithmatic (Tree):
def __init__(self, value, children):
Tree.__init__(self, value, children)
def strexp(self):
if len(self._children)==0:
return str(self._value)
else:
text='('
text+=self._children[0].strexp()
text+=str(self._value)
text+=self._children[1].strexp()
text+=')'
return text
def evaluate_exp(self):
if len(self._children)==0:
if ('.' in self._value):
return float(self._value)
else:
return int(self._value)
else:
function = OPERATORS[self._value]
left_value = self._children[0].evaluate_exp()
right_value = self._children[1].evaluate_exp()
return function(left_value,right_value)
#exp1=Arithmatic('*',[])
exp2=Arithmatic('1.2',[])
exp3=Arithmatic('5.2',[])
exp4=Arithmatic('*',[exp2,exp3])
exp5=Arithmatic('4.2',[])
exp6=Arithmatic('7.5',[])
exp7=Arithmatic('+',[exp5,exp6])
exp8=Arithmatic('/',[exp4,exp7])
exp9=Arithmatic('2',[])
exp10=Arithmatic('^',[exp9,exp8])
print("The expression is:",exp10.strexp())
print("The evluated value is:",exp10.evaluate_exp())
print("String representation is:",exp10.strep())
print("Depth of this computation graph tree is:",exp10.height())