【问题标题】:Having problems inserting "double" variables inside of a binary heap在二进制堆中插入“双”变量时遇到问题
【发布时间】:2019-06-01 11:21:14
【问题描述】:

这是我创建二进制堆的方式:

int *heapArray; // pointer to array of elements in heap
int capacity = 0; // maximum possible size of min heap
int heap_size; // Current number of elements in min heap
int d = 2;

int parent(int i)
{
    return (i/d);
}

void swap(double *x, double *y)
{
    double temp = *x;
    *x = *y;
    *y = temp;
}

double returnHeapValue(int key)
{
    return heapArray[key];
}

void initialize(int cap)
{
    heap_size = 0;
    capacity = cap;
    heapArray = (double*)malloc(cap*sizeof(double));
}

void insertJob(double x)
{
    if(capacity < 10)
    {
        capacity = 10;
        heapArray = (double*)realloc(heapArray,capacity*sizeof(double));
    }
    if(heap_size == capacity/2)
    {
        capacity += 10;
        heapArray = (double*)realloc(heapArray,capacity*sizeof(double));
    }

    heap_size = heap_size + 1;
    heapArray[heap_size] = x;
    maxHeapSwim(heap_size);
}

void maxHeapSwim(int k)
{
    while(k > 1 && heapArray[parent(k)] < heapArray[k] && k!=1)
    {
        swap(&heapArray[k],&heapArray[parent(k)]);
        k = parent(k);
    }
}

这是我在 main 方法中插入一堆双精度然后打印出来的方法:

int main()
{

  // test
  initialize(20);

  insertJob(2.0);
  insertJob(3.0);
  insertJob(1.0);
  insertJob(6.0);
  insertJob(4.0);
  insertJob(14.0);
  insertJob(7.0);
  insertJob(9.0);
  insertJob(8.0);
  insertJob(11.0);

  printf("\n");
  printf("%f", returnHeapValue(1));
  printf("\n");
  printf("%f", returnHeapValue(2));
  printf("\n");
  printf("%f", returnHeapValue(3));
  printf("\n");
  printf("%f", returnHeapValue(4));
  printf("\n");
  printf("%f", returnHeapValue(5));
  printf("\n");
  printf("%f", returnHeapValue(6));
  printf("\n");
  printf("%f", returnHeapValue(7));
  printf("\n");
  printf("%f", returnHeapValue(8));
  printf("\n");
  printf("%f", returnHeapValue(9));
  printf("\n");
  printf("%f", returnHeapValue(10));
  printf("\n");

  return 0;
}

但是,输出是这样的:

0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000

为什么不能正确打印数字?有什么我做错了吗?当我用整数值测试它时,一切正常。但是当我尝试插入“双”数据类型的值时它不起作用。

编辑:

我还尝试将 heapArray 的类型从 int 更改为 double,如下所示:

double *heapArray;

很遗憾,这导致我创建的另一个函数出现错误:

double delMaxPriorityJob() //This function deletes the largest value (in the root)
{
    double max = heapArray[1];
    swap(&heapArray[1],&heapArray[heap_size]);
    heap_size = heap_size - 1;
    maxHeapSink(1);
    heapArray[heap_size + 1] = NULL; //The error is in this line
    return max;
}

错误提示:

“从类型'void *'分配给类型'double'时不兼容的类型”

【问题讨论】:

  • int *heapArray; 可能你想要double* heapArray;
  • @IłyaBursov 检查我的编辑
  • NULL 不是double。为什么你觉得这个错误令人惊讶?
  • 所有发布的代码都无法编译。除其他问题外,它们缺少所需头文件所需的 #include 语句。请发帖minimal reproducible example
  • 您将数组视为从 1 开始的索引,但 C 数组是从 0 开始的。例如,您应该在插入函数的最后一件事上增加heap_size,否则您在活动数组之外写入一个并且永远不会设置heapArray[0]。鉴于此,您的 parent(k) 函数也很可能是错误的。对于从零开始的索引,它应该返回 (k - 1) / 2

标签: c double binary-heap


【解决方案1】:

我修好了。问题是,我正在尝试从不同的类运行 main() 函数。之前不起作用的原因是我没有在二进制堆的头文件中正确指定函数。这是头文件现在的样子:

extern void initialize(int cap);
extern void resetHeap();
extern int count_heap_elements();
extern int returnHeapCapacity();
extern double returnHeapValue(int key);
extern void insertJob(double x);
extern void insertEvent(double x);
extern double delMaxPriorityJob();
extern double delMaxPriorityEvent();
extern void maxHeapSink(int k);
extern void maxHeapSwim(int k);
extern void minHeapSwim(int k);
extern void minHeapSink(int k);

现在,当我从不同的类运行 main() 函数时,我得到了正确的输出。

【讨论】:

    猜你喜欢
    • 2019-05-28
    • 2016-05-23
    • 2022-01-18
    • 1970-01-01
    • 2017-09-01
    • 1970-01-01
    • 2013-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多