Example #1
0
    def testHeapExtractMax(self):
        """This test will verify that the resulting array
        is equal to a correct array after the element has
        been extracted"""

        theList = [2, 7, 26, 25, 19, 17, 1, 90, 3, 36]
        MaxHeapPriorityQueue.buildMaxHeap(theList)
        MaxHeapPriorityQueue.extractMax(theList)
        expected = [36, 25, 26, 7, 19, 17, 1, 2, 3]
        self.assertEqual(expected, theList)
Example #2
0
    def testHeapExtractMaxLargerList(self):
        """This test will verify that the resulting array
        is equal to a correct array after the element has
        been extracted"""

        theList = [5, 7, 9, 1, 3, 13, 21, 43, 2, 42, 12, 8, 77, 65, 23]
        MaxHeapPriorityQueue.buildMaxHeap(theList)
        MaxHeapPriorityQueue.extractMax(theList)
        expected = [65, 43, 23, 7, 42, 13, 21, 1, 2, 3, 12, 8, 9, 5]
        self.assertEqual(expected, theList)
Example #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)