def test_case02(self): A = [1, 2, 3, 4] heap.max_heapify(A, 1, len(A)) visit = heap.visit(A) print visit self.assertTrue(visit[0] == [1] and visit[1] == [4, 3] and visit[2] == [2])
def pop_max(self): if self.heap_size == 0: raise "heap underflow" max_value = self.max_heap[0] self.max_heap[0] = self.max_heap[self.heap_size-1] self.heap_size -= 1 heap.max_heapify(self.max_heap, 0) return max_value
def heap_extract_max(A, heap_size): if heap_size < 1: return "Error: heap underflow" else: max = A[0] A[0] = A[heap_size - 1] heap_size -= 1 heap.max_heapify(A, 0, heap_size) return max
def heap_extract_max(A): heap_size = len(A) if heap_size < 1: print("heap underflow") return max = A[0] A[0] = A[heap_size] heap_size -= 1 max_heapify(A, heap_size, 0) return max
def heap_extract_max(A): if len(A)<1: print('errror-heap underflow') max=A[0] A[0]=A[len(A)-1] # POP MAX(last element) A.pop() # Before call MAX-HEAPIFY,First set HEAP-SIZE's Value HEAP_SIZE['A']=len(A) max_heapify(A,0) # print(A) return max
def heap_sort(items): """Sorts a list of items. Uses heap sort to sort the list items. Args: items: A list of items. Returns: The sorted list of items. """ # Build a max heap of items. build_max_heap(items) n = len(items) result = [0 for x in range(n)] for i in reversed(range(n)): _swap(items, _root(), i) result[i] = remove_last(items) max_heapify(items, _root()) return result
def test_max_heapify_left_sift(self): lst = [2, 12, 9, 4, 3, 4, 6, 2, 1, 0] max_heapify(lst, 0, len(lst)) exp_out = [12, 4, 9, 2, 3, 4, 6, 2, 1, 0] self.assertEqual(exp_out, lst)
def test_max_heapify_basic(self): lst = [2, 5, 9, 4, 3, 4, 6, 2, 1, 0] max_heapify(lst, 0, len(lst)) exp_out = [9, 5, 6, 4, 3, 4, 2, 2, 1, 0] self.assertEqual(exp_out, lst)
def test_case01(self): A = [1, 2, 3] heap.max_heapify(A, 0, len(A)) visit = heap.visit(A) self.assertTrue(visit[0] == [3] and visit[1] == [2, 1])
def test_heapify(self): l = self.make_list() max_heapify(l, 1) self.assertEqual([16, 14, 10, 8, 7, 9, 3, 2, 4, 1], l)