码迷,mamicode.com
首页 > 编程语言 > 详细

maxHeap 的 python 实现

时间:2018-08-20 13:13:41      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:dom   class   elf   copy from   实现   imp   pop   swa   range   

# -*- 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 实现

标签:dom   class   elf   copy from   实现   imp   pop   swa   range   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!