python中binomial_二项堆python实现——eager binomial heap
关于eager binomial heaps的内容详见算法设计搬运(3)——Heaps,更多细节在后面的Lazy Binomial Heaps也有。比较懒用的双向链表写的。双向链表可以实现
的 decreaseKey 和 deleteMin,可以参考石溪的ppt,尽管它是用array来存heap指针。有的ppt就错得离谱,单向链表decreaseKey就不只是在树的一个路径上比较了,没法
找它的left。
资源已上传,新手上路,就只当为熟悉python指针练手...
# author: zyx
# eager binomial heaps
# merge,insert,find_min,extractMin
class Binomial_Heap():
def __init__(self):
self.head = None
self.min = None
def merge(self,H2):
if not self.min:
self.min = H2.min
elif not H2.min:
return
else:
if self.min.data > H2.min.data:
self.min = H2.min
h1 = self.head
h2 = H2.head
unique_order_heap = Binomial_Heap()
c = unique_order_heap.head
while h1 and h2:
if h1.order == h2.order:
if h1.data <= h2.data:
h1.order+=1
h2.pre = None
d = h2
h2 = h2.sibling
d.sibling = h1.Lchild
d.parent = h1
h1.Lchild = d
if unique_order_heap.head == None:
unique_order_heap.head = h1
c = h1
h1.pre = None
h1 = h1.sibling
else:
h1.pre = c
c.sibling = h1
h1 = h1.sibling
c = c.sibling
else:
h2.order += 1
h1.pre = None
d = h1
h1 = h1.sibling
d.sibling = h2.Lchild
d.parent = h2
h2.Lchild = d
if unique_order_heap.head == None:
unique_order_heap.head = h2
c = h2
h2.pre = None
h2 = h2.sibling
else:
h2.pre = c
c.sibling = h2
h2 = h2.sibling
c = c.sibling
elif h1.order < h2.order:
if unique_order_heap.head == None:
unique_order_heap.head = h1
c = unique_order_heap.head
h1.pre = None
h1 = h1.sibling
else:
if h1.order == c.order:
if c.data <= h1.data:
c.order += 1
d = h1
h1.pre = None
h1 = h1.sibling
d.sibling = c.Lchild
c.Lchild = d
d.parent = c
else:
if c.pre == None: # single B
unique_order_heap.head = h1
h1.pre = None
else:
c.pre.sibling = h1
h1.pre = c.pre
h1.order += 1
c.sibling = h1.Lchild
h1.Lchild = c
c.parent = h1
c = h1
h1 = h1.sibling
else:
c.sibling = h1
h1.pre = c
h1 = h1.sibling
c = c.sibling
else:
if unique_order_heap.head == None:
unique_order_heap.head = h2
c = unique_order_heap.head
h2.pre = None
h2 = h2.sibling
else:
if h2.order == c.order:
if c.data <= h2.data:
d = h2
c.order += 1
h2 = h2.sibling
d.sibling = c.Lchild
c.Lchild = d
d.parent = c
else:
if c.pre == None: # single B
unique_order_heap.head = h2
h2.pre = None
else:
c.pre.sibling = h2
h2.pre = c.pre
h2.order += 1
c.sibling = h2.Lchild
h2.Lchild = c
c.parent = h2
c = h2
h2 = h2.sibling
else:
c.sibling = h2
h2.pre = c
h2 = h2.sibling
c = c.sibling
if c:
c.sibling = None
while h1:
if c:
if h1.order == c.order:
if c.data <= h1.data:
c.order += 1
d = h1
h1.pre = None
h1 = h1.sibling
d.sibling = c.Lchild
c.Lchild = d
d.parent = c
else:
if c.pre == None: # single B
unique_order_heap.head = h1
h1.pre = None
else:
c.pre.sibling = h1
h1.pre = c.pre
h1.order += 1
c.sibling = h1.Lchild
h1.Lchild = c
c.parent = h1
c = h1
h1 = h1.sibling
else:
c.sibling = h1
h1.pre = c
break
else:
unique_order_heap.head = h1
h1.pre = None
break
while h2:
if c:
if h2.order == c.order:
if c.data <= h2.data:
d = h2
c.order += 1
h2 = h2.sibling
d.sibling = c.Lchild
c.Lchild = d
d.parent = c
else:
if c.pre == None: # single B
unique_order_heap.head = h2
h2.pre = None
else:
c.pre.sibling = h2
h2.pre = c.pre
h2.order += 1
c.sibling = h2.Lchild
h2.Lchild = c
c.parent = h2
c = h2
h2 = h2.sibling
else:
c.sibling = h2
h2.pre = c
break
else:
unique_order_heap.head = h2
h2.pre = None
break
self.head = unique_order_heap.head
def find_Min(self):
return self.min
def insert(self,item):
node = Binomial_Trees(item)
H2 = Binomial_Heap()
H2.head = node
H2.min = node
self.merge(H2)
def extractMin(self):
m = self.min
if m.pre:
m.pre.sibling = m.sibling
else:
self.head = m.sibling
if m.sibling:
m.sibling.pre = m.pre
new_heap = Binomial_Heap()
to_transform = m.Lchild
minimum = to_transform
while to_transform:
if minimum.data > to_transform.data:
minimum = to_transform
to_transform.parent = None
if not new_heap.head:
new_heap.head = to_transform
to_transform = to_transform.sibling
new_heap.head.sibling = None
else:
d = to_transform
to_transform = to_transform.sibling
d.sibling = new_heap.head
new_heap.head.pre = d
new_heap.head = d
new_heap.min = minimum
cur = self.head
minimum = self.head
while cur:
if minimum.data < cur.data:
minimum = cur
cur = cur.sibling
self.min = minimum
self.merge(new_heap)
return m.data
class Binomial_Trees():
def __init__(self,item):
self.order = 0
self.parent = None
self.sibling = None
self.Lchild = None
self.data = item
self.pre = None
if __name__ == '__main__':
a = Binomial_Heap()
a.insert(1)
a.insert(2)
a.insert(3)
a.insert(4)
a.insert(-1)
a.extractMin()
a.extractMin()
b = Binomial_Heap()
b.insert(2)
b.insert(5)
b.insert(0)
b.insert(3)
a.merge(b)
a.insert(-1)
python中binomial_二项堆python实现——eager binomial heap相关推荐
- python优先队列的库,python优先队列及二叉堆的实现
python优先队列及二叉堆的实现 发布于 2015-12-18 06:55:17 | 117 次阅读 | 评论: 0 | 来源: PHPERZ Python编程语言Python 是一种面向对象.解释 ...
- python中的二维数组_Python中的二维数组实例(list与numpy.array)
关于python中的二维数组,主要有list和numpy.array两种. 好吧,其实还有matrices,但它必须是2维的,而numpy arrays (ndarrays) 可以是多维的. 我们主要 ...
- python中的二维数组与一维数组,矩阵和一维数组之间的运算
一.python中的二维数组和一维数组之间的运算 1.当二维数组为p×p维 a=np.array([[1,2],[3,4]]) print(a.shape) a 输出: (2, 2) array([[ ...
- linux 移除python_第16 p,PYthon中的用户交互,Python GUI编程
大家好,我是杨数Tos,这是<从零基础到大神>系列课程的第16篇文章,第二阶段的课程:Python基础知识:PYthon中的用户交互.Python GUI编程实现方式介绍. 学习本课程,建 ...
- 可合并堆1:二项堆(Binominal Heap)
二项堆是一种可合并队,其合并的复杂度为.二项堆与二项树有着天然的联系,解析二项堆之前,先解析二项树. 1k阶二项树是一个递归定义: (1)单个节点是一颗二项树,表示为 (2)二项树由两颗二项树组成 ( ...
- 斐波那契堆的实现和比较(相对二项堆)
Fibonacci Heap(简称F-Heap)是一种基于二项堆的非常灵活的数据结构.它与二项堆不同的地方在于: 1)root list和任何结点的child list使用双向循环链表,而且这些lis ...
- 如何在Python中获取图片分辨率?——Python实现获取图片分辨率的代码及详解。
如何在Python中获取图片分辨率?--Python实现获取图片分辨率的代码及详解. 在进行图片处理或者图片分析的时候,获取图片的分辨率信息是必不可少的.Python提供了许多库可以方便地获取图片的分 ...
- 半路算法之二项树与二项堆
半路算法之二项树与二项堆 秉承前文.前面提到过针对稀疏图时,以二项堆来作为最小优先队列是更为适宜的.这里记录一下学习二项堆的过程. 二项堆的名字来源于二项树.二项堆是多个二项树连在了一起. 二项树的名 ...
- 算法设计与分析——二项堆
引入 上一篇博课-二叉堆介绍了如何使用具有微弱限制的二叉树--要求父节点上的元素同时大于等于子节点或同时小于等于子节点--这种数据结构来实现一个优先队列,我们看到基于二叉堆实现的优先队列的各个基本操作 ...
- python中with是什么意思,Python中With的用法
在看Dive Into Python中有关描述文件读写那章节的时候,看到了有关with的用法,查阅下相关资料,记录下来,以备后用. 官方的reference上有关with statement是这样说的 ...
最新文章
- 学习完Ajax的总结
- Linux: /dev/fd/详解
- Linux系统下如何查看及修改文件读写权限
- 十进制转二进制 java 程序_如何把一个十进制数转为二进制数的Java程序?
- linux文件存储结构,Linux文件存储方式以及软硬链接
- 播放列表php代码,【求助】如何直接在Discuz! 代码 FLASH代码中添加优酷播放列表...
- loss函数之L1Loss,MSELoss,SmoothL1Loss, HuberLoss
- HoloLens1开发(二):结合Vuforia开发
- [No00002D] “大学生还不如农民工”背后的“身份教育”困境
- C#百度OCR-身份证图片识别提取信息
- linux怎么用中文显示,linux中文显示设置
- Office小知识(三)——使用visio绘制流程图实例及遇到的一些问题
- 船务公司简称与缩写一览表
- 《大数据之路:阿里巴巴大数据实践》-第2篇 数据模型篇 -第9章 阿里巴巴数据整合及管理体系
- 英特尔显卡不支持自定义分辨率N卡解决办法
- Linux的命名空间
- 360度评估前HR必须掌握的优劣势
- iOS中给图片加水印或文字的几种方法
- Windows多个桌面切换快捷键
- 大数据为什么用python_为什么大数据用python