# An array-based implementation of the max-heap.class MaxHeap :# Create a max-heap with maximum capacity of maxSize.def __init__( self, maxSize ):self._elements = Array( maxSize )self._count = 0# Return the number of items in the heap.def __len__( self ):return self._count# Return the maximum capacity of the heap.def capacity( self ):return len( self._elements )# Add a new value to the heap.def add( self, value ):assert self._count < self.capacity(), "Cannot add to a full heap."# Add the new value to the end of the list.self._elements[ self._count ] = valueself._count += 1# Sift the new value up the tree.self._siftUp( self._count - 1 )# Extract the maximum value from the heap.def extract( self ):assert self._count > 0, "Cannot extract from an empty heap."# Save the root value and copy the last heap value to the root.value = self._elements[0]self._count -= 1self._elements[0] = self._elements[ self._count ]# Sift the root value down the tree.self._siftDown( 0 )# Sift the value at the ndx element up the tree.def _siftUp( self, ndx ):if ndx > 0 :parent = ndx // 2# swap elementsif self._elements[ndx] > self._elements[parent] :tmp = self._elements[ndx]self._elements[ndx] = self._elements[parent]self._elements[parent] = tmpself._siftUp( parent )# Sift the value at the ndx element down the tree.def _siftDown( self, ndx ):left = 2 * ndx + 1right = 2 * ndx + 2# Determine which node contains the larger value.largest = ndxif left < count and self._elements[left] >= self._elements[largest] :largest = leftelif right < count and self._elements[right] >= self._elements[largest]:largest = right# If the largest value is not in the current node (ndx), swap it with# the largest value and repeat the process.if largest != ndx :swap( self._elements[ndx], self._elements[largest] )self._siftDown( largest )
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。