1

我试图从我的课本中的 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


}
4

2 回答 2

0

关键点是:

  • 为了保持节点j的左孩子在2j,右孩子在2j+1,父节点在j/2的性质,j的范围是:1<=j<=n .
  • 数组范围从 0 到 n-1

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

尝试使用以下代码,我进行了测试并且它有效 - 输出为 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
}
于 2014-04-09T05:48:05.397 回答
0

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

于 2014-04-08T21:52:03.137 回答