ava斐波那契堆-斐波那契堆JAVA实现的问题

问题描述

斐波那契堆JAVA实现的问题

最近在做用斐波那契堆改进Prim算法的作业。但是Java代码调试了两个周还是有问题,只能正确输出前3项。
还有几天就要提交作业了,在次跪求大神们帮忙瞧瞧代码。
代码如下:
public class FibonacciNode {
FibonacciNode child, left, right, parent;

int vertex;
float element;
int degree;
Boolean mark;

/** Constructor **/

public FibonacciNode(int vertex, float element)
{
    this.right=this;
    this.left=this;
    this.parent=null;
    this.child=null;
    this.vertex=vertex;
    this.element=element;
    this.degree=0;
    this.mark=false;
}

}

public class FibonacciHeap {
FibonacciNode root;
int count;
public FibonacciHeap(){
root=null;
count=0;
}

//Return the number of nodes of the current heap
public int size(){
    return count;
}

//Judge if the heap is empty
public boolean isEmpty(){
    return root==null;
}

//Clear the whole heap
public void clear(){
    root=null;
    count=0;
}

//Insert a node to the heap.
public void insert(int vertex, Float element){

    FibonacciNode node=new FibonacciNode(vertex, element);
    if(root==null)
        root=node;
    else{
        addNode(node, root);
        if(root.element>node.element){
            root=node;
        }
    }
    count++;
}

//Add b to the tail of a
//Notify that a and b are both the heads of a double-linked list
private void catList(FibonacciNode a, FibonacciNode b){
      FibonacciNode tmp= a.right;
      a.right =b.right;
      b.right.left= a;
      b.right= tmp;
      tmp.left= b;
}

//Get the minimum node of the heap and remove it from the heap
public FibonacciNode extractMin(){

    if(root==null){
        return null;

    }

    if(root.child!=null){
        FibonacciNode m = root;
        FibonacciNode start=root.child;
        for(int i=0; i<m.degree; i++){
            if(start!=null){
                start.parent=null;
                addNode(start, root);
                start=start.right;
            }
        }
    }
    //remove root from the root list of heap
    FibonacciNode min=root;
    min.left.right=min.right;
    min.right.left=min.left;
    //if min.right==min, then the root of the heap has no child
    if(min.right==min){
        this.root=null;
    }
    else{
        root=min.right;
        consolidate();
    }
    //decrease the number of the nodes
    count--;    

    return min;
}

/*
// 将min每一个儿子(儿子和儿子的兄弟)都添加到"斐波那契堆的根链表"中
while (m.child != null){
    FibonacciNode child=m.child;

    removeNode(child);
    if(child.right==child)
        m.child=null;
    else
        m.child=child.right;

    addNode(child, min);
    child.parent=null;
}
*/

/*
if(min.child!=null){ //set all the min's child's parent as null
System.out.println("2:22222");
FibonacciNode startChild=min.child;
startChild.parent=null;
for(FibonacciNode x=startChild.right; x!=startChild; x=x.right){
x.parent=null;

        System.out.println("3:22222");
    }
    //merge the children to the root list
    catList(root, startChild);

}
*/

//unify two node if they have the same degree

private void consolidate() {
    FibonacciNode[] cons=new FibonacciNode[this.count];
    for(int i=0; i<this.count; i++)
        cons[i] = null;

    while (root!=null) {
        FibonacciNode x =root;
        if(x==x.right)
            root=null;
        else{
           removeNode(x);
           root=x.right;
       }
        int d=x.degree;
        while(cons[d]!=null) {
            FibonacciNode y=cons[d];
            if (x.element>y.element) {
                FibonacciNode tmp=x;
                x=y;
                y=tmp;
            }
            link(y, x);
            cons[d]=null;
            d++;
        }
        cons[d] = x;
    }
    root = null;
    for(int i=0; i<cons.length; i++){
        if(cons[i] != null) {
            if(root == null)
                root = cons[i];
            else{
                addNode(cons[i], root);
                if ((cons[i]).element < root.element)
                    root = cons[i];
            }
        }
    }
}

//remove node1 from the root list and make it as node2's child

private void link(FibonacciNode node1, FibonacciNode node2) {
    // remove node1 from the root list
    node1.left.right = node1.right;
    node1.right.left = node1.left;
    // set node as root's child
    if (node2.child == null)
        node2.child = node1;
    else{
      node1.right=node2.child.right;
      node2.child.right=node1;
      node1.left=node2.child;
      node1.right.left=node1;
    }
    node1.parent = node2;
    node2.degree++;
    node1.mark = false;
}
//add node to the list rooted at root
private void addNode(FibonacciNode node, FibonacciNode root) {
    node.left=root;
    node.right=root.right;
    root.right=node;
    node.right.left=node;
}

public void decreaseKey(FibonacciNode node, int key) {
    if (key > node.element) {
          System.out.println("decrease failed: the new key is no smaller than current key");
            return;
    }
    if (root==null||node==null)
        return;
    node.element=key;
    FibonacciNode parent = node.parent;
    //if parent is null or node's element is no smaller than it's parent's, nothing is needed to be done
    if (parent!=null&&(node.element<parent.element)) {
        //remove node and add it to the root list
        cut(node, parent);
        cascadingCut(parent);
    }
    // update the root node
    if (node.element<root.element)
        root=node;
}
private void removeNode(FibonacciNode node) {
    node.left.right = node.right;
    node.right.left = node.left;
}
private void renewDegree(FibonacciNode parent){
      parent.degree -= 1;
      if(parent. parent != null)
          renewDegree(parent.parent);
}

//remove node from it's parent and add it to the root list
private void cut(FibonacciNode node, FibonacciNode parent) {
     removeNode(node);
     renewDegree(parent);
     //node has no sibling
     if (node==node.right)
         parent.child=null;
     else
         parent.child=node.right;
     node.parent=null;
     node.left=node.right=node;
     node.mark=false;
     //add to the root list of heap
     addNode(node, root);
}

//recurse cut the parent' parent, until reach the root list
private void cascadingCut(FibonacciNode node) {
    FibonacciNode parent = node.parent;
    if (parent!=null) {
        if(node.mark==false)
            node.mark=true;
        else{
            cut(node, parent);
            cascadingCut(parent);
        }
    }
}
//Add heap other to the current heap
public void union(FibonacciHeap other) {
    if (this.root==null)   //this is empty, just return the othe
        this.root=other.root;
    else if((other.root)!=null) {// both this and other are not empty
          catList(this.root, other.root);
          if(this.root.element>other.root.element)
              this.root=other.root;
         }
    this.count=this.count+other.count;
    other=null;
    return;
}

}
测试程序:
public class FibonacciHeapTest {
public static void main(String[] args){
FibonacciHeap heap=new FibonacciHeap();
for(int i=10; i>0; i--){
heap.insert(9-i, (float)i);
}

for(int i=0; i<10; i++){
System.out.println(heap.extractMin().element);
}
}
}
运行结果:
1.0
2.0
3.0
4.0
Exception in thread "main" java.lang.NullPointerException
at MST.FibonacciHeapTest.main(FibonacciHeapTest.java:10)

