def main(): a = [16, 14, 10, 8, 7, 9, 3, 2, 4, 1] heap_sort.build_max_heap(a) print(heap_maximum(a)) #print maximum element from heap print(heap_extract_max(a)) #extract the maximum element from the heap max_heap_insert(a, 20) #insert new element in the heap print(a)
def heap_increase_key(arr, i, key): build_max_heap(arr) if key < arr[i]: print('New key is smaller then current key') arr[i] = key while i > 0 and arr[parent(i)] < arr[i]: arr[i], arr[parent(i)] = arr[parent(i)], arr[i] i = parent(i)
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 heap_maximum(arr): build_max_heap(arr) return arr[0]
def max_heap_insert(arr, key): build_max_heap(arr) n = len(arr) arr.append(-9999999) heap_increase_key(arr, n, key)
# Increase key in Heap def heap_increase_key(arr, i, key): build_max_heap(arr) if key < arr[i]: print('New key is smaller then current key') arr[i] = key while i > 0 and arr[parent(i)] < arr[i]: arr[i], arr[parent(i)] = arr[parent(i)], arr[i] i = parent(i) # Insert element to max heap def max_heap_insert(arr, key): build_max_heap(arr) n = len(arr) arr.append(-9999999) heap_increase_key(arr, n, key) # arr = [4, 1, 3, 2, 16, 9, 10, 14, 11, 13, 8, 12, 15, 7] arr = [15, 13, 9, 5, 12, 8, 7, 4, 0, 6, 2, 1] # for i in range(len(arr)-1): # max_val = heap_extract_max(arr) # print(max_val) build_max_heap(arr) print(arr) #heap_increase_key(arr, 8, 15) max_heap_insert(arr, 10) print(arr)
def test_as_my_wish(self): self.test_value = [1,2,3,45,6,7,100] after_run = heap_sort.build_max_heap(self.test_value) self.assertEqual(after_run,[100,45,7,2,6,1,3])
def test_range(self): self.test_value = range(100,0,-1) self.assertEqual(heap_sort.build_max_heap(self.test_value)[0],100)
from heap_sort import build_max_heap,max_heapify """ A Priority Queue is a data structure for maintaining a set S of elements, each with an associated value called a key. A max-priority queue supports the following operations: INSERT(S,x) MAXIMUM(S) EXTRACT-MAX(S): pop the maximum elements. INCREASE-KEY(S,x,k) : increase the value of elements x's key to the new value k, which is assumed to be at least as large as x's current key value. """ test = [4,1,3,2,16,9,10,14,8,7] test = build_max_heap(test) # A must be a max heap. def maximum(A): return A[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 increase_key(A,x,key): if key < A[x]: raise SyntaxError,'new key is smalller than current key' A[x] = key while x > 0 and A[(x+1)/2-1] < A[x]:
def __init__(self,a): build_max_heap(a) self.queue=a
def test_build_max_heap_for_given_sample(self): actual = build_max_heap([1, 2, 0, 8, 9, 3, 5, 4, 7, 6]) expected = [9, 8, 5, 7, 6, 3, 0, 4, 1, 2] self.assertListEqual(expected, actual)
def test_build_max_heap_given_even_count_of_nums(self): actual = build_max_heap([16, 4, 10, 14, 7, 9, 3, 2, 8, 1]) expected = [16, 14, 10, 8, 7, 9, 3, 2, 4, 1] self.assertListEqual(expected, actual)
def test_build_max_heap_given_odd_count_of_nums(self): actual = build_max_heap([3, 5, 1, 6, 0]) expected = [6, 5, 1, 3, 0] self.assertListEqual(expected, actual)