Environmental drivers of beluga summer distribution in the Mackenzie Estuary

Using 2019 aerial photos of beluga whales together with remotely sensed environmental variables, we modelled beluga habitat selection. Belugas selected warm water temperatures and high turbidity…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Learning Heaps with Python

A Heap is a special Tree-based data structure in which the tree is a complete Binary tree. The Heap data structure is very efficient to find the kth largest or smallest element. Heaps allow you to pull the smallest or the biggest element and quickly know the next smallest or biggest element. That is why it’s called a priority queue.

Each insert and delete operation is O(log N) at the very worst. And here is the best part, you can get either the smallest element or biggest element in Constant time i.e., O(1)

How do we implement it?

There are two ways to implement Heaps,

We typically come across two types of Heaps:

The above methods give the Index of its left child, right child, and parent by passing an element Index. The index of a Heap starts from 0 and go on like this:

Indexing of nodes in a Tree (Heap). The node at the top is called “root” of Heap.

The above methods would return True or False if Index hasLeftChild, hasRightChild, and isRoot on passing an elementIndex.

These methods would return the value stored in its leftChild, rightChild, and parent on passing an elementIndex.

Method swap swap’s value stored in firstIndex and SecondIndex. isEmpty returns True if the Heap is empty. peek returns the root/top element of Heap. If the Heap size is 0, then peek raises an IndexError.

pop returns & removes the root/top element from the Heap. It then initializes the root element (i.e. index = 0) to the last element of the Heap. Then we call heapifyDown which rearranges the Min-Heap in order (which brings the smallest element to the top). As an element is removed from the Heap, we decrease its size by 1.

add add’s an element to the Heap. The new element is stored at the end of the list and then we call heapifyUp which ensures that element falls in its correct spot in the Heap. As an element is added to the Heap, we increase its size by 1.

heapifyDown starts from index 0. smallerIndex stores rightChildIndex if the right child element is lesser than left Child otherwise it stores leftChildIndex. It then swaps smallerIndex element with index element if smallerIndex element value is greater than index element otherwise it breaks the loop. Later, the index is updated to smallerIndex. This continues till child elements are greater than index element. Here is how heapifyDown would look:

heapifyDown

displayHeap prints the elements of the Heap. heapifyUp starts with index as the last element of Heap. It then swaps with the parent element if index element is less than parent element. index is then updated to parentIndex. This continues until the parent element is no longer greater the index element. Here is how heapifyUp would look:

heapifyUp

Max-Heap is almost same with some minor tweaks in heapifyUp and heapifyDown methods.

That’s it for now, feel free to share your comments! :)

Note: There is an inbuilt module called ‘heapq’ in Python.

Add a comment

Related posts:

Branding is not a logo.

Branding is the result of a logo, marketing, social media campaigns, images, videos, hashtags, speaking events, conferences, billboards, etc. When you’re building a brand, what matters is what…

The benefits of working with Advance High Gloss Decorative Laminates

Design vision and use of different types of laminates play a vital factor to achieve the desired look into your interiors. In the context of decor style and intended application, the use of high…

Fin del mundo

Voy por el round no sé cuanto contra mi propia mente me preguntaba si también te da curiosidad saber de mí Sé que no mereces mucho pero yo también valgo nada tal vez por eso hoy te soñé Veía una…