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
Ejemplo n.º 2
0
def test_dequeue_when_empty():
    # TODO it should raise an exception when dequeueing an empty queue
    q = Queue()
    with pytest.raises(InvalidOperationError) as e:
        q.dequeue()

    assert str(e.value) == "Method not allowed on empty collection"
Ejemplo n.º 3
0
def fizz_buzz_tree(tree: object) -> object:
    """Traverse the given k-ary tree and replace its values in FizzBuzz manner

    Args:
        tree (object): k-ary tree

    Returns:
        object: new k-ary tree 
    """
    output = Tree()

    if tree.root:
        q1 = q2 = Queue()

        q1.enqueue(tree.root)
        output.root = Node(fizz_buzz(tree.root.val))
        q2.enqueue(output.root)

        while not q1.is_empty():
            el1, el2 = q1.dequeue(), q2.dequeue()

            for child in el1.children:
                q1.enqueue(child)
                new_el = Node(fizz_buzz(child.val))
                el2.children.append(new_el)
                q2.enqueue(new_el)

    return output
Ejemplo n.º 4
0
def test_dequeue_when_full():
    q = Queue()
    q.enqueue("apples")
    q.enqueue("bananas")
    actual = q.dequeue()
    expected = "apples"
    assert actual == expected
Ejemplo n.º 5
0
def test_queue_peek():
    value = 1
    node = Node(value)
    queue = Queue(node)
    actual = queue.peek()
    expected = value
    assert actual == expected
def test_dequeue_one():
    queue = Queue()
    queue.enqueue(2)
    queue.enqueue(1)
    actual = queue.dequeue()
    expected = 2
    assert actual == expected
Ejemplo n.º 7
0
def test_dequeue():
    q = Queue()
    q.enqueue('apple')
    q.dequeue()
    actual = q.is_empty()
    expected = True
    assert actual == expected
Ejemplo n.º 8
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
Ejemplo n.º 9
0
def test_queue_enqueue():
    value = 1
    queue = Queue()
    queue.enqueue(value)
    expected = value
    actual = queue.front.value
    assert actual == expected
Ejemplo n.º 10
0
def test_enqueue_two():
    q = Queue()
    q.enqueue("apples")
    q.enqueue("bananas")
    actual = q.peek()
    expected = "apples"
    assert actual == expected
def test_can_peek_queue():
    queue = Queue()
    queue.enqueue(1)
    queue.enqueue(2)
    actual = queue.peek()
    expected = 1
    assert actual == expected
Ejemplo n.º 12
0
def test_queue_dequeue():
    value = 1
    queue = Queue()
    queue.enqueue(value)
    actual = queue.dequeue()
    expected = value
    assert actual == expected
def test_enqueue_multiple():
    queue = Queue()
    queue.enqueue(3)
    queue.enqueue(2)
    queue.enqueue(1)
    actual = queue.rear.value
    expected = 1
    assert actual == expected
Ejemplo n.º 14
0
def test_peek():
    q = Queue()
    q.enqueue("apple")
    q.enqueue("banana")
    q.enqueue("cucumber")
    actual = q.peek()
    expected = "apple"
    assert actual == expected
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
Ejemplo n.º 16
0
def test_queue_enqueue_many():
    value1 = 1
    value2 = 2
    queue = Queue()
    queue.enqueue(value1)
    queue.enqueue(value2)
    expected = value1
    actual = queue.front.value
    assert actual == expected
Ejemplo n.º 17
0
def test_queue_dequeue_until_empty():
    value1 = 1
    value2 = 2
    queue = Queue()
    queue.enqueue(value1)
    queue.enqueue(value2)
    queue.dequeue()
    queue.dequeue()
    actual = queue.front
    expected = None
    assert actual == expected
Ejemplo n.º 18
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
Ejemplo n.º 19
0
def test_peek_post_dequeue():
    # TODO it should return a new first value if peek is called after a dequeue
    q = Queue()
    q.enqueue('apples')
    q.enqueue('oranges')
    actual = q.peek()
    expected = 'apples'
    assert actual == expected
    q.dequeue()
    actual = q.peek()
    expected = 'oranges'
    assert actual == expected
Ejemplo n.º 20
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
    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
def tree_intersection(bt1: object, bt2: object) -> set:
    output = set()
    q1 = q2 = Queue()

    if bt1.root and bt2.root:
        q1.enqueue(bt1.root)
        q2.enqueue(bt2.root)

    while not (q1.is_empty() and q2.is_empty()):
        el1, el2 = q1.dequeue(), q2.dequeue()

        if el1.val == el2.val:
            output.add(el1.val)

        if el1.left and el2.left:
            q1.enqueue(el1.left)
            q2.enqueue(el2.left)

        if el1.right and el2.right:
            q1.enqueue(el1.right)
            q2.enqueue(el2.right)

    return output
def test_multiple_enqueue_fails():
    queue = Queue()
    queue.enqueue(3)
    queue.enqueue(2)
    queue.enqueue(1)
    assert queue.rear.value != 2
Ejemplo n.º 24
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
Ejemplo n.º 25
0
def test_queue_peek_empty_exception():
    queue = Queue()
    with pytest.raises(Exception):
        queue.peek()
Ejemplo n.º 26
0
def test_enqueue():
    q = Queue()
    q.enqueue("apple")
    actual = q.front.value
    expected = "apple"
    assert actual == expected
def test_instantiate_queue():
    queue = Queue()
    assert queue.front == None
def test_enqueue_into_queue():
    queue = Queue()
    queue.enqueue(1)
    actual = queue.rear.value
    expected = 1
    assert actual == expected
Ejemplo n.º 29
0
 def __init__(self):
     self.cats = Queue()
     self.dogs = Queue()
     self.animal_order = Queue()
def test_raise_exception_empty_queue():
    queue = Queue()
    with pytest.raises(InvalidOperationError) as e:
        queue.peek()

    assert str(e.value) == "Peeking from an empty queue!"