哈夫曼算法.docx

上传人:b****6 文档编号:7916994 上传时间:2023-01-27 格式:DOCX 页数:13 大小:18.42KB
下载 相关 举报
哈夫曼算法.docx_第1页
第1页 / 共13页
哈夫曼算法.docx_第2页
第2页 / 共13页
哈夫曼算法.docx_第3页
第3页 / 共13页
哈夫曼算法.docx_第4页
第4页 / 共13页
哈夫曼算法.docx_第5页
第5页 / 共13页
点击查看更多>>
下载资源
资源描述

哈夫曼算法.docx

《哈夫曼算法.docx》由会员分享,可在线阅读,更多相关《哈夫曼算法.docx(13页珍藏版)》请在冰豆网上搜索。

哈夫曼算法.docx

哈夫曼算法

#include

#include

#include

#include

#include

#include

#include

#include

#include

#defineEXIT_OK0

#defineEXIT_FAILED-1

//////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////

FILE  *infile,*outfile;

unsignedlongint  textsize=0,codesize=0,printcount=0;

voidError(char*message)

{

        printf("\n%s\n",message);

        exit(-1);

}

/*LZSSParameters*/

#defineN              4096    /*Sizeofstringbuffer*/

#defineF              60      //*Sizeoflook-aheadbuffer60

#defineTHRESHOLD      2

#defineNIL            N      /*Endoftree'snode  */

unsignedchar

                text_buf[N+F-1];//-1

int            match_position,match_length,

                lson[N+1],rson[N+257],dad[N+1];

voidInitTree(void)  /*Initializingtree*/

