Python implementation Priority Queue (i)

Source: Internet
Author: User
Tags for in range python list

Learned the heap sort, implemented a priority queue structure using Python, and recorded the implementation process:

Use a Python list to represent the heap structure, pass the list as a parameter into the constructor, and then build the heap in the constructor:

class prioqueue:     def __init__ (Self, elist=[]):         = List (elist)        if  elist:            self.buildheap ()

Heap is generally a complete binary tree, then according to the nature of the complete binary tree, a node I of the left child node is i+1, the right child node is i*2+1, with the smallest heap for example, the root node must be the minimum value, priority queue must ensure that the value of each popup is the smallest. The process of building a heap is a recursive process, first of all, take out the first element in the list, an element must be a heap, and then take 2 elements from the list, respectively, as 2 sub-heap, to the first element this heap and the last 2 elements of the sub-heap composed of a heap, it is necessary to ensure that the value of the heap First judge the size of the left and right 2 sub-heap, and then with the first element and the smaller comparison, if small, then unchanged, if large, then the element and the sub-heap that element to replace the position, so that the formation of a minimum of 3 elements of the heap, and then take the second element from the list, from the sorted heap once this step Then jump out of the loop to take out the next element, if the larger Exchange 2 elements of comparison, and then use this element and 2 sub-nodes of the element to compare, until the entire binary tree is traversed no larger elements, then this element will be placed at the end of the element. This is the process of starting a downward scan from the top of the heap, the two-pronged root node, defining the Siftdown method first:

def Siftdown (self, E,begin, end):         = Self._elems, Begin, Begin*2+1

E is the element to be sorted, the variable I stores the position where we start the sort, in order not to waste space, the begin parameter is used as an index in the list of sorted and bit-ranked elements, so the start is incremented after 0, and the end parameter represents the number of heap elements, which is Len (list).

We then use the Elems variable to hold the array I as the pointer to the ordering element J to loop through the left subtree of I. As long as J doesn't go through the loop until the end.

 while J < end:

Then compare the left and right sub-tree, using the smaller Shirai and inserted e to do the comparison, if E is less than it then find the position, or the swap position continues with the change of position after the sub-tree comparison, I position is where the element e is located:

if  and elems[j+1] < elems[j]:                = j + 1                     if e < elems[j]:                break = elems[j ]            = j, 2*j+1        = e             

A downward scan of every element of the list allows you to complete a heap-building process and get a minimal heap:

    def buildheap (self):         = Len (self._elems)        for in range ( end//2,-1,-1):            Self.siftdown (self._elems[i ],i,end)        return self._elems

Python implementation Priority Queue (i)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.