解决方案

http://www.csdn123.com/html/mycsdn20140110/8e/8e18a40fb0eecc655cc068a1bb1d1ba4.html

解决方案二:

http://www.tuicool.com/articles/ryueAfu

解决方案三:

heap要设置为字段

解决方案四:

两个问题:
一个是增加节点的时候左右节点赋值错误,深入理解双向链表,赋值是有顺序的:修改如下;

// add node to the list rooted at root
    private void addNode(FibonacciNode node, FibonacciNode root) {
        // node.left = root;
        // node.right = root.right;
        // root.right = node;
        // node.right.left = node;

        // 修改
        node.left = root.left;
        root.left.right = node;
        node.right = root;
        root.left = node;
    }

第二个问题,不明白你的extractMin的逻辑,没有那么复杂,最小的就是根节点,取出来,并赋值后面的即可,修改如下:

// Get the minimum node of the heap and remove it from the heap
    public FibonacciNode extractMin() {
        // 修改
        FibonacciNode p = root;

        if (p == p.right)
            root = null;
        else {
            removeNode(p);
            root = p.right;
        }
        p.left = p.right = p;

        return p;

        // if (root == null) {
        // return null;
        //
        // }
        //
        // if (root.child != null) {
        // FibonacciNode m = root;
        // FibonacciNode start = root.child;
        // for (int i = 0; i < m.degree; i++) {
        // if (start != null) {
        // start.parent = null;
        // addNode(start, root);
        // start = start.right;
        // }
        // }
        // }
        // // remove root from the root list of heap
        // FibonacciNode min = root;
        // min.left.right = min.right;
        // min.right.left = min.left;
        // // if min.right==min, then the root of the heap has no child
        // if (min.right == min) {
        // this.root = null;
        // } else {
        // root = min.right;
        // consolidate();
        // }
        // // decrease the number of the nodes
        // count--;
        //
        // return min;
    }

解决方案五:

当打印4的时候已经最小了,赋值为null保存
if(min.right==min){
this.root=root.child;
}
这说明你排序的时候算法出错,可惜斐波那契堆我这个看不懂,只能给你指出错误,不能修正

解决方案六:

第二个问题你取得名字叫extractMin,我的理解是你要从里面取最小值。实际你是想removeMin。下面是removeMin方法,里面会调用consolidate进行重新合并,这个的代码修改起来还不如重新自己写或者参考一下http://www.cnblogs.com/skywang12345/p/3659122.html,建议你可以看看这个链接中内容。

解决方案七:

http://blog.csdn.net/bbirdsky/article/details/8964560

解决方案八:

java兔子问题(斐波那契数列)
数字问题之斐波那契数列全解
括号配对问题JAVA实现

时间: 2024-08-31 00:20:17

ava斐波那契堆-斐波那契堆JAVA实现的问题的相关文章

周立波域名纠纷案 周立波胜诉