{

        int  i;

        for(i=N+1;i<=N+256;i++)

                rson[i]=NIL;                  /*root*/

        for(i=0;i

                dad[i]=NIL;                  /*node*/

}

voidInsertNode(intr)  /*Insertingnodetothetree*/

{

        int  i,p,cmp;

        unsignedchar  *key;

        unsignedc;

        cmp=1;

        key=&text_buf[r];

        p=N+1+key[0];

        rson[r]=lson[r]=NIL;

        match_length=0;

        for(;;){

                if(cmp>=0){

                        if(rson[p]!

=NIL)

                                p=rson[p];

                        else{

                                rson[p]=r;

                                dad[r]=p;

                                return;

                        }

                }else{

                        if(lson[p]!

=NIL)

                                p=lson[p];

                        else{

                                lson[p]=r;

                                dad[r]=p;

                                return;

                        }

                }

                for(i=1;i

                        if((cmp=key[i]-text_buf[p+i])!

=0)

                                break;

                if(i>THRESHOLD){

                        if(i>match_length){

                                match_position=((r-p)&(N-1))-1;

                                if((match_length=i)>=F)

                                        break;

                        }

                        if(i==match_length){

                                if((c=((r-p)&(N-1))-1)

                                        match_position=c;

                                }

                        }

                }

        }

        dad[r]=dad[p];

        lson[r]=lson[p];

        rson[r]=rson[p];

        dad[lson[p]]=r;

        dad[rson[p]]=r;

        if(rson[dad[p]]==p)

                rson[dad[p]]=r;

        else

                lson[dad[p]]=r;

        dad[p]=NIL;  /*removep*/

}

voidDeleteNode(intp)  /*Deletingnodefromthetree*/

{

        int  q;

        if(dad[p]==NIL)

                return;                /*unregistered*/

        if(rson[p]==NIL)

                q=lson[p];

        else

        if(lson[p]==NIL)

                q=rson[p];

        else{

                q=lson[p];

                if(rson[q]!

=NIL){

                        do{

                                q=rson[q];

                        }while(rson[q]!

=NIL);

                        rson[dad[q]]=lson[q];

                        dad[lson[q]]=dad[q];

                        lson[q]=lson[p];

                        dad[lson[p]]=q;

                }

                rson[q]=rson[p];

                dad[rson[p]]=q;

        }

        dad[q]=dad[p];

        if(rson[dad[p]]==p)

                rson[dad[p]]=q;

        else

                lson[dad[p]]=q;

        dad[p]=NIL;

}

/*Huffmancodingparameters*/

#defineN_CHAR          (256-THRESHOLD+F)

                                /*charactercode(=0..N_CHAR-1)*/

#defineT              (N_CHAR*2-1)        /*Sizeoftable*/

#defineR              (T-1)                /*rootposition*/

#defineMAX_FREQ        0x8000

}

                                        /*updatewhencumulativefrequency*/

                                        /*reachestothisvalue*/

typedefunsignedcharuchar;

/*

*Tablesforencoding/decodingupper6bitsof

*slidingdictionarypointer

*/

/*encodertable*/

ucharp_len[64]={

        0x03,0x04,0x04,0x04,0x05,0x05,0x05,0x05,

        0x05,0x05,0x05,0x05,0x06,0x06,0x06,0x06,

        0x06,0x06,0x06,0x06,0x06,0x06,0x06,0x06,

        0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,

        0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,

        0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,

        0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,

        0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08

};

ucharp_code[64]={

        0x00,0x20,0x30,0x40,0x50,0x58,0x60,0x68,

        0x70,0x78,0x80,0x88,0x90,0x94,0x98,0x9C,

      0xA0,0xA4,0xA8,0xAC,0xB0,0xB4,0xB8,0xBC,

        0xC0,0xC2,0xC4,0xC6,0xC8,0xCA,0xCC,0xCE,

        0xD0,0xD2,0xD4,0xD6,0xD8,0xDA,0xDC,0xDE,

        0xE0,0xE2,0xE4,0xE6,0xE8,0xEA,0xEC,0xEE,

        0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,

        0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF

};

/*decodertable*/

uchard_code[256]={

        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

        0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,

        0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,

        0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,

        0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,

        0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,

        0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,

        0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,

        0x05,0x05,0x05,0x05,0x05,0x05,0x05,0x05,

        0x06,0x06,0x06,0x06,0x06,0x06,0x06,0x06,

        0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,

        0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,

        0x09,0x09,0x09,0x09,0x09,0x09,0x09,0x09,

        0x0A,0x0A,0x0A,0x0A,0x0A,0x0A,0x0A,0x0A,

        0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,0x0B,

        0x0C,0x0C,0x0C,0x0C,0x0D,0x0D,0x0D,0x0D,

        0x0E,0x0E,0x0E,0x0E,0x0F,0x0F,0x0F,0x0F,

        0x10,0x10,0x10,0x10,0x11,0x11,0x11,0x11,

        0x12,0x12,0x12,0x12,0x13,0x13,0x13,0x13,

        0x14,0x14,0x14,0x14,0x15,0x15,0x15,0x15,

        0x16,0x16,0x16,0x16,0x17,0x17,0x17,0x17,

        0x18,0x18,0x19,0x19,0x1A,0x1A,0x1B,0x1B,

        0x1C,0x1C,0x1D,0x1D,0x1E,0x1E,0x1F,0x1F,

        0x20,0x20,0x21,0x21,0x22,0x22,0x23,0x23,

        0x24,0x24,0x25,0x25,0x26,0x26,0x27,0x27,

        0x28,0x28,0x29,0x29,0x2A,0x2A,0x2B,0x2B,

        0x2C,0x2C,0x2D,0x2D,0x2E,0x2E,0x2F,0x2F,

        0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,

        0x38,0x39,0x3A,0x3B,0x3C,0x3D,0x3E,0x3F,

};

uchard_len[256]={

        0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,

        0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,

        0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,

        0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,

        0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,

        0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,

        0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,

        0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,

        0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,

        0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,

        0x05,0x05,0x05,0x05,0x05,0x05,0x05,0x05,

        0x05,0x05,0x05,0x05,0x05,0x05,0x05,0x05,

        0x05,0x05,0x05,0x05,0x05,0x05,0x05,0x05,

        0x05,0x05,0x05,0x05,0x05,0x05,0x05,0x05,

        0x05,0x05,0x05,0x05,0x05,0x05,0x05,0x05,

        0x05,0x05,0x05,0x05,0x05,0x05,0x05,0x05,

        0x05,0x05,0x05,0x05,0x05,0x05,0x05,0x05,

        0x05,0x05,0x05,0x05,0x05,0x05,0x05,0x05,

        0x06,0x06,0x06,0x06,0x06,0x06,0x06,0x06,

        0x06,0x06,0x06,0x06,0x06,0x06,0x06,0x06,

        0x06,0x06,0x06,0x06,0x06,0x06,0x06,0x06,

      0x06,0x06,0x06,0x06,0x06,0x06,0x06,0x06,

        0x06,0x06,0x06,0x06,0x06,0x06,0x06,0x06,

        0x06,0x06,0x06,0x06,0x06,0x06,0x06,0x06,

        0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,

        0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,

        0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,

        0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,

        0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,

        0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,

        0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,

        0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,

};

unsignedfreq[T+1];  /*cumulativefreqtable*/

/*

*pointingparentnodes.

*area[T..(T+N_CHAR-1)]arepointersforleaves

*/

intprnt[T+N_CHAR];

/*pointingchildrennodes(son[],son[]+1)*/

intson[T];

unsignedgetbuf=0;

uchargetlen=0;

intGetBit(void)        /*getonebit*/

{

        inti;

        while(getlen<=8){

                if((i=getc(infile))<0)i=0;

                getbuf|=i<<(8-getlen);

                getlen+=8;

        }

        i=getbuf;

       

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 初中教育 > 数学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1