<?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: 221910301033</title>
    <description>The latest articles on DEV Community by 221910301033 (@vish).</description>
    <link>https://dev.to/vish</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%2F613667%2Fef974db3-d8de-4182-9e69-4c1176f9f65f.png</url>
      <title>DEV Community: 221910301033</title>
      <link>https://dev.to/vish</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vish"/>
    <language>en</language>
    <item>
      <title>Heap Queue in Python</title>
      <dc:creator>221910301033</dc:creator>
      <pubDate>Tue, 13 Apr 2021 13:05:34 +0000</pubDate>
      <link>https://dev.to/vish/heap-queue-in-python-3jj4</link>
      <guid>https://dev.to/vish/heap-queue-in-python-3jj4</guid>
      <description>&lt;p&gt;A &lt;strong&gt;priority queue&lt;/strong&gt; is represented by a heap data structure. It is available in Python through the &lt;strong&gt;“heapq”&lt;/strong&gt; module. &lt;/p&gt;

&lt;p&gt;In Python, the property of this data structure is that the smallest of the heap elements is popped each time (min heap). The heap structure is preserved while components are &lt;strong&gt;moved or popped&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Every time, the &lt;em&gt;heap[0]&lt;/em&gt; element returns the smallest element.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Let's look at some important&lt;/em&gt; &lt;strong&gt;heap operations&lt;/strong&gt; :&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;-&lt;/strong&gt; &lt;strong&gt;heapify(iterable)&lt;/strong&gt; :- The &lt;strong&gt;iterable&lt;/strong&gt; is converted into a heap data structure using this feature. i.e. in &lt;em&gt;heap order&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;-&lt;/strong&gt; &lt;strong&gt;heappush(heap, ele)&lt;/strong&gt; :- This function inserts the element specified in the arguments into the heap. The order is changed to keep the &lt;em&gt;heap structure&lt;/em&gt; intact.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;-&lt;/strong&gt; &lt;strong&gt;heappop(heap)&lt;/strong&gt; :- This function is used to &lt;strong&gt;delete&lt;/strong&gt; the &lt;em&gt;smallest element from the heap&lt;/em&gt; and return it. The order is changed to keep the heap structure intact.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Python code to demonstrate working of
# heapify(), heappush() and heappop()

# importing "heapq" to implement heap queue
import heapq

# initializing list
li = [5, 7, 9, 1, 3]

# using heapify to convert list into heap
heapq.heapify(li)

# printing created heap
print ("The created heap is : ",end="")
print (list(li))

# using heappush() to push elements into heap
# pushes 4
heapq.heappush(li,4)

# printing modified heap
print ("The modified heap after push is : ",end="")
print (list(li))

# using heappop() to pop smallest element
print ("The popped and smallest element is : ",end="")
print (heapq.heappop(li))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt; :&lt;/p&gt;

&lt;p&gt;&lt;code&gt;The created heap is : [1, 3, 9, 7, 5]&lt;br&gt;
The modified heap after push is : [1, 3, 4, 7, 5, 9]&lt;br&gt;
The popped and smallest element is : 1&lt;/code&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>heap</category>
      <category>functions</category>
    </item>
  </channel>
</rss>
