-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_level.py
More file actions
53 lines (47 loc) · 954 Bytes
/
graph_level.py
File metadata and controls
53 lines (47 loc) · 954 Bytes
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
from stack import Stack
from queue import Queue
class Node(object):
def __init__(self, data):
self.data = data
self.nodes = []
def __repr__(self):
return "({})".format(self.data)
def create_ll(node, depth_lists, level = 0):
if not node:
return
if not level in depth_lists:
depth_lists[level] = []
depth_lists[level].append(node.data)
for child_node in node.nodes:
create_ll(child_node, depth_lists, level + 1)
def bfs(node):
q = Queue()
q.push(node)
while not q.is_empty():
n = q.pop()
print(n.data)
for cn in n.nodes:
q.push(cn)
def main():
root = Node(1)
a = Node(2)
b = Node(3)
c = Node(4)
d = Node(5)
e = Node(6)
f = Node(7)
g = Node(8)
h = Node(9)
i = Node(10)
j = Node(11)
k = Node(12)
root.nodes = [a, b, c, d]
b.nodes = [e, f, g]
c.nodes = [h, i]
i.nodes = [j]
j.nodes = [k]
main_depth_lists = {}
create_ll(root, main_depth_lists)
print(main_depth_lists)
if __name__ == "__main__":
main()