首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >提取最小元素后的最小堆问题

提取最小元素后的最小堆问题
EN

Stack Overflow用户
提问于 2021-02-25 03:58:29
回答 1查看 165关注 0票数 4

我正在做一个最小堆的实现,并且对这个概念还是个新手。

以此作为参考:

https://www.geeksforgeeks.org/building-heap-from-array/

https://algorithmtutor.com/Data-Structures/Tree/Binary-Heaps/

我修改了代码,并提出:

(这是我遇到问题的代码,所有其他代码都与我的问题无关,至少我是这样认为的)

代码语言:javascript
复制
#define LCHILD(x) (2 * (x)) + 1
#define RCHILD(x) (2 * (x)) + 2
#define PARENT(x) ((x) - 1) / 2
代码语言:javascript
复制
typedef struct {
    int key;
    Event *element; // Assume NULL for this example
} Node;
代码语言:javascript
复制
void swap(Node **x, Node **y) {
    Node *temp = *x;
    *x = *y;
    *y = temp;
}
代码语言:javascript
复制
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 ); 
    } 
}
代码语言:javascript
复制
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;
}
代码语言:javascript
复制
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);
    }
}
代码语言:javascript
复制
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;
}

输出:

代码语言:javascript
复制
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

:这是我正在使用的打印函数。我忘了在最初的帖子中添加它

(它是在这里找到的版本的修改版本:

https://algorithmtutor.com/Data-Structures/Tree/Binary-Heaps/))

代码语言:javascript
复制
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:我又做了一些测试。这是我得到的:

插入了一些print语句:

代码语言:javascript
复制
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);
    }
}

输出:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-25 05:28:16

问题是行 node[0] = node[*n - 1];在提取密钥中。那就是将您的两个节点指针设置为相同的值,因此您不再有 100 个唯一的节点指针。(因此,它也会泄漏内存。)将行更改为 swap(&node[0], &node[*n - 1]);应该可以解决问题。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66358034

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档