Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

breadth first search python

"""
breadFirstSearch method traverses a tree using
the Breadth-first search approach storing
its node values in an array and then
returns the resulting array.
"""
class Node:
    def __init__(self, value):
        self.value = value
        self.children = []

    def breadFirstSearch(self, array):
        queue = [self]
        while len(queue) > 0:
            current = queue.pop(0)
            array.append(current.value)
            for child in current.children:
                queue.append(child)
        return array
        
 
PREVIOUS NEXT
Tagged: #breadth #search #python
ADD COMMENT
Topic
Name
3+2 =