Exemplo n.º 1
0
def test_build_heap():
    print("#test_build_heap")
    for x in range(5, 41, 5):
        A = shuffled_list(x, x)
        heap.buildHeap(A)
        print("buildHeap(shuffled_list({}, {})) is a min heap: ".format(x, x),
              is_min_heap(A))
def kthSmallest(collection, k):
	'''Return Kth smallest element in collection for valid k >= 1'''

	A = collection[:k]
	heap.buildHeap(A)
	for i in xrange(k, len(collection)):
		if collection[i] < A[0]:
			A[0] = collection[i]
			heap.heapify(A, 0, k)

	return A[0]
Exemplo n.º 3
0
def report_counts_on_basic_ops(A, expected_build_count, expected_extract_count, expected_insert_count):
    print("Counts for list of len: {}".format(len(A)))
    heap.reset_counts()
    heap.buildHeap(A)
    bh = heap.current_counts()['heapify_call_count']
    print("buildHeap heapify calls within 20% of expected number of calls: {}".format(within_20_percent(expected_build_count, bh)))

    heap.reset_counts()
    m = heap.heapExtractMin(A)
    ex = heap.current_counts()['heapify_call_count']
    print("heapExtractMin heapify calls within 20% of expected number of calls: {}".format(within_20_percent(expected_extract_count, ex)))

    heap.reset_counts()
    heap.heapInsert(A, m)
    ins = heap.current_counts()['swap_count']
    print("heapInsert swap calls within 20% of expected number of calls: {}".format(within_20_percent(expected_insert_count, ins)))
import heap

a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

print(f"before building heap {a}")

heap.buildHeap(a)

print(f"after building heap {a}")

print(f"max : {heap.getMax(a)}")

print(f"latest array status {a}")

heap.increaseKey(a, 3, 400)

print(f"latest array status {a}")

heap.increaseKey(a, 8, 30)

print(f"latest array status {a}")

heap.insert(a, 15)
print(f"latest array status {a}")

heap.insert(a, 12)
print(f"latest array status {a}")

heap.insert(a, 500)
print(f"latest array status {a}")
Exemplo n.º 5
0
import heap

if __name__ == "__main__":
    A = heap.shuffled_list(20, 0)
    print("Complete Tree size 20:")
    heap.printCompleteTree(A)
    heap.buildHeap(A)
    print("After Build Heap")
    heap.printCompleteTree(A)
    print("After heapExtractMin")
    heap.heapExtractMin(A)
    heap.printCompleteTree(A)
    print("After heapInsert")
    heap.heapInsert(A, 9)
    heap.printCompleteTree(A)