【问题标题】:building a bottomUp heap建立一个bottomUp堆
【发布时间】:2014-05-21 20:11:34
【问题描述】:

我试图从我的教科书中的 Psuedo 代码中进行堆自下而上的构造,但是我得到的输出不是正确的堆,我正在输出 2 9 8 6 5 7

任何人都知道我哪里出错了(伪代码来自教科书,堆需要是数组)

这是我正在使用的自下而上的 PsuedoCode

//constructs a heap from elements of a given array by bottom up algorithm
//input an array H[1...n] of orderable items
//output a heap H[1...n]

for i<- [n/2] downto 1 do
 k<-i; v<-H[k];
 heap<-False
  while not heap and 2*k <= n do
   j<-2*k
   if(j<n) //there are two children
    if H[j] < H[j+1] j<- j+1
   if v>=h[j]
    heap = true
   else H[k]<-H[j]  k<-j
 H[k] <-V

这是我的代码

package heapbottomup;

import javax.swing.Spring;

public class heapbottomup {
public static void main(String args[]){
    int[] array = {2 ,9 ,7 ,6 ,5 ,8};
    BottomUp(array);
}

static int[] BottomUp (int[]array){
    int n = array.length-1;

    for(int i=(n/2);i>=1;i--){
        int k =i;
        int v = array[k];
        boolean Heap = false;

        while(!Heap && ((2*k)<=n)){
            int j = 2*k;
            if (j<n){
                if(array[j]<array[j+1]) j =j+1;
            }
            if(v>=array[j]){
                Heap=true;
            }
            else{
                array[k]= array[j];
                k=j;
            }
            array[k]=v;
        }//end while

    }//end for
    print(array);
    return(array);
}


static void print(int[]array){
    if(array==null){
        System.out.println("empty");
        return;
    }
    for(int i =0;i<array.length;i++){
        System.out.print(array[i] + " ");
    }
    System.out.println();
}//end print


}

【问题讨论】:

    标签: java arrays heap bottom-up


    【解决方案1】:

    Java 中的数组从索引 0 开始并转到 n-1(不是伪代码中的 1 到 n)。您正确地将 n 初始化为 array.length-1,但是您还应该在 i &gt;= 0 而不是 i &gt;= 1 时进行 for 循环。我没有运行您的程序来查看是否还有其他问题,但这似乎是首先要解决的问题。

    【讨论】:

    • 它更好,但是当正确的输出为 9 6 8 2 5 7 时,我得到一个排序堆,我得到 9 8 7 6 5 2
    • 从您的伪代码的缩进来看,最后一行H[k] &lt;-V 似乎在while 之外,但在您的代码中它在while 之内。
    • 请注意,如果使用 1 索引数组更容易理解或更快,您可以始终使数组比必要的大一点,并且不要使用第 0 个条目。
    【解决方案2】:

    重点是:

    • 为了保持节点在其左侧位置 j 的属性 孩子在 2j,它的右孩子在 2j +1,它的父母在 j/2, j的范围是:1
    • 数组范围从0到n-1

    诀窍是:在for和while循环中,你仍然会想到1到n的范围,但是当你做数组操作时,你只需要偏移1,即array[j-1]为位置j.

    试试下面的代码,我测试过,它成功了 - 输出是 9 6 8 2 5 7。请注意 array[k-1]=v 不在 while 循环中。

    public class heapbottomup {
        public static void main(String args[]){
            int[] array = {2 ,9 ,7 ,6 ,5 ,8};
            BottomUp(array);
        }
    
        static int[] BottomUp (int[]array){
            int n = array.length;
    
            for(int i=(n/2);i>=1;i--){
                int k =i;
                int v = array[k-1];
                boolean Heap = false;
    
                while(!Heap && ((2*k)<=n)){ 
                    int j = 2*k;
                    if (j<n){
                        if(array[j-1]<array[j]) j =j+1;
                    }
                    if(v>=array[j-1]){
                        Heap=true;
                    }
                    else{
                        array[k-1]= array[j-1];  
                        k=j; 
                    }                
                }//end while
    
                array[k-1]=v; 
    
            }//end for
            print(array);
            return(array);
        }
    
        static void print(int[]array){
            if(array==null){
                System.out.println("empty");
                return;
            }
            for(int i =0;i<array.length;i++){
                System.out.print(array[i] + " ");
            }
            System.out.println();
        }//end print
    }
    

    【讨论】:

      猜你喜欢
      • 2012-11-03
      • 2019-05-12
      • 2012-12-24
      • 2018-06-10
      • 1970-01-01
      • 2015-10-17
      • 2015-02-13
      • 2015-10-05
      • 1970-01-01
      相关资源
      最近更新 更多