huffman編碼和解碼實現

2021-05-25 14:21:59 字數 2105 閱讀 4117

寫資料結構的實驗確實是蠻麻煩的,下面提供乙個還可以執行的源程式給大家參考,應付實驗老師是綽綽有餘的了

link.h檔案內容:

#pragma once

class link

;//link的實現檔案什麼內容也沒有,所以就不寫啦

huffman.h內容:

#pragma once

#include "link.h"

class huffman:public link;

huffman.cpp檔案

#include "stdafx.h"

#include "huffman.h"

#include

using namespace std;

//建構函式,預定義編碼

huffman::huffman(void)

head=null;

built();

}//重新定義各個字母的頻率

void huffman::redef()

while(_char[i-1]!='~');

for(int j=0;j

built();

//建立huffman樹

void huffman::built()

for(int i=0;i<26;i++)}}

//對權值進行排序

_link[0].data=_char[0];

_link[0].lchild=null;

_link[0].rchild=null;

_link[0].parent=&_link[2];

_link[0].weight=_weight1[0];

_link[1].data=_char[1];

_link[1].lchild=null;

_link[1].rchild=null;

_link[1].parent=&_link[2];

_link[1].weight=_weight1[1];

_link[2].data=null;

_link[2].lchild=&_link[0];

_link[2].rchild=&_link[1];

_link[2].parent=&_link[4];

_link[2].weight=_weight1[0]+_weight1[1];

int count=2;

int i;

for(i=3;i<51;i++)

else

else

_link[i].parent=&_link[i+1];}}

head=&_link[50];

}void huffman::input()

cout<

break;

}if(p->rchild->data==(int)ch)

cout<

break;

}if(p->lchild->data==null)

if(p->rchild->data==null)

i++;}}

}//輸出輸入字串的編碼

void huffman::str()

break;

}if(p->rchild->data==(int)ch)

cout<

break;

}if(p->lchild->data==null)

if(p->rchild->data==null)

i++;

}count++;

}while(st[count]!=null);

cout<

cout<

cout<

cout<}

//對編碼進行解碼

void huffman::decode()

}else

}count++;

}else

}cout<

cout<

cout<}

主檔案:

#include "stdafx.h"

#include "huffman.h"

#include

using namespace std;

int _tmain(int argc, _tchar* argv)}}

C 實現Huffman編碼和解碼

using system using system.collections using system.collections.generic using system.linq using system.text namespace stringcompresser public huffman c...

Huffman編碼與解碼的實現

huffman編碼相信學過資料結構這麼課的都知道,概念也比較好理解,但是一般好理解的演算法,在實際實現的過程中總是會遇到各種問題,一方面個人認為是對演算法的實現過程不熟,另一方面在實際實現的過程中可以提公升自己實現演算法的能力,將自己的想法實現後還是比較滿足的。下面是本人親自實現的huffman編碼...

Huffman編碼與解碼

近期學習資料結構碰到huffman編碼與解碼問題,自己動手寫了一些,注釋比較全,ok,下面直接貼 include include define telemtype char define wtype int define leafnumber 5 預設權重集合大小 define totalnumbe...