2301_79683887 2023-10-19 17:26 采纳率: 70.6%
浏览 4
已结题

堆创建哈夫曼树,创建不了,代码怎么改


#include <stdio.h>
#include <stdlib.h>
#include<stdbool.h>
#define Max 100
//哈夫曼树 节点
typedef struct HuffmanNode{
    int weigth;//权值
     struct HuffmanNode* lchild;//左子树
   struct HuffmanNode* rchild;//右子树
}Huff,*pHuff;

typedef struct MinHeap{
      pHuff Node;
     int size;
     int max;
}MinHeap,*pMinHeap;
//初始化最小堆
pMinHeap init(int* arr,int len){
   pMinHeap H=(pMinHeap)malloc(sizeof(MinHeap));
   H->Node=(pHuff)malloc(sizeof(Huff)*len);
   H->size=len;
   H->max=Max;
   for(int i=1;i<=len;i++){
    H->Node[i].weigth=arr[i];
    H->Node[i].lchild=NULL;
    H->Node[i].rchild=NULL;
   }
   return H;
}
//创建最小堆
void creat(pMinHeap H){
   for(int i=H->size/2;i>=1;i--){
    int parent=i;
    int child=i*2;
    while(child<H->size){
        if(child!=H->size&&H->Node[child+1].weigth<H->Node[child].weigth)
            child++;
        if(H->Node[child].weigth<H->Node[parent].weigth){
            Huff hu=H->Node[child];
            H->Node[child]=H->Node[parent];
            H->Node[parent]=hu;
            parent=child;
            child=parent*2;
        }
        else{
            break;
        }
    }
   }

}
bool IsFull(pMinHeap H){
    if(H->size==Max){
        return true;
    }

    return false;
}
bool IsEmpty(pMinHeap H){
if(H->size==0){
    return true;
}
 return false;
   }
//插入
void Insert(pMinHeap H,pHuff Hu){
   if(IsFull(H)){
    printf("堆满");
    return ;
   }
   int i=H->size+1;
   H->size=H->size+1;
   for( ;H->Node[i/2].weigth>Hu->weigth;i=i/2){
    H->Node[i]=H->Node[i/2];
   }
   H->Node[i]=(*Hu);
}
//删除
Huff Delete(pMinHeap H){
   int parent,child;
   Huff Min=H->Node[1];
   Huff hu=H->Node[H->size];
   H->size--;
   parent=1;
   child=parent*2;
   while(parent*2<=H->size){
    if(child!=H->size&&H->Node[child+1].weigth<H->Node[child].weigth)
            child++;
        if(H->Node[child].weigth<hu.weigth){
            H->Node[parent]= H->Node[child];
            parent=child;
            child=parent*2;
        }
        else{
            break;
        }
   }
   H->Node[parent]=hu;
    return Min;
}
//先序遍历
void tra(pHuff T){
   if(T!=NULL){
    printf("%d ",T->weigth);
    if(T->lchild!=NULL)
    tra(T->lchild);
    if(T->rchild!=NULL)
    tra(T->rchild);
   }

}
void tra1(pMinHeap H){
 for(int i=1;i<=H->size;i++){
    printf("%d ",H->Node[i].weigth);
 }

}

int main()
{   int arr[]={0,10,7,5,2,4};
     int len=sizeof(arr)/sizeof(int)-1;
     printf("111111111111111\n");
     pMinHeap H=init(arr,len);
     printf("2222222222222222\n");
     creat(H);
     //printf("33333333333333333333\n");
     tra1(H);
     //printf("4444444444444444\n");
     pHuff T;
     Huff HU;
     //printf("55555555555555555\n");
     //创建树
     for(int i=1;i<H->size;i++){
            printf("%d\n",i);
     T=(pHuff)malloc(sizeof(Huff));
     HU=Delete(H);
     T->lchild=&HU;
     HU=Delete(H);
     T->lchild=&HU;
     T->weigth=T->lchild->weigth+T->rchild->weigth;
     Insert(H,T);
     }
     HU=Delete(H);
     T=&HU;
     printf("66666666666666666\n");
     tra(T);
    return 0;
}


```c
 //创建树
     for(int i=1;i<H->size;i++){
            printf("%d\n",i);
     T=(pHuff)malloc(sizeof(Huff));
     HU=Delete(H);
     T->lchild=&HU;
     HU=Delete(H);
     T->lchild=&HU;
     T->weigth=T->lchild->weigth+T->rchild->weigth;
     Insert(H,T);
     }
     HU=Delete(H);
     T=&HU;
     printf("66666666666666666\n");
     tra(T);

```

  • 写回答

2条回答 默认 最新

  • Leodong. 2023-10-19 17:29
    关注

    该回答通过自己思路及引用到GPTᴼᴾᴱᴺᴬᴵ搜索,得到内容具体如下:
    这段代码中,创建哈夫曼树的部分存在一些问题。在创建哈夫曼树的过程中,我们需要将权重最小的两个节点合并为一个新的节点,然后将新节点插入到最小堆中。但是在这段代码中,我们只是简单地将新节点的左右子节点设置为已经删除的两个节点,而没有更新这两个节点的权重。因此,我们需要修改这部分代码,以便正确地创建哈夫曼树。
    以下是修改后的代码:

    // 创建树
    for (int i = 1; i < H->size; i++) {
        printf("%d
    ", i);
        T = (pHuff)malloc(sizeof(Huff));
        HU = Delete(H);
        T->lchild = &HU;
        HU = Delete(H);
        T->rchild = &HU;
        T->weigth = T->lchild->weigth + T->rchild->weigth;
        Insert(H, T);
    }
    

    在这段代码中,我们首先从最小堆中删除两个权重最小的节点,并将它们的左右子节点分别设置为新节点的左子节点和右子节点。然后,我们计算新节点的权重,即其左右子节点的权重之和。最后,我们将新节点插入到最小堆中。这样,我们就可以正确地创建哈夫曼树了。


    如果以上回答对您有所帮助,点击一下采纳该答案~谢谢

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 11月8日
  • 已采纳回答 10月31日
  • 创建了问题 10月19日