Copy codeThe Code is as follows:
#-*-Coding: UTF-8 -*-
Class Heap (object ):
@ Classmethod
Def parent (cls, I ):
"Parent subscripts """
Return int (I-1)> 1 );
@ Classmethod
Def left (cls, I ):
"Left son subscript """
Return (I <1) + 1;
@ Classmethod
Def right (cls, I ):
"Right son subscript """
Return (I <1) + 2;
Class MaxPriorityQueue (list, Heap ):
@ Classmethod
Def max_heapify (cls, A, I, heap_size ):
"Maximizing heap A [I] as the root subtree """
L, r = cls. left (I), cls. right (I)
If l Largest = l
Else:
Largest = I
If r Largest = r
If largest! = I:
A [I], A [largest] = A [largest], A [I]
Cls. max_heapify (A, largest, heap_size)
Def maximum (self ):
"Returns the maximum element. The pseudo code is as follows:
HEAP-MAXIMUM (S)
1 return A [1]
T (n) = O (1)
"""
Return self [0]
Def extract_max (self ):
"Removes and returns the maximum element. The pseudo code is as follows:
HEAP-EXTRACT-MAX)
1 if heap-size [A] <1
2 then error "heap underflow"
3 max keys A [1]
4 A [1] specify A [heap-size [A] // place the end element first.
5 heap-size [A] ← heap-size [A]-1 // reduce heap-size [A]
6 MAX-HEAPIFY (A, 1) // maintain the maximum heap nature
7 return max
T (n) = θ (lgn)
"""
Heap_size = len (self)
Assert heap_size> 0, "heap underflow"
Val = self [0]
Tail = heap_size-1
Self [0] = self [tail]
Self. max_heapify (self, 0, tail)
Self. pop (tail)
Return val
Def increase_key (self, I, key ):
"Adds the I value to the key. The pseudo code is as follows:
HEAP-INCREASE-KEY (A, I, key)
1 if key <A [I]
2 the error "new key is smaller than current key"
3 A [I] primary key
4 while I> 1 and A [PARENT (I)] <A [I] // not the root node and the PARENT node is more hour-long
5 do exchange A [I]↔A [PARENT (I)] // exchange two elements
6 I usually PARENT (I) // point to the PARENT node location
T (n) = θ (lgn)
"""
Val = self [I]
Assert key> = val, "new key is smaller than current key"
Self [I] = key
Parent = self. parent
While I> 0 and self [parent (I)] <self [I]:
Self [I], self [parent (I)] = self [parent (I)], self [I]
I = parent (I)
Def insert (self, key ):
"Inserts the key into A. the pseudo code is as follows:
MAX-HEAP-INSERT (A, key)
1 heap-size [A] ← heap-size [A] + 1 // increase in the number of elements
2 A [heap-size [A] Random-∞ // the newly added element is-∞.
3 HEAP-INCREASE-KEY (A, heap-size [A], key) // Add the new element to the key
T (n) = θ (lgn)
"""
Self. append (float ('-inf '))
Self. increase_key (len (self)-1, key)
If _ name _ = '_ main __':
Import random
Keys = range (10)
Random. shuffle (keys)
Print (keys)
Queue = MaxPriorityQueue () # create the maximum heap by inserting
For I in keys:
Queue. insert (I)
Print (queue)
Print ('* 30)
For I in range (len (keys )):
Val = I % 3
If val = 0:
Val = queue. extract_max () # Remove and return the maximum element
Elif val = 1:
Val = queue. maximum () # returns the largest element.
Else:
Val = queue [1] + 10
Queue. increase_key (1, val) # increase queue [1] by 10
Print (queue, val)
Print ([queue. extract_max () for I in range (len (queue)])