-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTree.py
77 lines (54 loc) · 1.82 KB
/
BinaryTree.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
# node OP (T) (T) donde T puede ser lo mismo que escribi primero o "Leaf D" siendo D un numero
# OP = sum | res | div | mul
#
# Ejemplo:
# Node MUL (node sum (Leaf 49) (Leaf 104)) (Leaf 38)
#
from JLolibrary import Node
simbols = {
'SUM': '+',
'RES': '-',
'MUL': 'x',
'DIV': '÷'
}
def createNode(line):
parent = Node(simbols[line[5:8].upper()])
left_start = line.index('(')
left_end = left_start
left_stack = []
for c in line[left_start: ]:
if c == '(':
left_stack.append(1)
elif c == ')':
left_stack.pop()
if len(left_stack) == 0:
break
left_end += 1
if(line[left_start + 1 : left_start + 5] == 'Leaf'):
parent.left = Node(int(line[left_start + 6 : left_end]))
else:
parent.left = createNode(line[left_start + 1 : left_end])
right_start = line.index('(', left_end)
right_end = right_start
right_stack = []
for c in line[right_start: ]:
if c == '(':
right_stack.append(1)
elif c == ')':
right_stack.pop()
if len(right_stack) == 0:
break
right_end += 1
if(line[right_start + 1 : right_start + 5] == 'Leaf'):
parent.right = Node(line[right_start + 6 : right_end])
else:
parent.right = createNode(line[right_start + 1 : right_end])
# print("(%s, %s)" % (left_start, left_end), '(%s, %s)' % (right_start, right_end))
# print(root, '|', line[left_start + 1 : left_end], '|', line[right_start + 1 : right_end])
return parent
if __name__ == '__main__':
#line = "Node MUL (node SUM (Leaf 49) (Leaf 104)) (Leaf 38)"
line = "Node MUL (Node SUM (Node DIV (Leaf 16) (Leaf 4)) (Leaf 5)) (Node RES (Leaf 3) (Leaf 5))"
print(line)
tree = createNode(line)
print(tree)