Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding how to create a heap in Python

Tags:

python

heap

The collections.Count.most_common function in Python uses the heapq module to return the count of the most common word in a file, for instance.

I have traced through the heapq.py file, but I'm having a bit of trouble understanding how a heap is created/updated with respect to words let's say.

So, I think the best way for me to understand it, is to figure out how to create a heap from scratch.

Can someone provide a pseudocode for creating a heap that would represent word count?

like image 604
Sam Hammamy Avatar asked Oct 05 '12 15:10

Sam Hammamy


People also ask

How do you construct a heap?

To build a max heap, you:Assign it a value. Compare the value of the child node with the parent node. Swap nodes if the value of the parent is less than that of either child (to the left or right). Repeat until the largest element is at the root parent nodes (then you can say that the heap property holds).

How are heaps implemented in Python?

In the heap data structure, we assign key-value or weight to every node of the tree. Now, the root node key value is compared with the children's nodes and then the tree is arranged accordingly into two categories i.e., max-heap and min-heap.


1 Answers

In Python 2.X and 3.x, heaps are supported through an importable library, heapq. It supplies numerous functions to work with the heap data structure modelled in a Python list. Example:

>>> from heapq import heappush, heappop >>> heap = [] >>> data = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0] >>> for item in data:         heappush(heap, item)  >>> ordered = [] >>> while heap:         ordered.append(heappop(heap))  >>> ordered [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> data.sort() >>> data == ordered True 

You can find out more about Heap functions: heappush, heappop, heappushpop, heapify, heapreplace in heap python docs.

like image 88
Hueston Rido Avatar answered Oct 14 '22 17:10

Hueston Rido