コード例 #1
0
    def testHeapIncreaseKey(self):
        """This test will verify that the resulting array
        is not a max heap and it will fail. The resulting 100
         breaks the heap property."""

        theList = [5, 7, 9, 1, 3, 13, 21, 43, 2, 42, 12, 8, 77, 65, 23]
        MaxHeapPriorityQueue.buildMaxHeap(theList)
        MaxHeapPriorityQueue.increaseKey(theList, 4, 100)
        expected = [77, 43, 65, 7, 100, 13, 23, 1, 2, 3, 12, 8, 9, 21, 5]
        self.assertNotEqual(expected, theList)
コード例 #2
0
    def testHeapIncreaseKeyHoldHeapProperty(self):
        """This test will verify that the resulting array
        is a max heap after a key is increased, thus maintaining
        the heap property."""

        theList = [5, 7, 9, 1, 3, 13, 21, 43, 2, 42, 12, 8, 77, 65, 23]
        MaxHeapPriorityQueue.buildMaxHeap(theList)
        MaxHeapPriorityQueue.increaseKey(theList, 4, 100)
        expected = [100, 77, 65, 7, 43, 13, 23, 1, 2, 3, 12, 8, 9, 21, 5]
        self.assertEqual(expected, theList)
コード例 #3
0
class Main:
    a = [5, 7, 9, 1, 3, 13, 21, 43, 2, 42, 12, 8, 77, 65, 23]
    print(str(a) + " - Starting List\n")

    mh = MaxHeapPriorityQueue.buildMaxHeap(a)
    print(str(mh) + " - Max Heap\n\n")

    print("Now lets extract the max value")
    maxV = MaxHeapPriorityQueue.extractMax(mh)
    print(str(maxV) + " - Extracted max value ")
    print(str(mh) + " - Max heap after extraction\n\n")

    a = [5, 7, 9, 1, 3, 13, 21, 43, 2, 42, 12, 8, 77, 65, 23]
    mh = MaxHeapPriorityQueue.buildMaxHeap(a)

    print(
        "Now lets increase index 4 to 100 and then call build max heap again")
    print(MaxHeapPriorityQueue.increaseKey(mh, 4, 100))
    MaxHeapPriorityQueue.buildMaxHeap(mh)
    print(
        str(mh) +
        " - Max heap after index 4 was changed and heap was restored\n\n")

    a = [5, 7, 9, 1, 3, 13, 21, 43, 2, 42, 12, 8, 77, 65, 23]
    mh = MaxHeapPriorityQueue.buildMaxHeap(a)

    print("Now lets increase index 9 to 1")
    print(MaxHeapPriorityQueue.increaseKey(mh, 9, 1))
    print("This error occurs because the key is smaller than current key\n\n")

    print("We will now insert 26 into the heap")
    MaxHeapPriorityQueue.insert(mh, 26)
    print(str(mh) + "\n\n\n")

    a = [21, 7, 9, 1, 3, 13, 21, 43, 2, 42, 12, 8, 77, 65, 23]
    print(a)
    print(MaxHeapPriorityQueue.getMaximum(a))
    print(a)