コード例 #1
0
def test_dequeue():
    q = Queue()
    q.enqueue('apple')
    q.dequeue()
    actual = q.is_empty()
    expected = True
    assert actual == expected
コード例 #2
0
    def add(self, val: any) -> None:
        """Add a new node with Breadth first approach

        Args:
            val (any): Value to add
        """
        new_node = Node(val)
        if self.root:
            q = Queue()
            q.enqueue(self.root)

            while not q.is_empty():
                node = q.dequeue()

                if node.left:
                    q.enqueue(node.left)
                else:
                    node.left = new_node
                    return

                if node.right:
                    q.enqueue(node.right)
                else:
                    node.right = new_node
                    return

        else:
            self.root = new_node
コード例 #3
0
    def breadth_first(self, start: Vertex) -> set:
        """Traverse a graph in breadth-first manner and return a set of Vertices

        Args:
            start (Vertex): Starting point of the traversal

        Raises:
            KeyError: If provided vertex is not part of the graph

        Returns:
            set: Set of vertices
        """
        q = Queue()
        output = set()

        if not start in self.adjacency_list:
            raise KeyError('Given Vertex is not part of the Graph')

        q.enqueue(start)

        while not q.is_empty():
            vertex = q.dequeue()
            output.add(vertex)

            for neighbor in self.get_neighbors(vertex):
                if neighbor['vertex'] not in output:
                    q.enqueue(neighbor['vertex'])

        return output
def test_can_empty_queue():
    queue = Queue()
    queue.enqueue(1)
    queue.enqueue(1)
    queue.dequeue()
    queue.dequeue()
    actual = queue.is_empty()
    expected = True
    assert actual == expected
コード例 #5
0
def test_exhausted():
    #  TODO TODO is empty should return true after dequeueing all previously queued items
    q = Queue()
    q.enqueue('apples')
    q.enqueue('oranges')
    q.dequeue()
    q.dequeue()

    actual = q.is_empty()
    expected = True
    assert actual == expected
コード例 #6
0
    def breadth_first(self, vertex):
        node = []
        breadth = Queue()

        breadth.enqueue(vertex)
        visited = set()

        while not breadth.is_empty():
            front = breadth.dequeue()
            node.append(front.value)

            for neighbor in self.get_neighbor(front):
                if neighbor.vertex.value not in visited:
                    visited.add(neighbor.vertex.value)
                    breadth.enqueue(neighbor.vertex)
        return node
コード例 #7
0
    def breadth_first(self) -> list:
        """Traverse the tree in breadth-first manner

        Returns:
            list: Values of the tree
        """
        output = []
        if self.root:
            q = Queue()
            q.enqueue(self.root)

            while not q.is_empty():
                el = q.dequeue()
                output.append(el.val)

                if el.left:
                    q.enqueue(el.left)
                if el.right:
                    q.enqueue(el.right)

        return output
コード例 #8
0
def test_is_empty():
    #  TODO is empty should return true if no items were enqueued
    q = Queue()
    actual = q.is_empty()
    expected = True
    assert actual == expected