如何在斐波那契堆上的递减键操作中获得 O(1) 摊销复杂度?仅使用 BFS 在斐波那契堆中找到包含该元素的节点需要 O(n) 时间,这应该使得不可能获得 O(1) 摊销时间。
作为参考,这是我搜索相关节点的 BFS 实现:
public fHeapNode search(int x){
Stack<fHeapNode> stack = new Stack<fHeapNode>();
stack.push(minNode);
//Breath first searching
while (!stack.empty()) {
fHeapNode curr = stack.pop();
if(curr.data==x) {return curr;}
else{
if (curr.child != null) {
stack.push(curr.child);
}
fHeapNode start = curr;
curr = curr.right;
while (curr != start) {
if (curr.child != null) {
stack.push(curr.child);
}
if(curr.data==x) {return curr;}
curr = curr.right;
}
}
}
return null;
}
这是我的减少键代码:
public void decreaseKey(fHeapNode x, double k)
{
if (k > x.key) {
//throw new IllegalArgumentException("entered value is wrong");
}
x.key = k;
fHeapNode tempParent = x.parent;
if ((tempParent != null) && (x.key < tempParent.key)) {
cut(x,tempParent);
cascadingCut(tempParent);
}
if (x.key < minNode.key) {
minNode = x;
}
}