forked from tearf001/Training-Python-Public
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBiTree.py
More file actions
executable file
·123 lines (100 loc) · 2.95 KB
/
BiTree.py
File metadata and controls
executable file
·123 lines (100 loc) · 2.95 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
""" Implementing binary trees as embedded list
"""
def BiTree(data, left, right):
return [data, left, right]
def is_empty_BiTree(bitree):
return bitree == []
def root(bitree):
return bitree[0]
def leftch(bitree):
return bitree[1]
def rightch(bitree):
return bitree[2]
def set_root(bitree, data):
bitree[0] = data
def set_leftch(bitree, left):
bitree[1] = left
def set_rightch(bitree, right):
bitree[2] = right
###############################################
#### Functions for ############################
#### building and manipulating ################
#### mathematical expressions ################
from numbers import Number
def make_sum(a, b):
return ['+', a, b]
def make_prod(a, b):
return ['*', a, b]
def make_diff(a, b):
return ['-', a, b]
def make_div(a, b):
return ['/', a, b]
def is_basic_exp(a):
return not isinstance(a, list)
def is_compose_exp(a):
return isinstance(a, list)
def eval_exp(e):
if is_basic_exp(e):
return e
op, a, b = e[0], eval_exp(e[1]), eval_exp(e[2])
if op == '+':
return eval_sum(a, b)
elif op == '-':
return eval_diff(a, b)
elif op == '*':
return eval_prod(a, b)
elif op == '/':
return eval_div(a, b)
else:
raise ValueError("Unknown operator:", op)
def eval_sum(a, b):
if isinstance(a, Number) and isinstance(b, Number):
return a + b
if isinstance(a, Number) and a == 0:
return b
if isinstance(b, Number) and b == 0:
return a
return make_sum(a, b)
def eval_diff(a, b):
if isinstance(a, Number) and isinstance(b, Number):
return a - b
if isinstance(a, Number) and a == 0:
return -b
if isinstance(b, Number) and b == 0:
return a
return make_diff(a, b)
def eval_prod(a, b):
if isinstance(a, Number) and isinstance(b, Number):
return a * b
if (isinstance(a, Number) and a == 0 or
isinstance(b, Number) and b == 0):
return 0
if isinstance(a, Number) and a == 1:
return b
if isinstance(b, Number) and b == 1:
return a
return make_prod(a, b)
def eval_div(a, b):
if isinstance(a, Number) and isinstance(b, Number):
return a / b
if isinstance(a, Number) and a == 0:
return 0
if isinstance(b, Number) and b == 0:
raise ZeroDivisionError
if isinstance(b, Number) and b == 1:
return a
return make_div(a, b)
if __name__ == '__main__':
t1 = BiTree(2, BiTree(4, [], []), BiTree(8, [], []))
print(t1)
# set_leftch(leftch(t1), BiTree(5, [], []))
# print(t1)
#
# e1 = make_prod(make_sum(2, 3), make_diff(4, 5))
# e2 = make_prod(make_diff(make_prod(2, 'a'), 3), make_diff(4, 5))
# e3 = make_div(make_sum(make_prod(2, 7), make_div(0, 'b')), make_div('a', 1))
#
# ret = eval_exp(['+', 2, 3])
# print(ret)
# eval_exp(['$', 2, 3]) # This will cause an exception because $ is not a valid operator
#