-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarytree.py
More file actions
46 lines (33 loc) · 1016 Bytes
/
binarytree.py
File metadata and controls
46 lines (33 loc) · 1016 Bytes
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
from bigtree import DAGNode, dag_to_dot
import intopostfix
regex = "(a|b)*.a.b.b.#"
postRegex = intopostfix.infix_to_postfix(regex)
#regex = "ab|*ac.*|#."
def bintreeIMG(regex):
stack = []
output = []
count = 1
count2 = 1
for i in regex:
if i.isalpha() or i == '#':
stack.append(DAGNode(i+str(count)))
count += 1
elif i == '|' or i== '.':
a = stack.pop()
b = stack.pop()
newNode = DAGNode(i+str(count2))
a.parents = [newNode]
b.parents = [newNode]
stack.append(newNode)
output.append(newNode)
elif i == '*':
a = stack.pop()
newNode = DAGNode(i+str(count2), children=[a])
stack.append(newNode)
output.append(newNode)
else:
output.append(DAGNode(i))
count2 += 1
graph = dag_to_dot(output[-1], node_colour="gold")
graph.write_png("Syntax Tree.png")
bintreeIMG(postRegex)