-
Notifications
You must be signed in to change notification settings - Fork 1
/
BFSDFS.py
85 lines (69 loc) · 2.11 KB
/
BFSDFS.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
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
82
83
84
85
import sys
def get_breadth_first_nodes(root):
nodes = []
stack = [root]
while stack:
cur_node = stack[0]
stack = stack[1:]
nodes.append(cur_node)
for child in cur_node.get_children():
stack.append(child)
return nodes
def get_depth_first_nodes(root):
nodes = []
stack = [root]
while stack:
cur_node = stack[0]
stack = stack[1:]
nodes.append(cur_node)
for child in cur_node.get_rev_children():
stack.insert(0, child)
return nodes
########################################################################
class Node(object):
def __init__(self, id_):
self.id = id_
self.children = []
def __repr__(self):
return "Node: [%s]" % self.id
def add_child(self, node):
self.children.append(node)
def get_children(self):
return self.children
def get_rev_children(self):
children = self.children[:]
children.reverse()
return children
########################################################################
def println(text):
sys.stdout.write(text + "\n")
def make_test_tree():
a0 = Node("a0")
b0 = Node("b0")
b1 = Node("b1")
b2 = Node("b2")
c0 = Node("c0")
c1 = Node("c1")
d0 = Node("d0")
a0.add_child(b0)
a0.add_child(b1)
a0.add_child(b2)
b0.add_child(c0)
b0.add_child(c1)
c0.add_child(d0)
return a0
def test_breadth_first_nodes():
root = make_test_tree()
node_list = get_breadth_first_nodes(root)
for node in node_list:
println(str(node))
def test_depth_first_nodes():
root = make_test_tree()
node_list = get_depth_first_nodes(root)
for node in node_list:
println(str(node))
########################################################################
if __name__ == "__main__":
test_breadth_first_nodes()
println("")
test_depth_first_nodes()