Skip to content

Python implementation of a Max Heap based on Cormen's 'Introduction to Algorithms' book for educational purposes.

Notifications You must be signed in to change notification settings

notini/python_max_heap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

python_max_heap

Python implementation of a Max Heap based on Cormen's 'Introduction to Algorithms' book for educational purposes.

Usage

First, import the MaxHeap class and declare an object:

from maxHeap import MaxHeap
max_heap = MaxHeap()

The max_heapify() function guarantees the heap condition will always hold. In order to heapify an existing array:

values = [4,1,3,2,16,9,10,14,8,7]
for idx in range(math.floor(len(values) / 2), 0, -1):
	max_heap.max_heapify(values, len(values), idx - 1)

You can also start you heap by inserting values with the insertKey() function.

values = [4,1,3,2,16,9,10,14,8,7]
heap = []
for value in values:
	max_heap.insertKey(heap, value)

Inserting a new key on an existing heap:

max_heap.insertKey(heap, 20)

In order to sort the heap:

max_heap.heapsort(heap)
print('Heap is now sorted:', heap)

Example

example.py provides examples for the conditions mentioned above. To run it, run python example.py inside the project folder.

Any doubts/suggestions, feel free to contact me.

About

Python implementation of a Max Heap based on Cormen's 'Introduction to Algorithms' book for educational purposes.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages