Beispiel #1
0
class BFS:
    """
    Breath first search.
    """

    def __init__(self):
        self.__q = Queue()
        self.__list_bfs_val = []
        self.__list_bfs_node = []

    def bfs(self, node):
        def inner_bfs(inner_node):
            """ Algorithm
            1. Put all of the children of the node into a queue.
            2. Save the node content into array list.
            3. Recursive BFS algorithm by using the first of queue head node.
            4. Got an sequence of BFS order.

            :param inner_node: Now we're visiting node.
            """

            if inner_node.left:
                self.__q.enqueue(inner_node.left)
            if inner_node.right:
                self.__q.enqueue(inner_node.right)

            self.__list_bfs_val.append(inner_node.data)
            self.__list_bfs_node.append(inner_node)

            while not self.__q.is_empty():
                inner_bfs(self.__q.dequeue())

        self.__init__()
        inner_bfs(node)

    @property
    def bfs_list(self):
        return self.__list_bfs_val

    @property
    def bfs_node_list(self):
        return self.__list_bfs_node
Beispiel #2
0
 def __init__(self):
     self.__q = Queue()
     self.__list_bfs_val = []
     self.__list_bfs_node = []