新华社上海4月26日电(记者黄安琪)上海市第二中级人民http://www.aliyun.com/zixun/aggregation/4522.html">法院26日对涉及周立波域名纠纷案作出一审判决,驳回岳彤宇继续持有和使用zhoulibo.com域名的诉讼请求. 法院经审理认为,原告岳彤宇明知涉案域名的主要部分"zhoulibo"与被告周立波的姓名"周立波"近似,与被告周立波姓名的拼音"zhoulibo"相同,已足以造成相关公

猆波那契数列-斐波那契数列求和~~~~~

问题描述 斐波那契数列求和----- 如何用C语言程序求斐波那契数列的前10项和,哪位大牛帮帮忙.还有什么水仙花问题 解决方案 用c++随便写的#include#include #include using namespace std;int main(){ int sum=1; int *a = new int[10]; vectorv; a[0] = 0; a[1] = 1; for (int i = 2; i < 10; i++) { a[i] = a[i - 1] + a[i - 2];

中国拍照好手机 斐讯“小se”E651斐你莫属

随着智能手机的普及,随手拍照成为日常生活中的一种乐趣,它让我们随时随地http://www.aliyun.com/zixun/aggregation/17696.html">记录生活的点点滴滴,让我们成为生活的导演和演绎者,越来越多人用手机逐渐取代沉重的数码照相机,开始使用功能齐全的智能手机来拍照或者拍摄视频,因为这样可以随时上传到互联网与好友在朋友圈分享其中的快乐.随着智能手机拍照功能的逐渐普及,越来越多的手机用户开始对手机拍照功能提出了更高的要求,他们希望用手机可以拍出单反相机一样的画

堆糖APP怎么添加好友 堆糖好友添加方法一览

给各位堆糖软件的使用者们来详细的解析分享一下好友添加的方法. 方法分享: 1.首先打开堆糖APP,点击菜单栏上的"我"选项,进入个人中心后,点击"添加好友"选项;   2.然后你就可以通过"添加新浪微博好友"."添加微信好友"."添加QQ好友"."添加通讯录好友",或者是你可以搜索昵称来添加好友了.   好了,以上的信息就是小编给各位堆糖的这一款软件的使用者们带来的详细的好友添加的方法解

斐波那契堆(一)之 图文解析 和 C语言的实现

概要 本章介绍斐波那契堆.和以往一样,本文会先对斐波那契堆的理论知识进行简单介绍,然后给出C语言的实现.后续再分别给出C++和Java版本的实现:实现的语言虽不同,但是原理如出一辙,选择其中之一进行了解即可.若文章有错误或不足的地方,请不吝指出! 目录1. 斐波那契堆的介绍2. 斐波那契堆的基本操作3. 斐波那契堆的C实现(完整源码)4. 斐波那契堆的C测试程序 转载请注明出处:http://www.cnblogs.com/skywang12345/p/3659060.html 更多内容:数据结

输出斐波那契数列的算法

斐波那契数列(Fibonacci polynomial),又称黄金分割数列,指的是这样一个数列:0.1.1.2.3.5.8.13.21.-- 要求编程输出这样的一组数,比如输出10个数的序列 /** * @param i 第n个数 * @param j 第n+1个数 * @param n 输出个数 */ public static void ff( int i,int j,int n){ int m=1; System.out.print(i+","); while(m++<n)

《BI那点儿事》Microsoft 时序算法——验证神奇的斐波那契数列

原文:<BI那点儿事>Microsoft 时序算法--验证神奇的斐波那契数列 斐波那契数列指的是这样一个数列 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368斐波那契数列的发明者,是意大利数学家列昂纳多·斐波那契(Leonardo Fibonacci),生于公元1170年,卒于1250年,籍贯是比萨.他被人称作"比萨的列昂纳

孙洪波:我和我的庄

爱斐堡酒庄吸引了很多名士,马云.牛根生.王中军等等,都是爱斐堡的常客,爱斐堡究竟做了什么,让这些人都如此捧场. 采写|<小康·财智>记者 胡柯 孙洪波很忙,忙的连见记者一面都要推掉很多的事务.但是,孙洪波很有趣,一点没有庄主的架子,反倒更像是多年的老友. 一番寒暄之后,孙洪波开始讲起了他和爱斐堡酒庄的故事.孙洪波的一生都是在和葡萄酒打交道,来爱斐堡之前,孙洪波先是在张裕的进出口公司任经理,之后又在市场部任经理,2007年,正式成为张裕爱斐堡酒庄的庄主. 爱斐堡很美,有一座白墙蓝顶的城堡,有一个

吴晓波:柳传志曾在社科院门口卖过运动短裤

吴晓波 吴晓波档案:中国最出色的财经作家之一,哈佛大学访问学者,"蓝狮子"财经图书出版人.著有<大败局>.<穿越玉米地>.<非常营销>.<激荡三十年>.<跌荡一百年>.<大败局2>等.其中,<大败局>被评为"影响中国商业界的二十本书"之一. 电脑报记者 黄旭 话说吴晓波听闻丁磊养猪的消息后,发短信问丁磊:"这事儿真的么?"丁磊回:"兄弟,这事可真有.&q