首页 > 编程语言 > 详细

maxHeap 的 python 实现

时间:2018-08-20 13:03:17      阅读:294      评论:0      收藏:0      [点我收藏+]
# -*- coding: UTF-8 -*-

# copy from website!
# 20180820

class MaxHeap(object):

def __init__(self):
self._data = []
self._count = len(self._data)

def size(self):
return self._count

def isEmpty(self):
return self._count == 0

def add(self, item):
self._data.append(item)
self._count += 1
self._shiftup(self._count - 1)

def pop(self):
if self._count > 0:
ret = self._data[0]
self._data[0] = self._data[self._count - 1]
self._data.pop()
self._count -= 1
self._shiftDown(0)
return ret

def _shiftup(self, index):
# 上移self._data[index],以使它不大于父节点
parent = (index - 1) >> 1
while index > 0 and self._data[parent] < self._data[index]:
# swap
self._data[parent], self._data[index] = self._data[index], self._data[parent]
index = parent
parent = (index - 1) >> 1

def _shiftDown(self, index):
# 移self._data[index],以使它不小于子节点
j = (index << 1) + 1
while j < self._count:
# 有子节点
if j + 1 < self._count and self._data[j + 1] > self._data[j]:
# 有右子节点,并且右子节点较大
j += 1
if self._data[index] >= self._data[j]:
# 堆的索引位置已经大于两个子节点,不需要交换了
break
self._data[index], self._data[j] = self._data[j], self._data[index]
index = j
j = (index << 1) + 1

if __name__ == ‘__main__‘:
## test code!
myHeap = MaxHeap()
import random
for i in range(10):
num = random.randint(-300, 300)
print(num)
myHeap.add(num)
print(myHeap._data)
for i in range(10):
num = myHeap.pop()
print num
for i in range(10):
num = random.randint(-300, 300)
print(num)
myHeap.add(num)
print(myHeap._data)
for i in range(10):
num = myHeap.pop()
print num

maxHeap 的 python 实现

原文:https://www.cnblogs.com/wh228/p/9504908.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!