<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Subhasree</title>
    <description>The latest articles on DEV Community by Subhasree (@cyangirl).</description>
    <link>https://dev.to/cyangirl</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F742793%2Fdf2913a7-c9ce-479f-9a98-e817aff8c534.jpg</url>
      <title>DEV Community: Subhasree</title>
      <link>https://dev.to/cyangirl</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/cyangirl"/>
    <language>en</language>
    <item>
      <title>Quick start to Recursion | Identify Use case | In Simple Words</title>
      <dc:creator>Subhasree</dc:creator>
      <pubDate>Sun, 30 Oct 2022 12:15:34 +0000</pubDate>
      <link>https://dev.to/cyangirl/quick-start-to-recursion-identify-use-case-in-simple-words-3h3d</link>
      <guid>https://dev.to/cyangirl/quick-start-to-recursion-identify-use-case-in-simple-words-3h3d</guid>
      <description>&lt;p&gt;Hey there! Hope you are doing well. In this article, I am going to cover&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What is recursion,&lt;/li&gt;
&lt;li&gt;How memory is allocated for recursion&lt;/li&gt;
&lt;li&gt;When we should use recursion ( over iteration )&lt;/li&gt;
&lt;li&gt;It’s limitations ( or when not to use )&lt;/li&gt;
&lt;li&gt;Reference Articles.
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  What is Recursion?
&lt;/h2&gt;

&lt;p&gt;So let’s start with a real world example. A simple yet exciting illustration is Googling Recursion. Noticed what happens?&lt;/p&gt;

&lt;p&gt;It keeps asking you "&lt;strong&gt;&lt;em&gt;Did you mean Recursion?&lt;/em&gt;&lt;/strong&gt;" And even after clicking it, it continues to prompt the same question. Doesn’t it appear to be calling itself over and over again? Well, that is recursion in play.&lt;/p&gt;

&lt;p&gt;In simple words, Recursion is the process where a function calls itself directly or indirectly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;counting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;no&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;

  &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;no&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;counting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;no&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;








&lt;h2&gt;
  
  
  When does recursion stop?
&lt;/h2&gt;

&lt;p&gt;As you have already noticed, in the above example, the recursion goes infinitely. With this, our terminal will overflow with prints and the program’s call stack might crash with Stack overflow, where it will be no longer able to track any further function calls.&lt;/p&gt;

&lt;p&gt;So how do we stop it? We use a simple base case from where the function returns with a terminal point and does not call itself any further.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;counting&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;no&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;no&lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Recursion ends at 10"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;no&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;counting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;no&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code, when our recursion hits an input with &lt;code&gt;no=10&lt;/code&gt;, it simply prints the statement and no longer calls itself again. Thus, we do not have any stack overflow this time.&lt;/p&gt;




&lt;h2&gt;
  
  
  Doesn’t it sound similar to iteration?
&lt;/h2&gt;

&lt;p&gt;Well, it does. Like iteration (irrespective of ‘for’ or ‘while’ loop), in recursion also, we perform sub-tasks repetitively. So broadly speaking, we can implement every logic of recursion with iteration if we want and vice versa.&lt;/p&gt;

&lt;h3&gt;
  
  
  Then why recursion?
&lt;/h3&gt;

&lt;p&gt;Even though it looks similar, recursion has certain advantages over iteration on the basis of readability of code.&lt;/p&gt;

&lt;p&gt;Specially when we need to maintain a stack to keep track of processes we are calling so that they can be referenced again at later point of time. Unlike in iteration, recursion keeps a memory call stack implicitly for its execution.&lt;/p&gt;




&lt;h2&gt;
  
  
  How Recursion works?
&lt;/h2&gt;

&lt;p&gt;As said, Recursion works with the help of &lt;code&gt;Memory Call stack&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In other words, when any function gets called, some memory is allocated for storing its data so that it can be referenced when required. This memory does not get freed up until the function has returned its control or exited from the function’s reference.&lt;br&gt;
Now when other functions get called successively, those calls get stacked in the memory as well, until its execution is completed one by one in LIFO (Last In First Out) sequence.&lt;/p&gt;

&lt;p&gt;The same process takes place when recursion happens. All the function calls get stacked in the memory until they are executed. Once a function returns its control, the stack is popped and next function call in the stack is being executed.&lt;/p&gt;

&lt;p&gt;Let’s analyse with a code snippet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;counting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;no&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;no&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;
    &lt;span class="n"&gt;counting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;no&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;no&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;" "&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;counting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output of the code snippet will look like: &lt;code&gt;5 4 3 2 1 0&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Why the output is in reverse order?
&lt;/h3&gt;

