-
Notifications
You must be signed in to change notification settings - Fork 0
/
ter_exp_to_bin.py
49 lines (43 loc) · 912 Bytes
/
ter_exp_to_bin.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
# SHIV's code for converting ternary exp to binary tree
'''
3
a?b:c
a?b?c:d:e
a?b?c:d?e:f:g?h:i
'''
tn = int(input())
class node:
def __init__(self, name):
self.name = name
self.L= None
self.R = None
self.count = 0
def build_tree(tree):
stack = []
for c in tree:
if c == '?': continue
if c == ':':
old_head = stack[-1]
del stack[-1]
stack[-1].count+=1
if stack[-1].count == 1:
stack[-1].L = old_head
elif stack[-1].count == 2:
stack[-1].R = old_head
del stack[-1]
stack[-1].count+=1
while stack[-1].count == 2:
del stack[-1]
stack[-1].count+=1
else:
curr_node = node(c)
stack.append(curr_node)
while stack[-1].count == 2 or stack[-1].count == 0:
del stack[-1]
if not stack: break
stack[-1].count+=1
print([(x.name, x.count) for x in stack])
for i in range(tn):
tree = input()
root = build_tree(tree)
print('----------------')