-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrep.py
102 lines (89 loc) · 2.62 KB
/
rep.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
from collections import deque
class Node:
def __init__(self):
self.children = []
self.state = None
self.o = None
self.c = None
def addChild(self, child):
self.children.append(child)
def __len__(self):
return 1 + sum(len(c) for c in self.children)
def louds(self, sep=''):
print('10', end=sep)
dq = deque((self,))
while len(dq) > 0:
v = dq.popleft()
for c in v.children:
print('1', end='')
dq.append(c)
print('0', end=sep)
print()
def bp(self, sep=''):
dq = deque((self,))
while len(dq) > 0:
v = dq.pop()
if v.state == None:
print('(', end='')
v.state = 'open'
dq.append(v)
for c in reversed(v.children):
dq.append(c)
elif v.state == 'open':
print(')', end=sep)
v.state = None
print()
def dfuds(self, sep=''):
dq = deque((self,))
print('(', end=sep)
while len(dq) > 0:
v = dq.pop()
for c in reversed(v.children):
print('(', end='')
dq.append(c)
print(')', end=sep)
print()
def format(self):
x = 0
dq = deque((self,))
while len(dq) > 0:
v = dq.pop()
if v.state == None:
v.state = 'open'
v.o = x
x += 1
dq.append(v)
for c in reversed(v.children):
dq.append(c)
elif v.state == 'open':
v.state = None
v.c = x
x += 1
def printW(v):
print('(', end='')
print(' ' * (v.c - v.o - 1), end='')
print(')', end='')
def printV(v):
x = int((v.c + v.o) / 2) - v.o
print(',' * x, end='')
print('()', end='')
print('.' * x, end='')
cl = 0
dq = deque((self,))
while len(dq) > 0:
v = dq.popleft()
if v.c < cl:
print()
cl = 0
print(' ' * (v.o - cl), end='')
printV(v)
cl = v.c + 1
for c in v.children:
dq.append(c)
print()
@staticmethod
def fromParens(parens):
root = Node()
for c in parens:
root.addChild(Node.fromParens(c))
return root