-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbfs_traversal.py
57 lines (45 loc) · 1.19 KB
/
bfs_traversal.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
# A node structure
class Node:
# A utility function to create a new node
def __init__(self, key):
self.data = key
self.left = None
self.right = None
# height of a binary tree
def printLevelOrder(root):
"""
prints the level order traversal of a tree
>>> root = Node(1)
>>> root.left = Node(2)
>>> root.right = Node(3)
>>> root.left.left = Node(4)
>>> root.left.right = Node(5)
>>> root.right.left = Node(6)
>>> root.right.right = Node(7)
>>> printLevelOrder(root)
[1, 2, 3, 4, 5, 6, 7]
"""
# Base Case
if root is None:
return
# Create an empty queue
# for level order traversal
queue = []
# Enqueue Root and initialize height
queue.append(root)
res = []
while len(queue) > 0:
# Print front of queue and
# remove it from queue
res.append(queue[0].data)
node = queue.pop(0)
# Enqueue left child
if node.left is not None:
queue.append(node.left)
# Enqueue right child
if node.right is not None:
queue.append(node.right)
print(res)
if __name__ == "__main__":
import doctest
doctest.testmod()