Exemple #1
0
 def test_maxheap(self):
     """verifies that heap.build_maxheap() returns an array that satisfies
     the max_heap property."""
     h = Heap(self.unsorted_array)
     h.build_max_heap()
     list_1 = h.get_array()
     self.assertTrue(self.is_maxheap(list_1))
Exemple #2
0
 def test_sort_does_not_work(self):
     """makes sure that heap does not return a sorted array before
     sort is called. sounds stupid but needs to be done lol."""
     h = Heap(self.unsorted_array)
     h.build_max_heap()
     list_1 = h.get_array() # should probably change this method name. bad name.
     list_2 = sorted(self.unsorted_array)
     self.assertNotEqual(list_1, list_2)
Exemple #3
0
 def test_heapsort_correctness_after_insertion(self):
     """tests to see if heap sort works properly after element insertion."""
     h = Heap(self.unsorted_array)
     h.build_max_heap()
     h.insert(5)
     h.insert(10)
     self.unsorted_array.append(5)
     self.unsorted_array.append(10)
     h.heap_sort()
     sorted_array = sorted(self.unsorted_array)
     list_1 = h.get_array()
     self.assertEqual(sorted_array, list_1)
Exemple #4
0
 def test_sort_correctness(self):
     """tests to see whether or not Heap sorts correctly by sorting
     a list using heap sort and comparing it to a sort using python's
     standard library sort."""
     # initialize heap with this array
     h = Heap(self.unsorted_array)
     # build the heap using max heapify
     h.build_max_heap()
     # sort the array using heap sort 
     h.heap_sort()
     list1 = h.get_array()
     list2 = sorted(self.unsorted_array)
     self.assertEqual(list1, list2)
Exemple #5
0
    def test_heap_correctness_after_insertion(self):
        """checks to see whether or not heap constraints are satisified after insertion.
        Heap Property: each node should be greater than or equal to its parent
        The parent of element i in a binary heap stored in an array element
        is (i - 1) // 2.
        
        found on stackoverflow: 
        https://stackoverflow.com/questions/16414671/\
                determining-if-a-list-of-numbers-is-in-heap-order-python-3-2
        """
        # NOTE:
        # is_heap and heapq.heapify(x) BOTH implement min heaps and
        # Heap() uses max heap. we need to invert the values so that
        # our max-heap turns in to a min-heap to verify correctness.
        # the other alternative is to actually write a max-heap checker
        # or actually implement min-heap.

        h = Heap(self.unsorted_array)
        h.build_max_heap()
        for i in range(9):
            h.insert(i)
        list_1 = h.get_array()
        self.assertTrue(self.is_maxheap(list_1))