Beispiel #1
0
    def breadth_first(self, starting_node, action_function=None):
        list_of_nodes = []
        breadth_queue = Queue()
        breadth_queue.enqueue(starting_node)
        starting_node.visited = True
        i = 1

        while not breadth_queue.is_empty():
            print(f"trip {i} through while loop")
            i += 1
            current_vertex = breadth_queue.dequeue()
            list_of_nodes.append(current_vertex)
            current_edges = self._adjacency_list[current_vertex]
            for edge in current_edges:
                if edge.vertex.visited == False:
                    breadth_queue.enqueue(edge.vertex)
                    edge.vertex.visited = True

        # NOTE: Doing this traversal method over the weekend, I choose to give each Vertex an attribute of visited that had a value of True or False.  During class on Monday, JB did say that some are set up this way.  However, he said he prefers to keep track of the visited nodes by using a variable of type set.  This prevents any possibility of leaving an item visited attribute as true.  The set variable would be cleared to length 0 at the start of each breadth traversal.
        for node in self._adjacency_list:
            node.visted = False

        list_of_values_of_nodes = list(map(lambda x: x.value, list_of_nodes))
        # attempts to get the passed in function to act on the list_of_nodes and thereby append to the list existing in the space from where this method was called is on the next line.
        # map(action_function(list_of_nodes))

        # return list_of_nodes
        return list_of_values_of_nodes
def test_peek_queue():
    q3 = Queue()
    q3.enqueue(1)
    q3.enqueue(3)
    q3.enqueue(5)
    q3.enqueue(7)
    assert q3.peek() == 1
    assert not q3.is_empty()
def test_exhausted():
    q = Queue()
    q.enqueue("apple")
    q.enqueue("banana")
    q.enqueue("cucumber")
    q.dequeue()
    q.dequeue()
    q.dequeue()
    actual = q.is_empty()
    expected = True
    assert actual == expected
Beispiel #4
0
 def traverse_breath_first(self):
     queue = Queue()
     curr = self.root
     queue.enqueue(curr)
     # import pdb; pdb.set_trace()
     while queue.is_empty():
         curr = queue.dequeue()
         if curr.child_left:
             queue.enqueue(curr.child_left)
         if curr.child_right:
             queue.enqueue(curr.child_right)
         yield (curr.value)
def test_dequeue_multiple():
    q4 = Queue()
    q4.enqueue(7)
    q4.enqueue(5)
    q4.enqueue(3)
    q4.enqueue(1)
    q4.dequeue()
    q4.dequeue()
    q4.dequeue()
    q4.dequeue()
    assert q4.front == None
    assert q4.rear == None
    assert q4.is_empty()
 def breath_first(self):
     rtn = []
     queue = Queue()
     curr = self.root
     queue.enqueue(curr)
     # import pdb; pdb.set_trace()
     while queue.is_empty():
         curr = queue.dequeue()
         if curr.child_left:
             queue.enqueue(curr.child_left)
         if curr.child_right:
             queue.enqueue(curr.child_right)
         rtn.append(curr.value)
     return rtn
 def breadth_first(self) -> list:
     """ takes a Binary Tree as its unique input, traverse the input tree using a Breadth-first approach, and return a list of the values in the tree in the order they were encountered. """
     frontier = Queue()
     frontier.enqueue(self.root)
     explored = []
     current = None
     while not frontier.is_empty():
         current = frontier.dequeue()
         explored.append(current.value)
         if current.left:
             frontier.enqueue(current.left)
         if current.right:
             frontier.enqueue(current.right)
     return explored
 def find_max_value(self):
     max_value = self.root.value
     curr=self.root
     queue=Queue()
     queue.enqueue(curr)
     # import pdb; pdb.set_trace()
     while queue.is_empty():
         curr = queue.dequeue()
         if curr.value > max_value:
             max_value=curr.value
         if curr.child_left:
             queue.enqueue(curr.child_left)
         if curr.child_right:
             queue.enqueue(curr.child_right)
     return max_value 
    def breadthOrder(self):
        '''
        This method prints out the tree using a breadth-first approach that steps across the width of the tree before descending another level.
        '''
        ordered_list = []

        tree_node_queue = Queue()
        tree_node_queue.enqueue(self.root)

        while not tree_node_queue.is_empty():
            tree_node_removed_from_queue = tree_node_queue.dequeue()
            ordered_list.append(tree_node_removed_from_queue.value)

            if tree_node_removed_from_queue.left_node:
                tree_node_queue.enqueue(tree_node_removed_from_queue.left_node)

            if tree_node_removed_from_queue.right_node:
                tree_node_queue.enqueue(
                    tree_node_removed_from_queue.right_node)

        return ordered_list
 def find_height(self):
     height_counter = 0
     marker= None
     curr = self.root
     queue = Queue()
     queue.enqueue(curr)
     check_queue = Queue()
     while queue.is_empty():
         curr=queue.dequeue()
         if curr.child_left:
             queue.enqueue(curr.child_left)
         if curr.child_right:
             queue.enqueue(curr.child_right)
         if marker is curr.value:
             marker= None
         if marker is None:
             if curr.child_left or curr.child_right:
                 if curr.child_left:
                     height_counter +=1  
                     marker = curr.child_left.value
                 if curr.child_right:
                     marker = curr.child_right.value
                     height_counter +=1
     return height_counter
def test_is_empty():
    q = Queue()
    actual = q.is_empty()
    expected = True
    assert actual == expected
def test_queue_isEmpty():
    queue7 = Queue()
    assert queue7.is_empty() == True
    queue7.enqueue(5)
    assert queue7.is_empty() == False
Beispiel #13
0
def test_is_empty_queue():  # 13
    queue = Queue()
    actual = queue.is_empty()
    expected = True
    assert actual == expected
def test_peek_queue_from_empty():
    q7 = Queue()
    assert q7.is_empty()
    with pytest.raises(AttributeError):
        q7.peek()
def test_dequeue_from_empty():
    q6 = Queue()
    assert q6.is_empty()
    with pytest.raises(AttributeError):
        q6.dequeue()
def test_instantiate_empty_queue():
    q5 = Queue()
    assert q5.front == None
    assert q5.is_empty() == True