gen_code.asv
上传用户:xxhxxny
上传日期:2021-02-19
资源大小:87k
文件大小:1k
源码类别:

压缩解压

开发平台:

Matlab

  1. %==============================================
  2. % 根据Huffman树,生成不同字符的huffman编码,
  3. % 保存在hufcode中,递归调用
  4. % 参数:(构建霍夫曼树,根节点,临时霍夫曼编码,临时霍夫曼编码的二进制位数,最终生成的霍夫曼编码)
  5. %==============================================
  6. function hufcode = gen_code(huffman_tree,index_tree,tmpcode,tmpbits,hufcode)
  7. if index_tree <= 256   %递归调用的退出条件
  8.     hufcode.code(index_tree)=tmpcode;
  9.     hufcode.bits(index_tree)=tmpbits;
  10.     return;
  11. end
  12. tmpcode=tmpcode*2;   %
  13. tmpbits=tmpbits+1;
  14. hufcode=gen_code(huffman_tree,huffman_tree.leftchild(index_tree),tmpcode,tmpbits,hufcode);
  15. hufcode=gen_code(huffman_tree,huffman_tree.rightchild(index_tree),bitor(tmpcode,1),tmpbits,hufcode);