All about Heaps — Day 9(Python)

All about Heaps — Day 9(Python)

Today we will be reviewing the heap data structure. A heap is a tree-based data structure, which indicates a heap will have a root node and some child nodes. We usually work with the binary ...

Today we will be reviewing the heap data structure.

A heap is a tree-based data structure, which indicates a heap will have a root node and some child nodes. We usually work with the binary heap. A binary has a left child and a right child. The following image is an example of a heap data structure.

Image for post

Heap data structure.

To insert a node into a heap, we use the push function, and to remove the root from the heap, we use the pop function. When a new node needs to be inserted, it is inserted at the end of the heap structure. When we pop from a heap structure, the root element is removed from the heap structure. Once the root is removed, restructure is necessary to preserve the structure of the heap.

If we want to represent our heap in form of an array, index ‘k’ will hold the root node, then its left child is stored at index ‘2k + 1’ and its right child at index ‘2k + 2’.

The above heap will be represented as follows:

Image for post

Array representation of Heap

Based on our requirement, we can either have a min-heap or a max-heap.

What is the difference between min-heap and max-heap?

In min-heap, the root nodes will be smaller than the child nodes.

An example of min-heap is as follows:

Image for post

Min Heap

In a max-heap, the root node will be greater than the child node.

An example of max-heap is as follows:

Image for post

Max Heap

Why would we require a min-heap or a max-heap?

Sorting is one of the applications of the heap. When we create a min-heap or a max-heap, it is easy for us to arrange our elements in ascending or descending order.

What are the other applications of the heap?

  1. We can create a priority queue using a heap. In a priority queue, each element will have a priority associated with it. Based on this priority we can determine the position of elements in the queue.
  2. We can get Kth largest/smallest element in our array.

Let us move into sorting.

To perform heapsort we need to follow 2 steps.

  1. Build heap — Building heap is a method to create a tree-like data structure from the unsorted input array.
  2. Heapify — It is similar to building heap, but assumes that the tree is sorted partly.

Algorithm:

  1. We build the heap as our first step, call heapify, and keep track of the current root.
  2. The left child of the root is 2i+1 where i is the root index and the right child of the root is 2i+2.
  3. Since we are building max heap, our root should be greater than the child. Exchange the position of the children with root if the root is smaller than the children.
  4. Once the max-heap is built, remove the root node and place it at the end of the array.

coding programming python heapsort 365dayschallenge

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Guide to Python Programming Language

Guide to Python Programming Language

Generate QR Code using Python — Python Programming — PyShark

This article will explore how to generate QR code in Python and some useful creation features from pyqrcode library. QR codes recently became more popular than ever before, yet few people know that the first iterations of QR codes were created back in 1990s in Japan for the automotive industry.

Python Programming: A Beginner’s Guide

Python is an interpreted, high-level, powerful general-purpose programming language. You may ask, Python’s a snake right? and Why is this programming language named after it?

Python Hello World Program - Create & Run Your First Python Program in PyCharm

Python Hello World Program - Your first step towards Python world. Learn how to create the Hello World Python program in PyCharm.

Python Programming Tutorials For Beginners

Python Programming Tutorials For Beginners