-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtraverse.py
More file actions
66 lines (60 loc) · 1.7 KB
/
traverse.py
File metadata and controls
66 lines (60 loc) · 1.7 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
def get_last_index(name, node_list):
for i in range(len(node_list)):
if name == node_list[i]:
return i
return
def get_children(index, side_list, node_list):
result = []
for i in range(len(side_list)):
if side_list[i][0] == index:
result.append(node_list[side_list[i][1]])
return result
def traverse(nodes, sides):
forks = []
output = []
current = []
if not len(nodes):
return
output.append(nodes[0])
has_no_children = False
while len(output):
next_obj = output[-1]
next_index = get_last_index(next_obj, nodes)
if has_no_children:
has_no_children = False
print([int(item) for item in output])
while len(output) and (
len(forks) <= 0
or output[-1]
!= forks[-1]
):
transfer = output.pop()
if len(forks):
forks.pop()
else:
children = get_children(next_index, sides, nodes)
if len(children):
for i in range(len(children)):
current.append(children[i])
if i:
forks.append(next_obj)
else:
has_no_children = True
continue
if len(current):
transfer = current.pop()
output.append(transfer)
nodes = [str(item + 1) for item in range(0, 8)]
sides = [[0, 2],
[0, 1],
[1, 4],
[1, 3],
[2, 5],
[3, 7],
[4, 7],
[4, 6],
[4, 5],
[4, 3],
[5, 6],
[6, 7]]
traverse(nodes, sides)