我使用C创建了一个二进制堆。这是初始化堆并插入值的函数
int *heapArray; // pointer to array of elements in heap
int capacity = 0; // maximum possible size of min heap, initially set to 0
int heap_size; // Current number of elements in min heap
int d = 2; //max number of children per node
int getParent(int i) //get the parent index of i
{
return (i/d);
}
bool isFull() //check if heap is full
{
if(heap_size == capacity)
return true;
if(heap_size < capacity)
return false;
}
void initializeHeap(int cap) //initialize the heap
{
heap_size = 0;
capacity = cap;
heapArray = (int*)malloc(cap*sizeof(int));
}
void insertValue(int x) //insert a value into the heap
{
if(isFull()) //If heap is already full, double the heap size
{
capacity = 2*capacity;
heapArray = realloc(heapArray,capacity*sizeof(int));
}
heap_size = heap_size + 1;
heapArray[heap_size] = x; //insert new value into heap (not zero-indexed)
maxHeapSwim(heap_size);
}
void maxHeapSwim(int k) //move value up the heap to maintain max-heap order
{
while(k > 1 && heapArray[getParent(k)] < heapArray[k])
{
swap(&heapArray[k],&heapArray[parent(k)]);
k = parent(k);
}
}
然后在main()方法中,我尝试将值插入堆并打印出值:
int main()
{
initializeHeap(1); //Set the heap capacity to 1
insertValue(2);
insertValue(3);
insertValue(1);
insertValue(6);
insertValue(4);
printf("\n");
printf("%i", heapArray[1]);
printf("\n");
printf("%i", heapArray[2]);
printf("\n");
printf("%i", heapArray[3]);
printf("\n");
printf("%i", heapArray[4]);
printf("\n");
printf("%i", heapArray[5]);
printf("\n");
return 0;
}
由于这是最大堆,我希望输出看起来像这样:
6
4
1
2
3
但是,它看起来像这样:
6
4
1
0
3
我不明白。为什么2变成0?我感觉这与我如何在 insertValue()函数中将堆大小加倍有关。...也许是我使用realloc的方式。我做错了什么吗?
要注意的另一件事是我的二进制堆不是零索引的。第一个值插入heapArray [1],第二个值插入heapArray [2],依此类推。
答案 0 :(得分:0)
代码第一部分的某些更改对我有用。
实施subscribe(onNext:)
和一些小的更改。
代码
swap()
输出
#define false 0
#define true 1
int *heapArray; // pointer to array of elements in heap
int capacity = 0; // maximum possible size of min heap, initially set to 0
int heap_size; // Current number of elements in min heap
int d = 2; //max number of children per node
int getParent(int i) //get the parent index of i
{
return (i/d);
}
void maxHeapSwim(int k) //move value up the heap to maintain max-heap order
{
while(k > 1 && heapArray[getParent(k)] < heapArray[k])
{
//swap(&heapArray[k],&heapArray[parent(k)]);
int x=heapArray[k];
heapArray[k]=heapArray[getParent(k)];
heapArray[getParent(k)]=x;
k = getParent(k);
}
}
int isFull() //check if heap is full
{
if(heap_size == capacity)
return true;
if(heap_size < capacity)
return false;
}
void initializeHeap(int cap) //initialize the heap
{
heap_size = 0;
capacity = cap;
heapArray = (int*)malloc(cap*sizeof(int));
}
void insertValue(int x) //insert a value into the heap
{
if(isFull()) //If heap is already full, double the heap size
{
capacity = 2*capacity;
heapArray = realloc(heapArray,capacity*sizeof(int));
}
heap_size = heap_size + 1;
heapArray[heap_size] = x; //insert new value into heap (not zero-indexed)
maxHeapSwim(heap_size);
}
int main()
{
initializeHeap(1); //Set the heap capacity to 1
insertValue(2);
insertValue(3);
insertValue(1);
insertValue(6);
insertValue(4);
printf("\n");
printf("%i", heapArray[1]);
printf("\n");
printf("%i", heapArray[2]);
printf("\n");
printf("%i", heapArray[3]);
printf("\n");
printf("%i", heapArray[4]);
printf("\n");
printf("%i", heapArray[5]);
printf("\n");
return 0;
}
答案 1 :(得分:0)
这对我有用:
void insertValue(int x)
{
if(capacity < 10)
{
capacity = 10;
heapArray = (int*)realloc(heapArray,capacity*sizeof(int));
}
if(heap_size >= capacity/2)
{
capacity += 10;
heapArray = (int*)realloc(heapArray,capacity*sizeof(int));
}
heap_size = heap_size + 1;
heapArray[heap_size] = x;
maxHeapSwim(heap_size);
}
关键是确保二进制堆永远不会满。