哈弗曼树建立与哈弗曼编码

#include

#define LEN sizeof(HaffmanNode)
#define MAXSIZE 100
#define MAX 9999

using namespace std;

typedef struct{
  int value;
  int lChild,rChild,parent;
}HaffmanNode,Haffman[MAXSIZE];

typedef struct{
  char name;
  int w;
  char code[MAXSIZE];
}HaffmanCode[MAXSIZE];

//全局变量
Haffman h;
HaffmanCode hc;

//筛选出值最小的两个结点,s1,s2存储其结点的索引位置
void Select(Haffman h,int index,int *s1,int *s2)
{
  *s1=0;*s2=0;
  for(int i=1;i<=index;i++)
  {
   //筛选出无双亲结点,且权值最小的两个结点
    if(h[i].parent==0)
    {
      if(h[i].value<=h[*s1].value)
      {  
         *s2=*s1;
         *s1=i;
      }
      else if(h[i].value>n;
     cout<<"输入叶子结点名称和每个叶子结点的权值\n";
     for(int i=1;i<=n;i++)
    {
      cin>>hc[i].name>>hc[i].w;
     }
     CreateHaffmanTree(h,hc,n);
     CreateHaffmanCode(h,hc,n);
     PrintHaffmanCode(h,hc,n);
     cout<<"继续吗y/Y\n";
     cin>>cmd;
  }while(cmd=='y'||cmd=='Y');
  return 0;
}
 

你可能感兴趣的:(Data,Structure,And,Algorithm)