def test_delete_two(self):
     """
     Deleting when there are two elements in the heap removes the root element
     and returns it, leaving the other element in its place as the new root.
     Hint: There's a version of the pop method that takes an argument.
     """
     h = MaxHeap()
     h.insert(10)
     h.insert(5)
     self.assertEqual(10, h.delete())
     self.assertEqual(1, len(h._data))
     self.assertEqual(5, h.delete())
     self.assertEqual(0, len(h._data))
 def test_delete_one(self):
     """
     Deleting when there is only one element removes that element
     and returns it.
     """
     h = MaxHeap()
     h.insert(10)
     self.assertEqual(10, h.delete())
     self.assertEqual(0, len(h._data))
class PriorityQueue:
    def __init__(self):
        self.heap = MaxHeap()

    def enqueue(self, value):
        self.heap.insert(value)

    def dequeue(self):
        return self.heap.delete()

    def is_empty(self):
        return self.heap._size() == 0
 def test_delete_larger_left_three(self):
     """
     Deleting when there are three elements in the heap removes the root element
     and returns it, leaving the larger of the two children as the new root.
       10            5
      /  \    =>    /
     5    1        1
     """
     h = MaxHeap()
     h.insert(10)
     h.insert(5)
     h.insert(1)
     self.assertEqual(10, h.delete())
     self.assertEqual(2, len(h._data))
     self.assertEqual(5, h._data[0])
     self.assertEqual(1, h._data[1])
 def test_delete_larger_right_three(self):
     """
     Deleting when there are three elements in the heap removes the root element
     and returns it, leaving the larger of the two children as the new root.
       10            5
      /  \    =>    /
     1    5        1
     Hint: Two base cases, and one case that requires the algorithm.
     """
     h = MaxHeap()
     h.insert(10)
     h.insert(1)
     h.insert(5)
     self.assertEqual(10, h.delete())
     self.assertEqual(2, len(h._data))
     self.assertEqual(5, h._data[0])
     self.assertEqual(1, h._data[1])
 def test_delete_omg(self):
     """
     Lots of deletions should result in the MaxHeap obeying the max-heap
     property at every node in the tree, and the root always being the largest
     value in the tree.
     """
     h = MaxHeap()
     for _ in range(100):
         h.insert(random.randint(1, 1000))
     previous_root = h._data[0] + 1 # Seed a value larger than anything in the heap.
     while len(h._data) > 0:
         latest_root = h.delete()
         self.assertTrue(previous_root >= latest_root)
         for i in reversed(range(len(h._data))):
             if (i - 1) // 2 < 0:
                 break
             self.assertTrue(h._data[i] <= h._data[(i - 1) // 2])
         previous_root = latest_root
 def test_delete_larger_right_four(self):
     """
     Deleting when there are four elements in the heap removes the root element
     and returns it, leaving the larger of the two children as the new root.
         10            8
        /  \    =>    /  \
       5    8        5    2
      /
     2
     """
     h = MaxHeap()
     h.insert(10)
     h.insert(5)
     h.insert(8)
     h.insert(2)
     self.assertEqual(10, h.delete())
     self.assertEqual(3, len(h._data))
     self.assertEqual(8, h._data[0])
     self.assertEqual(5, h._data[1])
     self.assertEqual(2, h._data[2])
 def test_delete_larger_left_five_root(self):
     """
     Deleting when there are five elements in the heap removes the root element
     and returns it, leaving the larger of the two children as the new root.
     The leaf that was made the new root sifts down as far as it needs to,
     to obey the heap property.
         10            8
        /  \    =>    /  \
       8    5        2    5
      / \           /
     2   1         1
     """
     h = MaxHeap()
     h.insert(10)
     h.insert(8)
     h.insert(5)
     h.insert(2)
     h.insert(1)
     self.assertEqual(10, h.delete())
     self.assertEqual(4, len(h._data))
     self.assertEqual(8, h._data[0])
     self.assertEqual(2, h._data[1])
     self.assertEqual(5, h._data[2])
     self.assertEqual(1, h._data[3])
示例#9
0
class HeapTests(unittest.TestCase):
    def setUp(self):
        self.heap = MaxHeap()

    def test_heap_insert_works(self):
        self.heap.insert(6)
        self.heap.insert(8)
        self.heap.insert(10)
        self.heap.insert(9)
        self.heap.insert(1)
        self.heap.insert(9)
        self.heap.insert(9)
        self.heap.insert(5)
        self.assertEqual(self.heap.storage, [10, 9, 9, 6, 1, 8, 9, 5])

    def test_get_max_works(self):
        self.heap.insert(6)
        self.heap.insert(8)
        self.heap.insert(10)
        self.heap.insert(9)
        self.heap.insert(1)
        self.heap.insert(9)
        self.heap.insert(9)
        self.heap.insert(5)
        self.assertEqual(self.heap.get_size(), 8)
        self.assertEqual(self.heap.get_max(), 10)

    def test_get_max_after_delete(self):
        self.heap.insert(6)
        self.heap.insert(8)
        self.heap.insert(10)
        self.heap.insert(9)
        self.heap.insert(1)
        self.heap.insert(9)
        self.heap.insert(9)
        self.heap.insert(5)
        self.heap.delete()
        self.assertEqual(self.heap.get_max(), 9)
        self.heap.delete()
        self.assertEqual(self.heap.get_max(), 9)
        self.heap.delete()
        self.assertEqual(self.heap.get_max(), 9)
        self.heap.delete()
        self.assertEqual(self.heap.get_max(), 8)
        self.heap.delete()
        self.assertEqual(self.heap.get_max(), 6)

    def test_delete_elements_in_order(self):
        self.heap.insert(6)
        self.heap.insert(7)
        self.heap.insert(5)
        self.heap.insert(8)
        self.heap.insert(10)
        self.heap.insert(1)
        self.heap.insert(2)
        self.heap.insert(5)

        descending_order = []

        while self.heap.get_size() > 0:
            descending_order.append(self.heap.delete())

        self.assertEqual(descending_order, [10, 8, 7, 6, 5, 5, 2, 1])

    def test_bubble_up_was_called(self):
        self.heap._bubble_up = MagicMock()
        self.heap.insert(5)
        self.assertTrue(self.heap._bubble_up.called)

    def test_sift_down_was_called(self):
        self.heap._sift_down = MagicMock()
        self.heap.insert(10)
        self.heap.insert(11)
        self.heap.delete()
        self.assertTrue(self.heap._sift_down.called)
 def test_delete_empty(self):
     """
     Deleting from an empty MaxHeap returns None.
     """
     h = MaxHeap()
     self.assertIsNone(h.delete())