zoukankan      html  css  js  c++  java
  • python实现最大堆与最小堆

    转载:https://blog.csdn.net/Fire_KAKA/article/details/78297016

    class MaxHeap(object): # def __init__(self): #   self.data = []  # 创建堆 #   self.count = len(self.data)  # 元素数量 def __init__(self, arr): self.data = copy.copy(arr) self.count = len(self.data) i = self.count / 2 while i >= 1: self.shiftDown(i) i -= 1 def size(self): return self.count def isEmpty(self): return self.count == 0 def insert(self, item): # 插入元素入堆 self.data.append(item) self.count += 1 self.shiftup(self.count) def shiftup(self, count): # 将插入的元素放到合适位置,保持最大堆 while count > 1 and self.data[(count/2)-1] < self.data[count-1]: self.data[(count/2)-1], self.data[count-1] = self.data[count-1], self.data[(count/2)-1] count /= 2 def extractMax(self): # 出堆 if self.count > 0: ret = self.data[0] self.data[0], self.data[self.count-1] = self.data[self.count-1], self.data[0] self.data.pop() self.count -= 1 self.shiftDown(1) return ret def shiftDown(self, count): # 将堆的索引位置元素向下移动到合适位置,保持最大堆 while 2 * count <= self.count : # 证明有孩子 j = 2 * count if j + 1 <= self.count: # 证明有右孩子 if self.data[j] > self.data[j-1]: j += 1 if self.data[count-1] >= self.data[j-1]: # 堆的索引位置已经大于两个孩子节点,不需要交换了 break self.data[count-1], self.data[j-1] = self.data[j-1], self.data[count-1] count = j
    ---------------------
    作者:Fire_KAKA
    来源:CSDN
    原文:https://blog.csdn.net/Fire_KAKA/article/details/78297016
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    bzoj 4012: [HNOI2015]开店
    POJ 1054 The Troublesome Frog
    POJ 3171 Cleaning Shifts
    POJ 3411 Paid Roads
    POJ 3045 Cow Acrobats
    POJ 1742 Coins
    POJ 3181 Dollar Dayz
    POJ 3040 Allowance
    POJ 3666 Making the Grade
    洛谷 P3657 [USACO17FEB]Why Did the Cow Cross the Road II P
  • 原文地址:https://www.cnblogs.com/siconglove/p/10557209.html
Copyright © 2011-2022 走看看