用C建立霍夫曼树。结果总是只有一棵树的根和两个儿子

时间:2018-10-21 02:57:01

标签: pointers huffman-code

我一直在尝试使用这种算法在C语言中构建霍夫曼树。

struct node *temp1, *temp2, *new_node;
while (size != 1) {
      temp1 = extractmin(minheap, size);
      size--;
      temp2 = extractmin(minheap, size);
      size--;
      new_node = newnode((temp1->frequency) + (temp2->frequency), '$');
      new_node->lson = temp1;
      new_node->rson = temp2;
      insert(minheap, new_node, size);
      size++;
}
struct node * extractmin(struct node * heap[], int N) {
    struct node *min = heap[1];
    struct node *temp = newnode((heap[N])->frequency, (heap[N])->c);
    heap[1] = temp;
    N = N - 1;
    min_heapify(heap, 1, N);
    return min;
}
void insert(struct node * heap[], struct node * new_node, int N) {
    int i, j;
    N = N + 1;
    i = N;
    j = (i / 2);
    while ((i > 1) && ((heap[j])->frequency > (new_node)->frequency)) {
            heap[i] = heap[j];
            i = j;
            j = i / 2;
    }  
    heap[i] = new_node;
}

结果似乎总是只有其根和两个儿子的一棵树。尝试访问其任何子级的子级总是返回NULL并导致分段错误。我这样做的结果是什么?

0 个答案:

没有答案