&lt;p&gt;If we run in debug mode and watch the call stack, we will find something like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yXCFUKvg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vmna971frafy2a69x37u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yXCFUKvg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vmna971frafy2a69x37u.png" alt="Snippet of Call Stack in recursion" width="800" height="243"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Decoding Call stack:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;As we can see, the call stack started with the function call at line &lt;code&gt;no. 9&lt;/code&gt; . This is the entry point of our recursive function and &lt;code&gt;counting(0)&lt;/code&gt; gets pushed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Post that, every time we were calling &lt;code&gt;counting(no+1)&lt;/code&gt; , from line &lt;code&gt;no. 6&lt;/code&gt; , we have an entry of it in the call stack with the corresponding value. Print statement never gets executed since before that only Function invokes itself recursively.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Once we reach the base case that is, &lt;code&gt;no&amp;gt;5&lt;/code&gt; , we return from the function for the first time. Hence the last function call that got pushed with &lt;code&gt;no=6&lt;/code&gt; gets popped out of the stack.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Hereafter, popping from the call stack begins. Memory points the next function in the call stack and execute the remaining statements in the function which previously weren’t executed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In this case, &lt;code&gt;print(no)&lt;/code&gt; gets executed and &lt;code&gt;5&lt;/code&gt; gets printed in the terminal.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Similarly, all successive functions get popped out of the call stack one by one in LIFO manner and the print statement is executed with the corresponding values of no .Hence &lt;code&gt;4 3 2 1 0&lt;/code&gt; gets printed.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And that’s it.&lt;/p&gt;




&lt;h2&gt;
  
  
  When to use it?
&lt;/h2&gt;

&lt;p&gt;From the above code examples, it can be seen that the same task of printing number is getting repeated as the function keeps calling itself with a different parameter.&lt;/p&gt;

&lt;p&gt;In other words, we can find the use of recursion where&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We have a definite subtask to be performed.&lt;/li&gt;
&lt;li&gt;We want to repeat that subtask over and over again.&lt;/li&gt;
&lt;li&gt;We need a stack to keep track of processes so that can be referenced later.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For example, to implement &lt;strong&gt;&lt;em&gt;traversing trees&lt;/em&gt;&lt;/strong&gt;, recursion would be a better choice.&lt;/p&gt;




&lt;h2&gt;
  
  
  When not to use it?
&lt;/h2&gt;

&lt;p&gt;Even with this tempting advantage of recursion (who does not like readable code? 👀) , Recursion has certain disadvantages. As we have already seen, recursion requires more memory usage since it saves each function calls in the implicit stack. This can be a major overhead when a task is simpler. In those cases, iteration should be the better choice.&lt;/p&gt;

&lt;p&gt;For example, to implement &lt;strong&gt;&lt;em&gt;fibonacci series&lt;/em&gt;&lt;/strong&gt; iteration would be a better option than recursion.&lt;/p&gt;




&lt;h2&gt;
  
  
  References:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.geeksforgeeks.org/introduction-to-recursion-data-structure-and-algorithm-tutorials/"&gt;GeeksForGeeks Article&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=IJDJ0kBx2LM"&gt;FreeCodeCamp Video&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Conclusion:
&lt;/h3&gt;

&lt;p&gt;I hope this article helps you to get started with the basic concepts of Recursion.&lt;/p&gt;

&lt;p&gt;In the upcoming articles, I am going to cover recursion in more depth like how it works internally, how each operation is handled in recursive functions, more advance example and its complexity with optimisation techniques like dynamic programming.&lt;/p&gt;

&lt;p&gt;For such updates and articles on Data structure and algorithm, follow this page and keep supporting. Cheers! 🥂&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>beginners</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Quick Start to Priority Queue and Heap | With Python</title>
      <dc:creator>Subhasree</dc:creator>
      <pubDate>Sun, 24 Apr 2022 13:08:37 +0000</pubDate>
      <link>https://dev.to/cyangirl/quick-start-to-priority-queue-and-heap-with-python-224n</link>
      <guid>https://dev.to/cyangirl/quick-start-to-priority-queue-and-heap-with-python-224n</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Priority Queue&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Starting with Priority Queue, let’s first understand what a simple queue is and how it is different from a Priority Queue.&lt;br&gt;
Can think of a line where you had to stand, waiting for your turn to get your Pizza, or may be to get into a bus? That is a queue you are standing in. A simple queue, where the first person who joined it, will be the first one to leave it too. Hence, a FIFO (or First In First Out) linear data structure.&lt;/p&gt;

&lt;p&gt;Now imagine, what if in that same queue, a celebrity joins in? Or may be the President of your country? Would that person be standing at the end of the queue now to wait for his turn? Well, in this circumstances, you might want them to go before you, despite the fact they joined last.&lt;/p&gt;

