Skip to content

A step-by-step implementation of a heap-tree in Python. By this class you can easily convert an array to maxheap or minheap tree. Also you can extract the maximum or minimum value from the heap-tree. The heap-tree is a binary tree that can be used to sort an array of numbers. (both MaxHeap and MinHeap)

License

Notifications You must be signed in to change notification settings

BaseMax/StepByStepHeapTreePy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

StepByStep Heap-Tree in Python

A step-by-step implementation of a heap-tree in Python. By this class you can easily convert an array to maxheap or minheap tree. Also you can extract the maximum or minimum value from the heap-tree.

The heap-tree is a binary tree that can be used to sort an array of numbers. The heap-tree is a complete binary tree, which means that all levels of the tree are filled except possibly the last level. The last level is filled from left to right.

Example

import random
from HeapTree import *

# Create an array of random numbers
array = [random.randint(0, 100) for i in range(10)]

# Create a heap tree
heap = HeapTree(array)

# Print the array
print(heap.array)

# Sort the tree in max heap method
heap.max_heapify()

# Print the array
print(heap.array)

# Sort the tree in min heap method
heap.min_heapify()

# Print the array
print(heap.array)

# Print the maximum value
print(heap.max_extract())

# Print the minimum value
print(heap.min_extract())

Copyright (c) 2023, Max Base

About

A step-by-step implementation of a heap-tree in Python. By this class you can easily convert an array to maxheap or minheap tree. Also you can extract the maximum or minimum value from the heap-tree. The heap-tree is a binary tree that can be used to sort an array of numbers. (both MaxHeap and MinHeap)

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages