Heap Introduction
Binary heap can be seen as a near-complete binary tree. The parent that can be divided into max-heap and min-heap,max-heap is larger than children, min-heap opposite.
There are two basic features in the heap that are usually composed of array a: 1. A.length, the number of elements in the array is given. 2. A.heap-size, give the empty array column the element in the heap.
The difference between the two is that the a.heap-size is in the heap, and the length of the a.length may contain many other elements.
This can be seen in the code that follows. For example, there is a list of 8 elements, but only the first 7 elements in the heap, satisfying the heap characteristics, then a.length=8, a.heap-size=7.
The heap and array are as follows.
Some features:
For each node, to know its parent and child, there are the following relationships:
PARENT (i)
RETURN[I/2]
Left (i)
Return[2i]
Right (i)
Return (2I+1)
Note that here I is starting from 1, in the actual write code is, the general list sequence is starting from 0, pay attention to the difference between the two. This will be reflected in the code that follows.
To convert a A[1...N] into a heap, all the elements in a ([N/2]+1...N) are the leaves of the heap.
Operations that maintain MAX-HEAP characteristics
This function, combined with the build Max heap, is key to maintaining the entire heap feature. The following is a discussion of how to implement
Create a Max heap
The first is to implement the Max heap starting from a node
The algorithm is as follows:
The time complexity of this process is O (logn)
On this basis, the algorithm for creating a Max heap is as follows:
The time complexity of this process is O (NLOGN)
The priority-queue operation of the heap
Prority-queue is a data structure, each of its elements has a key value, mainly has the following several operations, its use of the heap implementation of the algorithm as follows.
Insert an Element
The time complexity of this process is O (logn)
Increase the value of an element
The time complexity of this process is O (logn)
Extracting the largest element
The time complexity of this process is O (logn)
One of the important features of the heap is that it can complete all priority-queue operations within the time complexity of LGN.
Application of heap: heap sort
The whole process is divided into
- Build heap
- Extract Maximum Value
- Put it at the end of the sequence
- Repeat guided sorting complete
The specific algorithm is as follows:
Realize
#heapclass Heap (object): Def __init__ (self,a): "" "A is a list" "" # self.list = [0] Self.list = a[:] # Self.list.insert (0,0) self.heapsize = Len (A) # self.ismaxheap = False Self . Build_max_heap () def __str__ (self): return str (self.list) def left (self, i): return (i+1)-1 def Right (self, I): Return (I+1) def-parent (self, I): return (i-1)/2 def insert (self, key): "" " Run time O (logn) "" "Self.heapsize + = 1 self.list.append (-float (" INF ")) Self.increase_ke Y (Self.heapsize-1,key) def increase_key (Self,i,key): "" "Run Time O (logn)" "" If key < S Elf.list[i]: Raise ValueError ("New key is smaller than current key") self.list[i] = key while I&G T;0 and Self.list[self.parent (i)] < Self.list[i]: # print ("i1=" +str (i)) temp = Self.list[i] Self.list[i] = SelF.list[self.parent (i)] self.list[self.parent (i)] = temp i = self.parent (i) # print ("i2=" + STR (i)) def max_heapify (self, I): "" "Run Time:o (LOGN) The key to maintain the Max-heap property "" "L = Self.left (i) r = self.right (i) # i_m = i # largest = 0 # print (" L= "+str ( L) + "r=" +str (R)) # Print (self.heapsize) largest = i if L < self.heapsize:if self.list[ L] > Self.list[i]: largest = l # print ("r=" +str (R) + "largest=" +str (largest)) if R < Sel F.heapsize:if Self.list[r] > self.list[largest]: largest = r # print ("largest=" +str (l argest)) if largest! = I:temp = Self.list[i] self.list[i] = self.list[largest] se Lf.list[largest] = temp Self.max_heapify (largest) def build_max_heap (self): "" "Run Time:o (nl OGN) "" "# Print (SELF.HEAPSIZE//2) for I in Range (self.heapsize//2-1,-1,-1): # print (i) self.max_heapif Y (i) # self.ismaxheap = True def extract_max (self): "" "Build a maxheap and return the max value O F It (the first element) also pop the first element out of the heap run Time O (logn) "" "If sel F.heapsize < 1:raise valueerror ("Heap underflow") # If self.ismaxheap = False: # Self.bu Ild_max_heap () Maxele = self.list[0] self.list[0] = self.list[self.heapsize-1] Self.list[self.heapsiz E-1] = Maxele self.heapsize-= 1 self.max_heapify (0) return Maxele def heap_sort (self): "" " Using recurrence to Impelete, can also with for loop "" "If self.heapsize = = 1:return sel F.list self.build_max_heap () temp = self.list[0] self.list[0] = self.list[self.heapsize-1] Sel F.LIST[SELF.HEAPSIZE-1]= Temp Self.heapsize-= 1 Self.heap_sort () A = [3,4,5,7,1,4,9]heap1 = Heap (A) print (HEAP1) Heap1.insert (2) Print (HEAP1) print (heap1.heapsize) Maxele = Heap1.extract_max () print (Maxele) print (HEAP1) print (heap1.heapsize) Heap1.increase_key (3,10) print (HEAP1) print (heap1.heapsize) heap1.heap_sort () print (HEAP1) print (heap1.heapsize)
Sorting algorithm: Heap sort (including heap description, Python)