Exemplo n.º 1
0
    def test_extract_min(self):
        """ Makes sure that the heap produces min values and that
        the heap property is preserved.
        Given the following heap:
                        (4)
                       /   \
                    (4)    (8)
                   /  \    /  \
                 (9) (4) (12) (9)
                /  \
             (11)  (13)
        """
        data = [4, 4, 8, 9, 4, 12, 9, 11, 13]
        h = Heap(data)

        min_key = h.extract_min()
        self.assertEqual(min_key, 4, 'should extract the min value')
        self.assertTrue(Heap.is_heap(data),
                        'should still hold the heap property')

        min_key = h.extract_min()
        self.assertEqual(min_key, 4, 'should extract the min value')
        self.assertTrue(Heap.is_heap(data),
                        'should still hold the heap property')

        min_key = h.extract_min()
        self.assertEqual(min_key, 4, 'should extract the min value')
        self.assertTrue(Heap.is_heap(data),
                        'should still hold the heap property')
Exemplo n.º 2
0
    def test_extract_min(self):
        """ Makes sure that the heap produces min values and that
        the heap property is preserved.
        Given the following heap:
                        (4)
                       /   \
                    (4)    (8)
                   /  \    /  \
                 (9) (4) (12) (9)
                /  \
             (11)  (13)
        """
        data = [4, 4, 8, 9, 4, 12, 9, 11, 13]
        h = Heap(data)

        min_key = h.extract_min()
        self.assertEqual(min_key, 4, 'should extract the min value')
        self.assertTrue(Heap.is_heap(data), 'should still hold the heap property')

        min_key = h.extract_min()
        self.assertEqual(min_key, 4, 'should extract the min value')
        self.assertTrue(Heap.is_heap(data), 'should still hold the heap property')

        min_key = h.extract_min()
        self.assertEqual(min_key, 4, 'should extract the min value')
        self.assertTrue(Heap.is_heap(data), 'should still hold the heap property')
Exemplo n.º 3
0
    def test_problem_1(self):
        """ You are given as input an unsorted array of n distinct numbers,
        where n is a power of 2. Give an algorithm that identifies the
        second-largest number in the array, and that uses at most
        n+log2n−2 comparisons.

        Solution: use a hash data structure.
        """
        numbers = [5,1,2,5,1,2,3,54,6,7,1,3,3,5,6,2,3,4,56,6]
        h = Heap()
        for number in numbers:
            h.insert(-number)

        h.extract_min()
        actual = -h.extract_min()
        self.assertEqual(actual, 54, 'found the second largest number')
Exemplo n.º 4
0
    def test_problem_1(self):
        """ You are given as input an unsorted array of n distinct numbers,
        where n is a power of 2. Give an algorithm that identifies the
        second-largest number in the array, and that uses at most
        n+log2n−2 comparisons.

        Solution: use a hash data structure.
        """
        numbers = [
            5, 1, 2, 5, 1, 2, 3, 54, 6, 7, 1, 3, 3, 5, 6, 2, 3, 4, 56, 6
        ]
        h = Heap()
        for number in numbers:
            h.insert(-number)

        h.extract_min()
        actual = -h.extract_min()
        self.assertEqual(actual, 54, 'found the second largest number')