• 大小: 505KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-10
  • 语言: C/C++
  • 标签: 算法  编码  

资源简介

本程序已在visual stdio2008上调试成功。 赫夫曼树的c++win32控制台下的源程序。代码简洁,容易理解。

资源截图

代码片段和文件信息

  #include      
  #include      
  #include      
  #include      
    
  using   namespace   std   ;   
    
  typedef   struct   
  {   
          unsigned   int   weight;     //结点权值   
          unsigned   int   parentlchildrchild;   //结点的父指针,左右孩子指针   
  }HTNode*HuffmanTree;               //动态分配数组存储哈夫曼树   
    
  typedef   char   **HuffmanCode;   //动态分配数组存储哈夫曼编码表   
    
    
  HuffmanTree   HT;     //哈夫曼树HT   
  HuffmanCode   HC;     //哈夫曼编码表HC   
  unsigned   int   *w;   //w存放叶子结点权值       
  char   *   source_code   ;//source_code   存放原始码     
    
  void   CreateHuffmanTree(HuffmanTree   &unsigned   int*int   );   //生成一棵哈夫曼树   
    
  void   HuffmanCoding(HuffmanTreeHuffmanCode   &int   );               //对哈夫曼树进行编码   
    
  void   PrintHuffmanCode(HuffmanCodeunsigned   int*int);           //显示哈夫曼编码   
    
  void   Select(HuffmanTreeintint&int&);   //在数组中寻找权值最小的两个结点   
    
  int   main()   
  {   
  int   ni;                   //n是哈夫曼树叶子结点数   
  char   choose=‘y‘;   //用于选择程序是否退出     
  //程序解说   
  printf(“本程序将演示构造哈夫曼树.\n“);   
  printf(“首先输入叶子结点数目.\n例如:2\n“);   
  printf(“然后输入原始码以及权值.\n“);   
  printf(“第1个原始码及其权值   :“);   
  printf(“a   5\n“);   
  printf(“第2个原始码及其权值   :“);   
  printf(“b   3\n“);   
  printf(“程序会构造一棵哈夫曼树并显示哈夫曼编码.\n“);   
  cout<<“权值“<<“          “<<“原始码“<<“          “<<“哈夫曼编码“<  printf(“   5            a                1\n“);   
  printf(“   3            b                0\n“);   
  putchar(‘\n‘);   
  putchar(‘\n‘);   
    
  while(choose!=‘N‘&&choose!=‘n‘)   
              {   
                printf(“请输入叶子结点数目:“);   
                scanf(“%d“&n);       //输入叶子结点数   
                  
                if(n<=1)   
                  {printf(“该数不合理!\n“);continue;}   
                w=(unsigned   int*)malloc(n*sizeof(unsigned   int));   //开辟空间存放权值   
                source_code   =   (char   *)malloc(n*sizeof(char));       //开辟空间存放原始码     
                //printf(“请输入各原始码以及权值   :\n“);     
                for(i=0;i                  {   
                          printf(“第%d个原始码及其权值   :“i+1);   
                          cin>>source_code[i];                     //输入原始码     
                          cin>>w[i];                                       //输入各叶子结点权值   
                            
                  }           
                CreateHuffmanTree(HTwn);               //生成哈夫曼树   
                HuffmanCoding(HTHCn);                     //进行哈夫曼编码   
                putchar(‘\n‘);   
                PrintHuffmanCode(HCwn);                 //显示哈夫曼编码   
                printf(“哈夫曼树构造完毕,还要继续吗?(Y/N)“);   
                scanf(“   %c“&choose);   
            }   
  }   
    
  void   CreateHuffmanTree(HuffmanTree   &HTunsigned   int   *wint   n)   
  {//w存放n个结点的权值,将构造一棵哈夫曼树HT   
  int   im;   
  int   s1s2;   
  HuffmanTree   p;   
  if(n<=1)   return;

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      44032  2010-05-09 11:15  HuffmanTree\Debug\HuffmanTree.exe

     文件     417056  2010-05-09 11:15  HuffmanTree\Debug\HuffmanTree.ilk

     文件     592896  2010-05-09 11:15  HuffmanTree\Debug\HuffmanTree.pdb

     文件       8306  2010-05-09 11:15  HuffmanTree\HuffmanTree\Debug\BuildLog.htm

     文件        663  2010-05-09 11:13  HuffmanTree\HuffmanTree\Debug\HuffmanTree.exe.embed.manifest

     文件        728  2010-05-09 11:13  HuffmanTree\HuffmanTree\Debug\HuffmanTree.exe.embed.manifest.res

     文件        621  2010-05-09 11:15  HuffmanTree\HuffmanTree\Debug\HuffmanTree.exe.intermediate.manifest

     文件      52468  2010-05-09 11:15  HuffmanTree\HuffmanTree\Debug\HuffmanTree.obj

     文件         65  2010-05-09 11:15  HuffmanTree\HuffmanTree\Debug\mt.dep

     文件     166912  2010-05-09 11:15  HuffmanTree\HuffmanTree\Debug\vc90.idb

     文件     217088  2010-05-09 11:15  HuffmanTree\HuffmanTree\Debug\vc90.pdb

     文件       7871  2010-05-09 11:47  HuffmanTree\HuffmanTree\HuffmanTree.cpp

     文件       3931  2010-05-09 11:07  HuffmanTree\HuffmanTree\HuffmanTree.vcproj

     文件       1427  2010-05-09 11:47  HuffmanTree\HuffmanTree\HuffmanTree.vcproj.宋二伟欢迎您-PC.宋二伟欢迎您!!!.user

     文件    1149952  2010-05-09 11:47  HuffmanTree\HuffmanTree.ncb

     文件        899  2010-05-09 11:05  HuffmanTree\HuffmanTree.sln

    ..A..H.      8192  2010-05-09 11:47  HuffmanTree\HuffmanTree.suo

     目录          0  2010-05-09 11:15  HuffmanTree\HuffmanTree\Debug

     目录          0  2010-05-09 11:13  HuffmanTree\Debug

     目录          0  2010-05-09 11:47  HuffmanTree\HuffmanTree

     目录          0  2010-05-09 11:13  HuffmanTree

----------- ---------  ---------- -----  ----

              2673107                    21


评论

共有 条评论