-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfs.py
executable file
·51 lines (45 loc) · 1.22 KB
/
dfs.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
#!/usr/bin/env python
class Node:
def __init__(self, name, children):
self.name = name
self.children = children
def __repr__(self):
return self.name
def recursiveTree(node):
print node.name
isNone = node.children is None
if(isNone):
return
for n in node.children:
recursiveTree(n)
def size(children):
return 0 if children is None else len(children)
def stackTree(node):
stack = [node]
visited = set([])
while(len(stack) > 0):
cur_node = stack[-1]
#print cur_node.name
cur_children = cur_node.children
pushed = False
for i in range(0, size(cur_children)):
if not cur_children[i] in visited:
stack.append(cur_children[i])
pushed = True
if cur_children[i].children is None:
print "stack : ", stack
break
if not pushed:
visited.add(stack.pop())
def main():
a = Node("a", None)
g = Node("g", None)
e = Node("e", None)
f = Node("f", None)
b = Node("b", [a,g])
d = Node("d", [e,f])
c = Node("c", [b,d])
#recursiveTree(c)
stackTree(c)
if __name__ == "__main__":
main()