&lt;p&gt;This is a priority queue where each data in the queue has a certain priority and the data with highest priority goes out first and then the second and so on.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Implementation&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;From the above example, it can be seen that a simple queue cannot be used to implement a priority queue, where each data is associated with some weight. So far, priority queue is just like an Abstract Data Type which has to be implemented separately to get the desired order.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Note:&lt;/em&gt;&lt;/strong&gt; Priority queue only supports the data which can be compared with each other. That is, it should be possible to order the data from least to highest priority or highest to least priority.&lt;br&gt;
A classic implementation of Priority Queue can be with Binary Heap.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Binary Heap&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;As the name suggests, in a binary heap, every node has at most two children. Sounds similar to Binary Tree? Well, Binary heap is a special kind of binary tree where the ordering of every nodes matter. In which way, let’s find those below:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each Node has higher priority than its children’s. In the below diagram, the red coloured numbers represent the priorities of the nodes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3p5Vv28o--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kr0k55v6algtt0nvlx9o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3p5Vv28o--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kr0k55v6algtt0nvlx9o.png" alt="Heap Example" width="800" height="191"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Every heap is a complete binary tree, meaning each level in the tree should have the maximum number of nodes, except the last level.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5fadYQd0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5l7gmv1m6utotpdfs6ah.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5fadYQd0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5l7gmv1m6utotpdfs6ah.png" alt="Heap VS Non-Heap" width="800" height="483"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Insertion of new Node always happens from left to right and only in the last level (or new next level when the last level is complete). In case of violation of the Heap Property after insertion, swapping must takes place to satisfy it.
For example, if we try to insert 4 to the above valid heap:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3beB5MEy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2e9xogw0umaicfcf5a69.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3beB5MEy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2e9xogw0umaicfcf5a69.png" alt="Inserting a node to Heap" width="800" height="181"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;There are two types of Heaps:&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;ol&gt;
&lt;li&gt;Max Heap — In this, nodes are ordered from larger values in the top levels to smaller values in the lower levels.&lt;/li&gt;
&lt;li&gt;Min Heap — In this, nodes are ordered from smaller values in top levels to larger values in lower levels.&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;The root node of a Heap always contains the element with highest priority or greatest value in case of Max heap or lowest value in case of Min heap, which is removed first when popped from the Heap.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Representations of Heap:&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;A Heap can be typically represented with an array.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fXlAX8M_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mjmidnyfme7qbgqx67jj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fXlAX8M_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mjmidnyfme7qbgqx67jj.png" alt="A diagram of Heap" width="800" height="181"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above Heap can be represented as :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LaLbuPlc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2cl8u9nqsc7oxwzawi0l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LaLbuPlc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2cl8u9nqsc7oxwzawi0l.png" alt="Array representation of Heap" width="800" height="183"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now some points to note from the array implementation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Here we can clearly see that for each node at index i, the children nodes are at (2*i + 1, 2*i +2)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Parent of a node at index i is (i-1)//2&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To distinguish between Leaf and internal nodes, we have : if n is the length of the array, then any node at index i is internal if 2*i+2 &amp;lt;= n. Else it is a leaf node.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Operations on Binary Heap&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;We can now implement Priority Queue requirements with the help of a Binary heap but in extreme rare occasions, we will need to write the entire implementation from scratch by ourselves. Since already lots of libraries in most of the programming languages offer pre-built Heaps , knowing how it works and how to use the implementations to get our desired output often suffice.&lt;/p&gt;

&lt;p&gt;In this article, we will go through the Python 3 library, heapq, for Heap implementation and get an overview of what their major functions do.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Heapify:&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Starting with Heapify, it is the process of creating a heap data structure from a binary tree. Both Min-Heap or a Max-Heap can be created with this process.&lt;/p&gt;

&lt;p&gt;This process is also used internally to maintain the heap property after insertion or deletion of a node. Time complexity of the process is O(logn).&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Heap Pop or Delete:&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This is the process of popping out element from the Heap, in which always the root node gets deleted first.&lt;br&gt;
The deletion starts with replacement of the last element with the root node to be deleted and then heapifying the last node in its correct index.&lt;/p&gt;

&lt;p&gt;This process uses PercolateDown approach and the Time complexity is O(logn).&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Heap push or Insertion:&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This is the process of inserting new value into the Heap, which always happens at the last level, from left to right order. In case of violation of the Heap property, heapifying the tree may takes place and the process uses PercolateUp approach with a time complexity of O(logn).&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;A simple Python Implementation&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;heapq&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;heappush&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;heappop&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;heapify&lt;/span&gt;

&lt;span class="n"&gt;heap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;26&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;19&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;heapify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The heap is :&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;&amp;gt;&amp;gt;&amp;gt; "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;heappush&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"After inserting 9 &lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;&amp;gt;&amp;gt;&amp;gt; "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;root&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;heappop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The popped value: "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The left over heap: &lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;&amp;gt;&amp;gt;&amp;gt; "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;heap&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: By default, the heap created is min-heap. in case max-heap implementation is needed, we can use this library instead or multiply our value with -1:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;https://pypi.org/project/heapq_max/&lt;/code&gt;&lt;/p&gt;




&lt;p&gt;Hope it helps to get started with Heap. Cheers! 😊&lt;/p&gt;

</description>
      <category>python</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
