Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita5f8fe0

Browse files
author
ssjssh
committed
斐波那契树完成插入和extra_min
1 parentffcb064 commita5f8fe0

File tree

1 file changed

+168
-0
lines changed

1 file changed

+168
-0
lines changed

‎src/ssj/heap/fibonacci_heap.py

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
#!/usr/bin/env python
2+
# -*- coding:UTF-8
3+
__author__='shenshijun'
4+
5+
6+
classFibonacciHeap(object):
7+
"""实现斐波那契堆,斐波那契堆在一些操作执行的时候负责度比较低,而其他操作的复杂度和普通的堆是一样的"""
8+
9+
classNode(object):
10+
"""节点实现"""
11+
12+
def__init__(self,key,parent=None,child=None,left=None,right=None):
13+
"""Constructor for """
14+
self.key=key
15+
self.degree=0
16+
self.mark=False
17+
self.parent=parent
18+
self.child=child
19+
self.left=leftorself
20+
self.right=rightorself
21+
22+
def__str__(self):
23+
returnstr(self.key)+" : "+str(self.degree)
24+
25+
def__init__(self,*argv):
26+
"""Constructor for """
27+
self.__root=None
28+
self.__size=0
29+
map(self.insert,argv)
30+
31+
definsert(self,key):
32+
self.__size+=1
33+
ifself.__rootisNone:
34+
self.__root=self.Node(key)
35+
else:
36+
new_node=self.Node(key,left=self.__root.left,right=self.__root)
37+
self.__root.left.right=new_node
38+
self.__root.left=new_node
39+
ifnew_node.key<self.__root.key:
40+
self.__root=new_node
41+
42+
defextract_min(self):
43+
ifself.__rootisNone:
44+
returnNone
45+
46+
result=self.__root
47+
self.__size-=1
48+
49+
# 首先把根节点的子节点全部移到根链表上
50+
child=self.__root.child
51+
foriinxrange(0,self.__root.degree):
52+
next_node=child.right
53+
self.__root.left.right=child
54+
child.left=self.__root.left
55+
self.__root.left=child
56+
child.right=self.__root
57+
child.parent=None
58+
child=next_node
59+
60+
# 移除根节点
61+
self.__root.left.right=self.__root.right
62+
self.__root.right.left=self.__root.left
63+
ifself.__root.rightisself.__root:
64+
self.__root=None
65+
else:
66+
self.__root=self.__root.right
67+
self.__extract_consolidating()
68+
69+
returnresult.key
70+
71+
def__extract_consolidating(self):
72+
# 合并根链表
73+
node_degrees= {}
74+
pass_root=False
75+
cur_node=self.__root
76+
after_merge=False
77+
next_node=None
78+
whilecur_nodeisnotself.__rootornotpass_rootorafter_merge:
79+
ifcur_node==self.__root:
80+
pass_root=True
81+
82+
ifnotafter_merge:
83+
next_node=cur_node.right
84+
85+
d=cur_node.degree
86+
ifdinnode_degrees:
87+
other_node=node_degrees[d]
88+
sm_node=cur_nodeifcur_node.key<=other_node.keyelseother_node
89+
bg_node=cur_nodeifcur_node.key>other_node.keyelseother_node
90+
# 把大的节点连接到小的节点上面,然后继续在sm_node上面循环,因为sm_node的度数发生了变化
91+
self.__link_to(bg_node,sm_node)
92+
delnode_degrees[d]
93+
after_merge=True
94+
cur_node=sm_node
95+
else:
96+
after_merge=False
97+
node_degrees[d]=cur_node
98+
cur_node=next_node
99+
100+
self.__root=None
101+
102+
ford,nodeinnode_degrees.iteritems():
103+
node.parent=None
104+
ifself.__rootisNone:
105+
self.__root=node
106+
self.__root.left=node
107+
self.__root.right=node
108+
else:
109+
node.left=self.__root.left
110+
node.right=self.__root
111+
self.__root.left.right=node
112+
self.__root.left=node
113+
114+
ifself.__root.key>node.key:
115+
self.__root=node
116+
117+
@staticmethod
118+
def__link_to(src,dest):
119+
"""
120+
bugfix: 不能把src从根链表移除,这样的话会破坏根链表,导致在__extract_consolidating中的while找不到退出条件
121+
我是为了避免重新创建根链表而把src从根链表中移除,这段注释只是为了提醒
122+
:param src:
123+
:param dest:
124+
:return:
125+
"""
126+
src.mark=False
127+
128+
dest_child=dest.child
129+
src.parent=dest
130+
ifdest_childisNone:
131+
dest.child=src
132+
src.left=src
133+
src.right=src
134+
else:
135+
src.left=dest_child.left
136+
dest_child.left.right=src
137+
dest_child.left=src
138+
src.right=dest_child
139+
dest.degree+=1
140+
141+
defunion(self,other):
142+
pass
143+
144+
defmin(self):
145+
returnNoneifself.__rootisNoneelseself.__root.key
146+
147+
defchange_key(self,node,new_key):
148+
pass
149+
150+
def__len__(self):
151+
returnself.__size
152+
153+
defdelete(self,node):
154+
self.change_key(node,float('-Inf'))
155+
self.extract_min()
156+
157+
158+
defmain():
159+
test= ['O','J','S','Y','C','M','B','R','N','F','L','Z','U','Q','A','G','V','E','D','W','I',
160+
'H','T','K','X','P']
161+
162+
heap=FibonacciHeap(*test)
163+
foriinxrange(0,len(heap)):
164+
print(heap.extract_min())
165+
166+
167+
if__name__=="__main__":
168+
main()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp