-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBreadthFirstSearch.py
More file actions
34 lines (28 loc) · 877 Bytes
/
BreadthFirstSearch.py
File metadata and controls
34 lines (28 loc) · 877 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
'''
Breadth First Search:
Given a tree as an input, perform a BFS traversal, adding
each nodes value along the way to an array. Return the array.
Time: O(V+E)
Space: O(V)
Last Practiced: 2022-03-22 06:24:41
'''
# Do not edit the class below except
# for the breadthFirstSearch method.
# Feel free to add new properties
# and methods to the class.
class Node:
def __init__(self, name):
self.children = []
self.name = name
def addChild(self, name):
self.children.append(Node(name))
return self
def breadthFirstSearch(self, array):
queue = []
queue.append(self)
while len(queue):
currentNode = queue.pop(0)
array.append(currentNode.name)
for child in currentNode.children:
queue.append(child)
return array