def extract_max(S):
    "Remove and return the element of S with largest key - O(lg n)"
    if S.heap_size < 1:
        raise Exception("Trivial heap")
    max, S[0] = S[0], S[S.heap_size - 1]
    S.heap_size -= 1
    max_heapify(S, 0)
    return max
def heap_extract_max(a):
    if len(a) < 1:
        return 'error: heap underflow'
    max = a[0]
    a[0] = a[len(a) - 1]
    a.pop()
    heap_sort.max_heapify(a, 0)
    return max
Example #3
0
def extract_max(heap):
    if heap.heap_size < 1:
        raise HeapUnderflowException
    mx = heap.at(0)
    lst = heap.heap_size - 1
    heap.put(0, heap.at(lst))
    heap.heap_size -= 1
    del heap.array[lst]  # not totally necessary
    hs.max_heapify(heap, 0)
    return mx
	def extract_max(self):
		if self.heap_size()<1:
			raise "heap underflow"
		maxnum=self.queue[1]
		self.queue[1]=self.queue[self.heap_size()]
		self.queue.pop()
		# self.heap_size -= 1
		heap_size=self.heap_size()
		max_heapify(self.queue,1,heap_size)
		return maxnum
Example #5
0
def heap_extract_max(array, heap_size):
    if heap_size < 1:
        raise ValueError

    max_value = array[0]
    array[0] = array[heap_size - 1]
    del array[heap_size - 1]
    heap_size -= 1
    max_heapify(array, heap_size, 0)

    return max_value
Example #6
0
def heap_extract_max(arr):
    n = len(arr)
    build_max_heap(arr)
    #print n
    if n < 0:
        return 'Heap Underflow'
    max = arr[0]
    arr.remove(max)
    n = len(arr)
    max_heapify(arr, n, 0)
    return max
def delete(S, i):
    """
    Deletes the item at node i. Delete is done in 2 steps:
    1. Increase the element at i to infinity using increase_key(). This bring S[i] to the root of the max heap.
    2. Replace the root with the last leaf in the heap. Decrement heap size by 1 and apply max_heapify()
    This fixes the gap that is left in the subtree where S[i] is the root - O(lg n)
    """
    increase_key(S, i, float("inf"))
    S[0] = S[S.heap_size - 1]
    S.heap_size -= 1
    max_heapify(S, 0)
Example #8
0
def extract_max(A):
    if len(A) < 1:
        return []
    max = A[0]
    A[1] = A[-1]
    A = A[:-1]
    A = max_heapify(A,0)
    return max,A
 def runTest(self) :
     heap_sort.max_heapify( self.values[ 0 ], 1, len( self.values[ 0 ] ))
     self.assertEqual( self.values[ 0 ],
                       self.expected[ 0 ],
                       "not passed")
 def test_not_max_heap_before(self):
     self.array = [1,2,3]
     self.assertEqual(heap_sort.max_heapify(self.array,0),[3,2,1])
 def test_already_max_heap(self):
     self.array = [3,2,1]
     self.assertEqual(heap_sort.max_heapify(self.array,0),self.array)
 def test_height2_not_max_heapify_before(self):
     self.array = [5,3,9,2,1,4,6]
     self.assertEqual(heap_sort.max_heapify(self.array,0),[9,3,6,2,1,4,5])
Example #13
0
 def test_max_heapify_for_even_count_of_nums(self):
     self.assertEqual([3, 6, 5, 1, 0, 9], max_heapify([3, 1, 5, 6, 0, 9],
                                                      2))
Example #14
0
 def test_max_heapify_for_bigger_sample_with_inner_pos(self):
     nums = [16, 4, 10, 14, 7, 9, 3, 2, 8, 1]
     self.assertEqual(nums, max_heapify(nums, 4))
Example #15
0
 def test_max_heapify_for_bigger_sample(self):
     nums = [16, 4, 10, 14, 7, 9, 3, 2, 8, 1]
     self.assertEqual([16, 14, 10, 8, 7, 9, 3, 2, 4, 1],
                      max_heapify(nums, 2))