# Python数据结构----最大堆

import array as Array
class MaxHeap:
def __init__(self,maxSize):
self._elements = Array(maxSize)
self._count = 0

def __len__( self ):
return self._count

def capacity( self ):
return len( self._elements )

assert self._count < len( self._elements ), ""
self._elements[self._count] = value
self._count += 1
self._siftUp( self._count - 1 )

#delete maximum from heap
def extract( self ):
assert self._count > 0,""
value = self._elements[0]
self._elements[0] = self._elements[self._count-1]
self._siftDown(0)

def _siftUp( self, ndx ):
if ndx > 0 :
parent = ndx // 2
if self._elements[ndx] > self._elements[parent]:
tmp = self._elements[parent]
self._elements[parent] = self._elements[ndx]
self._elements[ndx] = tmp
self._siftUp( parent )

def _siftDown( self, ndx ):
left = 2 * ndx + 1
right = 2* ndx + 2
largest = ndx
if left < count and  self._elements[left] >= self._elements[largest]:
largest = left
if right < count and  self._elements[right] >= self._elements[largest]:
largest = right
if largest != ndx:
swap( self._elements[ndx], self._elements[largest] )
_siftDown( largest )

#### 【算法——Python实现】最大堆和最小堆

2017-10-20 16:37:36

#### Python实现最大堆（大顶堆）

2018-05-01 20:41:09

#### python使用heapq实现小顶堆（TopK大）/大顶堆（BtmK小）

2017-02-16 19:16:10

#### Python的heapq模块实现大顶堆，小顶堆

2016-11-26 10:55:46

#### 用Python实现八大排序算法--堆排序

2016-11-03 09:30:08

#### 利用Python实现堆排序

2016-07-07 11:41:54

#### 浅析Python heapq模块 堆数据结构

2016-07-08 01:42:05

#### python数据结构学习笔记-2016-11-30-01-堆

2016-12-31 18:52:55

#### 堆与堆排序，最大索引堆（python实现）

2018-01-28 22:21:00

#### 用最大堆模拟最大优先级序列（Python实现）

2016-01-14 22:09:19