前一段时间我用C ++编程,我找到了一个关于制作最大堆的教程。这是代码,它似乎产生了正确的订单堆。
#include <iostream>
const int size = 14;
static int A[size] = {1, 2, 5, 10, 2, 11, 12, 7, 23, 22, 34, 54, 12, 22};
int left(int i)
{
return (2*i)+1;
}
int right(int i)
{
return (2*i)+2;
}
//a leaf of a value less than the size of the array
//and larger than the center of the array
bool isLeaf(int i)
{
if(i >= (size/2) && i <= size)
{
return true;
}
return false;
}
void maxHeapify(int i)
{
//if it isn't a leaf it may need to be swapped
if(!isLeaf(i))
{
//if the value is less than it's right or left child
if(A[i] < A[left(i)] || A[i] < A[right(i)])
{
//if the left child is smaller
if(A[left(i)] > A[right(i)])
{
//swap the left child with the index
std::cout << "Swapping " << A[i] << " and " << A[left(i)] << std::endl;
int a = A[i];
A[i] = A[left(i)];
A[left(i)] = a;
//and run maxHeapify in it. This will push the value to
//it's lowest point and order all things below the parent
maxHeapify(left(i));
}
else
{
//else swap with the right child since it is larger
std::cout << "Swapping " << A[i] << " and " << A[right(i)] << std::endl;
int a = A[i];
A[i] = A[right(i)];
A[right(i)] = a;
//run maxheap on that value. This will push the value to it's lowest position
maxHeapify(right(i));
}
}
}
}
bool buildHeap()
{
//need to run for each node that is not a leaf
for(int i = (size/2); i >= 0; i--)
{
maxHeapify(i);
}
return true;
}
int main()
{
std::cout << "before: " << std::endl;
for(int i = 0; i < size; i++)
{
std::cout << A[i] << ", ";
}
std::cout << std::endl;
buildHeap();
std::cout << "After: " << std::endl;
for(int i = 0; i < size; i++)
{
std::cout << A[i] << ",";
}
std::cout << std::endl;
return 0;
}
以上的输出是:
的之前:
1,2,5,10,2,11,12,7,23,22,34,54,12,22,
交换12和22
交换11和54
交换2和34
交换10和23
交换5和54
交换5和12
交换2和34
交换2和22
交换1和54
交换1和22
交换1和12
后:
54,34,22,23,22,12,12,7,10,2,2,11,5,1,
目前我正在学习期中考试,我正在尝试用java重现我的代码。我一直在看这个,我似乎无法找到我错的地方。这是破碎的java: Main.java
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
int[] a = { 1, 2, 5, 10, 2, 11, 12, 7, 23, 22, 34, 54, 12, 22 };
System.out.println("Begfore");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + ", ");
}
Heap h = new Heap(a);
h.buildHeap();
h.print();
System.out.println();
System.out.println(a.length);
}
}
Heap.java
public class Heap {
int[] heap;
public Heap(int[] a) {
// heap = new int[a.length];
heap = a;
}
public void buildHeap() {
for (int i = heap.length / 2; i >= 0; i--) {
maxHeapify(i);
}
}
public int getLeft(int a) {
return (2 * a) + 1;
}
public int getRight(int a) {
return (2 * a) + 2;
}
private boolean isLeaf(int a) {
if (a >= ((heap.length) / 2) && a <= heap.length) {
return true;
} else {
return false;
}
}
public void maxHeapify(int a) {
if (!isLeaf(a)) {
if (heap[a] < heap[getLeft(a)] || heap[a] < heap[getRight(a)]) {
if (getLeft(a) <= heap.length && getRight(a) < heap.length) {
if (heap[getLeft(a)] > heap[getRight(a)]) {
int watcher = heap[a];
heap[a] = heap[getLeft(a)];
heap[getLeft(a)] = watcher;
maxHeapify(getLeft(a));
} else {
int watcher = heap[a];
heap[a] = heap[getRight(a)];
heap[getRight(a)] = watcher;
maxHeapify(getRight(a));
}
}
}
}
}
public void print() {
System.out.println("heap");
for (int i = 0; i < heap.length; i++) {
System.out.print(heap[i] + ", ");
}
}
}
哪个不能产生正确的最大有序堆 Java输出:54,34,12,23,22,12,1,7,10,2,2,11,5,22,
答案 0 :(得分:1)
此代码存在一些基本问题。让我解释一下。
首先,在Max Heapify功能中,您不需要检查是否是叶子的条件。这将我带到你的max heapify函数调用。
总是从索引 heap.length / 2 - 1到0 调用heapify函数,注意你没有使用-1。这允许始终从叶子中保存heapify,因为在二进制堆中是否为max on min,叶子被放置在索引heap.length / 2到heap.length-1。
此代码的另一个错误是使用索引的方式。有两种方法可以使用指数。
要么使用从0到heap.length -1的实际索引,在这种情况下不应该有任何条件,例如
<=heap.length or >=heap.length
但是你在堆化代码时已经使用了这样的条件。
或者使用伪索引,范围从1到heap.length,每当你在[]中使用它们时,只减去-1。这在我的java代码中完美运行。
希望这会有所帮助。如果你愿意,我可以给你java代码。