-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
parent_child_tree.py
57 lines (51 loc) · 1.72 KB
/
parent_child_tree.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import import_ipynb
from rotate_translate import node
def writetofile(filename, string):
file = open(filename, "a")
file.write(string)
file.close()
def createtree(nodes, filename):
if(len(nodes) == 0):
print("No shapes detected")
return
subtree = []
string = ''
present = node()
present.left = nodes[0]
nodes[0].name()
present.string = nodes[0].string
if(len(nodes) == 1):
writetofile(filename, present.string)
for i, object in enumerate(nodes[1:]):
object.name()
if(object.operation != 'None'):
if(present.right == 'None'):
present.right = object
present.string = object.operation + "() {\n\t" + present.string + '\n\t' + object.string + '}\n'
string = present.string
else:
temp = present
present = node()
present.left = temp
present.string = temp.string
present.right = object
present.string = object.operation + "() {\n\t" + present.left.string + '\n\t' + object.string + '}\n'
string = present.string
else:
if(present.right == 'None'):
writetofile(filename, present.string)
subtree.append(present)
writetofile(filename, string)
string = ''
present = node()
present.left = object
present.string = object.string
if(i == len(nodes)-2):
writetofile(filename, present.string)
if(string != ''):
writetofile(filename, string)
subtree.append(present)
print("Subtree:", len(subtree))