Here is an example of how to implement a two fork heap and a heap ordering under Python. Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.
The heap is a special kind of tree structure, and the data storage in the heap satisfies certain heap order. Heap sorting is a sort of selection, its algorithm complexity, time complexity relative to other sorting algorithms have a great advantage.
Heap is divided into big head heap and small pile, as its name, the first element of the big head heap is the largest, each has a child node of the parent node, its data value is greater than its child node value. The small head heap is the opposite.
I'll walk you through the algorithmic process of building a tree-shaped heap:
Locate the array data for the N/2 location, starting from this position, we find the index of the node's left dial hand nodes, compare the sub-nodes at the junction, find the largest one, assign the index of the largest child node to the left Dial hand node, and then compare the largest child node to the parent node, and exchange the data with the parent if it is larger than the parent. Of course, I just probably said the next implementation, in this process, you also need to consider the situation where the node does not exist.
Look at the code:
# Build Binary heap def binaryheap (arr, Lenth, m): temp = arr[m] # current node value while (2*m+1 < Lenth): lchild = 2*m+1 if l Child! = Lenth-1 and Arr[lchild] < Arr[lchild + 1]: lchild = lchild + 1 If temp < Arr[lchild]: arr[m] = Arr[lchild] else: break m = lchild arr[m] = temp def heapsort (arr, length): i = int (len ( ARR)/2) while (i >= 0): binaryheap (arr, Len (arr), i) i = i-1 print ("The physical order of the binary heap is:") print ( ARR) # The physical order of the output binary heap if __name__ = = ' __main__ ': arr = [2, 6, X, A, x, A, X, 98] heapsort (arr, Len (arr))
The heap sort process is to compare the last node to the first one in turn:
# Build Binary heap def binaryheap (arr, Lenth, m): temp = arr[m] # current node value while (2*m+1 < Lenth): lchild = 2*m+1 if LC Hild! = lenth-1 and Arr[lchild] < Arr[lchild + 1]: lchild = lchild + 1 If temp < Arr[lchild]: arr[m] = Arr[lchild] else: break m = lchild arr[m] = tempdef heapsort (arr, length): i = int (len (arr)/2) C12/>while (i >= 0): binaryheap (arr, Len (arr), i) i = i-1 print ("The physical order of the binary heap is:") print (arr) # The physical order of the output binary heap i = length-1 while (i > 0): arr[i], arr[0] = arr[0], arr[i] # variable Exchange binaryheap (arr, I, 0) C21/>i = i-1560def Pop (arr): First = Arr.pop (0) return firstif __name__ = = ' __main__ ': arr = [2, 87, 39, , 6, 98] heapsort (arr, Len (arr)) print ("Heap sorted physical order") print (arr) # Output The physical order after the heap is sorted data = Pop (arr) print (data) print (arr)
Python encapsulates a heap module that we use to efficiently implement a priority queue
Import Heapqclass Item: def __init__ (self, name): self.name = name def __repr__ (self): return ' Item ({! R}) '. Format (Self.name) class Priorityqueue: def __init__ (self): self._queue = [] self._index = 0 def Push (self, item, priority): Heapq.heappush (Self._queue, (-priority, Self._index, item)) # Deposit a ternary group Self._index + = 1 def pop (self): return Heapq.heappop (Self._queue) [-1] # reverse Output if __name__ = = ' __main__ ': p = Priorityqueue () P.push (item (' foo '), 1) P.push (item (' Bar '), 5) P.push (item (' Spam '), 4) P.push ( Item (' Grok '), 1) print (P.pop ()) print (P.pop ())
Please see HEAPQ website for details.