Exemple #1
0
    def breadth_first_search(graph, start_node, destination_node):
        """
        BFS. Make use of a queue, and make sure to mark nodes as
        visited!
        TODO: Return the path and its cost.
        """
        graph.reset_visited()

        q = MyQueue()
        start_node.visited = True
        q.push(start_node)

        while (q.is_empty() == False):
            node = q.pop()
            if (node == destination_node):
                return True
            for adjacent in node.adjacents:
                q.push(adjacent)

        return False
Exemple #2
0
class TestMyQueue(unittest.TestCase):
    def setUp(self):
        # Provide empty queue
        self.queue = MyQueue()

        # Provide filled queue
        self.len_test_data = 5
        self.test_data = [i + 1 for i in range(self.len_test_data)]
        self.filled_queue = MyQueue()
        for i in self.test_data:
            self.filled_queue.push(i)

    def test_instantiation(self):
        queue = MyQueue()
        self.assertIsNotNone(queue)
        self.assertIsNone(queue.latest.top)
        self.assertIsNone(queue.oldest.top)

    def test_len(self):
        # Push and pop repeatedly, still one stack should always be empty
        self.assertEqual(0, len(self.queue))
        self.assertEqual(0, len(self.queue.latest))
        self.assertEqual(0, len(self.queue.oldest))

        self.queue.push(1)
        self.queue.push(1)
        self.assertEqual(2, len(self.queue))
        self.assertEqual(2, len(self.queue.latest))
        self.assertEqual(0, len(self.queue.oldest))

        self.queue.pop()
        self.assertEqual(1, len(self.queue))
        self.assertEqual(0, len(self.queue.latest))
        self.assertEqual(1, len(self.queue.oldest))

    def test_push(self):
        for i in self.test_data:
            self.queue.push(i)
            self.assertEqual(i, len(self.queue))
            self.assertEqual(i, len(self.queue.latest))
            self.assertEqual(0, len(self.queue.oldest))
            self.assertEqual(i, self.queue.latest.top.key)

    def test_pop(self):
        for i in self.test_data:
            data = self.filled_queue.pop()
            self.assertEqual(data, i)
            self.assertEqual(self.len_test_data - i, len(self.filled_queue))
            self.assertEqual(0, len(self.filled_queue.latest))
            self.assertEqual(self.len_test_data - i,
                             len(self.filled_queue.oldest))
            if (i < self.len_test_data):
                self.assertEqual(i + 1, self.filled_queue.oldest.top.key)
Exemple #3
0
    def list_of_depths(self):
        """
        Create a linked list of all the nodes at each depth.
        For example, a tree with depth D has D linked lists.
        """
        queue = MyQueue()
        the_dict = defaultdict(list)
        level = 0

        queue.push((self, level))
        while (queue.is_empty() == False):
            node, level = queue.pop()
            the_dict[level].append(node.key)
            if (node.left != None):
                queue.push((node.left, level + 1))
            if (node.right != None):
                queue.push((node.right, level + 1))

        return the_dict