forked from holonking/rhinoscript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_print_tree.py
More file actions
81 lines (68 loc) · 2.23 KB
/
test_print_tree.py
File metadata and controls
81 lines (68 loc) · 2.23 KB
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
# T='├'
# I='│'
# L='└'
# D='─'
SPACE=' '
import os
class Node:
def __init__(self,name='Node',parent=None,children=[],level=0):
self.name=name
self.parent=None
self.children=[]
#Should not set level manually!
self.level=level
if parent is not None:
self.set_parent(parent)
if children is not None:
if children is list:
if len(children>0):
self.set_children(children)
def print_node(self,is_end_node=True,starting_node=None):
prefix=''
parent=self.parent
if parent is not None and starting_node is not None:
gparent=parent.parent
end_condition=[]
while gparent is not None:
if parent==starting_node:break
i=gparent.children.index(parent)
if i==len(gparent.children)-1:
end_condition.append(True)
else: end_condition.append(False)
parent=gparent
gparent=parent.parent
end_condition.append(True)
end_condition.reverse()
for c in end_condition:
if not c: prefix+=I+SPACE
else :prefix+=' '+SPACE
# for i in range(self.level):
# prefix+=I+SPACE
leader=L if is_end_node else T
txt=prefix+leader+self.name
print (txt)
for i in range(len(self.children)):
child=self.children[i]
is_end=True if i>=len(self.children)-1 else False
child.print_node(is_end_node=is_end,strating_node=starting_node)
def set_parent(self,parent):
self.parent=parent
parent.add_child(self)
#update children level
for c in self.children:
c.level=self.level+1
def set_children(self,children):
self.children+=children
for c in self.children:
c.level=self.level+1
def add_child(self,child):
self.children.append(child)
child.level=self.level+1
A=Node('Node_A')
B=Node('Node_B',parent=A)
C=Node('Node_C',parent=A)
D=Node('Node_D',parent=B)
E=Node('Node_E',parent=B)
F=Node('Node_F',parent=B)
G=Node('Node_G',parent=C)
A.print_node()