提取最小元素后的最小堆问题
Min Heap Problem After Extracting Minimum Element
我正在研究最小堆实现,对这个概念真的很陌生。
以此为参考:
https://www.geeksforgeeks.org/building-heap-from-array/
https://algorithmtutor.com/Data-Structures/Tree/Binary-Heaps/
我修改了代码,得出:
(这是我遇到问题的代码,所有其他代码与我的问题无关,至少我是这样认为的)
#define LCHILD(x) (2 * (x)) + 1
#define RCHILD(x) (2 * (x)) + 2
#define PARENT(x) ((x) - 1) / 2
typedef struct {
int key;
Event *element; // Assume NULL for this example
} Node;
void swap(Node **x, Node **y) {
Node *temp = *x;
*x = *y;
*y = temp;
}
void heapify(void *pq, int n, int i) {
Node **node = (Node**) pq;
int smallest = i; // Initialize smallest as root
int left = LCHILD(i);
int right = RCHILD(i); // right = 2*i + 2
if (left < n && (node[left])->key < (node[smallest ])->key)
smallest = left;
if (right < n && (node[right])->key < (node[smallest ])->key)
smallest = right;
if (smallest != i) {
swap(&node[i], &node[smallest ]);
heapify(node, n, smallest );
}
}
int extractKey(void *pq, int *n) {
Node **node = (Node**) pq;
int minElement = (node[0])->key;
node[0] = node[*n - 1];
*n = *n - 1;
heapify(pq, *n, 0);
return minElement;
}
void insert(void *pq, int key, void *element, int *n) {
Node **node = (Node**) pq;
node[*n]->key = key;
node[*n]->element = element;
*n = *n + 1;
int i = *n - 1;
while ( (i != 0) && (node[PARENT(i)]->key > node[i]->key) ) {
swap(&node[PARENT(i)], &node[i]);
i = PARENT(i);
}
}
int main() {
Node **pq = malloc (100 * sizeof(Node*));
int i;
for(i = 0; i < 100; i++) {
pq[i] = malloc(sizeof(Node));
pq[i]->element = malloc(sizeof(Event));
}
int n = 0; // heap size
insert(pq, 0, NULL, &n);
printHeap(pq, n);
insert(pq, 5, NULL, &n);
printHeap(pq, n);
insert(pq, 1, NULL, &n);
printHeap(pq, n);
insert(pq, 50, NULL, &n);
printHeap(pq, n);
extractKey(pq, &n);
printHeap(pq, n);
insert(pq, 10, NULL, &n);
printHeap(pq, n);
return 0;
}
输出:
Array representation of heap is:
0
Array representation of heap is:
0 5
Array representation of heap is:
0 5 1
Array representation of heap is:
0 5 1 50
Array representation of heap is:
1 5 50
Array representation of heap is:
1 5 10 10 // What happened here?
只有当我提取最小节点然后添加一个新节点时才会发生这种情况。如果我不提取节点,那么它工作得很好。我错过了什么吗?
编辑 1:这是我正在使用的打印功能。忘记在初始添加它 post
(这是从此处找到的版本的修改版本:
https://algorithmtutor.com/Data-Structures/Tree/Binary-Heaps/)
void printHeap(void *pq, int n) {
Node **node = (Node**) pq;
printf("Array representation of heap is:\n");
for (int i = 0; i < n; ++i) {
printf("%d ", node[i]->key);
}
printf("\n");
}
编辑 2:我做了更多测试。这是我得到的:
插入一些打印语句:
void insert(void *pq, int key, void *element, int *n) {
Node **node = (Node**) pq;
if(*n > 0) {
printf("node[%d] = %d\n", *n-1, node[*n-1]->key);
}
node[*n]->key = key;
printf("node[%d] = %d\n", *n, node[*n]->key);
if(*n > 0) {
printf("node[%d] = %d\n", *n-1, node[*n-1]->key);
}
node[*n]->element = element;
*n = *n + 1;
// move up until the heap property satisfies
int i = *n - 1;
while ( (i != 0) && (node[PARENT(i)]->key > node[i]->key) ) {
swap(&node[PARENT(i)], &node[i]);
i = PARENT(i);
}
}
输出:
node[0] = 0
Array representation of heap is:
0
node[0] = 0
node[1] = 5
node[0] = 0
Array representation of heap is:
0 5
node[1] = 5
node[2] = 1
node[1] = 5
Array representation of heap is:
0 5 1
node[2] = 1
node[3] = 50
node[2] = 1
Array representation of heap is:
0 5 1 50
Array representation of heap is:
1 5 50
node[2] = 50
node[3] = 10
node[2] = 10 // Huh? it should be 50
Array representation of heap is:
1 5 10 10
问题出在 extractKey
中的行 node[0] = node[*n - 1];
。那就是将两个节点指针设置为相同的值,因此您不再拥有 100 个唯一的节点指针。 (因此,它也会泄漏内存。)将行更改为 swap(&node[0], &node[*n - 1]);
应该可以解决问题。
我正在研究最小堆实现,对这个概念真的很陌生。
以此为参考:
https://www.geeksforgeeks.org/building-heap-from-array/
https://algorithmtutor.com/Data-Structures/Tree/Binary-Heaps/
我修改了代码,得出:
(这是我遇到问题的代码,所有其他代码与我的问题无关,至少我是这样认为的)
#define LCHILD(x) (2 * (x)) + 1
#define RCHILD(x) (2 * (x)) + 2
#define PARENT(x) ((x) - 1) / 2
typedef struct {
int key;
Event *element; // Assume NULL for this example
} Node;
void swap(Node **x, Node **y) {
Node *temp = *x;
*x = *y;
*y = temp;
}
void heapify(void *pq, int n, int i) {
Node **node = (Node**) pq;
int smallest = i; // Initialize smallest as root
int left = LCHILD(i);
int right = RCHILD(i); // right = 2*i + 2
if (left < n && (node[left])->key < (node[smallest ])->key)
smallest = left;
if (right < n && (node[right])->key < (node[smallest ])->key)
smallest = right;
if (smallest != i) {
swap(&node[i], &node[smallest ]);
heapify(node, n, smallest );
}
}
int extractKey(void *pq, int *n) {
Node **node = (Node**) pq;
int minElement = (node[0])->key;
node[0] = node[*n - 1];
*n = *n - 1;
heapify(pq, *n, 0);
return minElement;
}
void insert(void *pq, int key, void *element, int *n) {
Node **node = (Node**) pq;
node[*n]->key = key;
node[*n]->element = element;
*n = *n + 1;
int i = *n - 1;
while ( (i != 0) && (node[PARENT(i)]->key > node[i]->key) ) {
swap(&node[PARENT(i)], &node[i]);
i = PARENT(i);
}
}
int main() {
Node **pq = malloc (100 * sizeof(Node*));
int i;
for(i = 0; i < 100; i++) {
pq[i] = malloc(sizeof(Node));
pq[i]->element = malloc(sizeof(Event));
}
int n = 0; // heap size
insert(pq, 0, NULL, &n);
printHeap(pq, n);
insert(pq, 5, NULL, &n);
printHeap(pq, n);
insert(pq, 1, NULL, &n);
printHeap(pq, n);
insert(pq, 50, NULL, &n);
printHeap(pq, n);
extractKey(pq, &n);
printHeap(pq, n);
insert(pq, 10, NULL, &n);
printHeap(pq, n);
return 0;
}
输出:
Array representation of heap is:
0
Array representation of heap is:
0 5
Array representation of heap is:
0 5 1
Array representation of heap is:
0 5 1 50
Array representation of heap is:
1 5 50
Array representation of heap is:
1 5 10 10 // What happened here?
只有当我提取最小节点然后添加一个新节点时才会发生这种情况。如果我不提取节点,那么它工作得很好。我错过了什么吗?
编辑 1:这是我正在使用的打印功能。忘记在初始添加它 post
(这是从此处找到的版本的修改版本:
https://algorithmtutor.com/Data-Structures/Tree/Binary-Heaps/)
void printHeap(void *pq, int n) {
Node **node = (Node**) pq;
printf("Array representation of heap is:\n");
for (int i = 0; i < n; ++i) {
printf("%d ", node[i]->key);
}
printf("\n");
}
编辑 2:我做了更多测试。这是我得到的:
插入一些打印语句:
void insert(void *pq, int key, void *element, int *n) {
Node **node = (Node**) pq;
if(*n > 0) {
printf("node[%d] = %d\n", *n-1, node[*n-1]->key);
}
node[*n]->key = key;
printf("node[%d] = %d\n", *n, node[*n]->key);
if(*n > 0) {
printf("node[%d] = %d\n", *n-1, node[*n-1]->key);
}
node[*n]->element = element;
*n = *n + 1;
// move up until the heap property satisfies
int i = *n - 1;
while ( (i != 0) && (node[PARENT(i)]->key > node[i]->key) ) {
swap(&node[PARENT(i)], &node[i]);
i = PARENT(i);
}
}
输出:
node[0] = 0
Array representation of heap is:
0
node[0] = 0
node[1] = 5
node[0] = 0
Array representation of heap is:
0 5
node[1] = 5
node[2] = 1
node[1] = 5
Array representation of heap is:
0 5 1
node[2] = 1
node[3] = 50
node[2] = 1
Array representation of heap is:
0 5 1 50
Array representation of heap is:
1 5 50
node[2] = 50
node[3] = 10
node[2] = 10 // Huh? it should be 50
Array representation of heap is:
1 5 10 10
问题出在 extractKey
中的行 node[0] = node[*n - 1];
。那就是将两个节点指针设置为相同的值,因此您不再拥有 100 个唯一的节点指针。 (因此,它也会泄漏内存。)将行更改为 swap(&node[0], &node[*n - 1]);
应该可以解决问题。