下面是源**:
#include
#include
#include
using namespace std;
typedef struct hchar
hchar,*wcharptr;
typedef struct htnode
htnode;
typedef struct htcode
htcode;
class hftree
;bool hftree::inputdata(istream& input)
return true;
}bool hftree::huffmancoding()
int n=_ht.size();
int m=2*n-1;
_ht.resize(m);
int min1=0,min2=0;
for (int i=n;iint hftree::select(int n)
int min=-1;
for (int i=0;i
bool hftree::putoutcode()
vector::iterator hcit=_hc.begin(),hcitend=_hc.end();
while (hcit!=hcitend)
unsigned int sum=0; //被編碼的字元數 unsigned int string::size()
string codes; //**
string::iterator cit=chars.begin(),citend=chars.end();
vector::iterator hcit=_hc.begin(),hcitend=_hc.end();
for (;cit!=citend;cit++)}}
if (sum
string hftree::decoding(std::string &codes)
int i;
string chars;
string::iterator cit=codes.begin(),citend=codes.end();
// vector::iterator htit=_ht.begin(),htitend=_ht.end();
while (cit!=citend)
else if (*cit=='1')
}chars.push_back(_ht[i]._data._char);
}return chars;
}int main()
{cout<<"請輸入資料初始化:"<>chars;
codes=temp.coding(chars);
cout<>codes;
chars=temp.decoding(codes);
cout/初步測試功能正常,未發現異常錯誤,待進一步測試~!
資料結構 赫夫曼樹
赫夫曼樹 huffman tree 又稱為最優樹,是一類帶權路徑長度最短的樹。本文僅討論最優二叉樹。樹的路徑長度是指從樹根到樹中其餘各個結點的路徑長度之和。對具有n個結點的二叉樹而言,完全二叉樹具有最短的樹的路徑長度。若在二叉樹中,樹葉結點帶有權值,則有 結點的帶權路徑長度定義為從樹根到該結點之間的...
資料結構 赫夫曼樹
赫夫曼編碼是首個試用的編碼方案。屬於無失真壓縮的編碼方案。在資料通訊中,赫夫曼編碼可以根據字元出現頻率,構造出一種不等長的二進位制,使編碼後的電文長度最短,且不產生二義性。weighteed path length是樹中所有節點的帶權路徑長度之和。wpl 值越小,說明構造出來的二叉樹效能越優。赫夫曼...
資料結構(八) 赫夫曼樹
一 基本介紹 1 給定n個權值作為n個葉子節點,構造一顆二叉樹,如果這個樹的帶權路徑長度達到最小,就叫做 最優二叉樹 也叫 赫夫曼樹 帶權路徑最短 最優二叉樹 赫夫曼樹 2 赫夫曼樹是帶權路徑長度最短的樹,權值較大的節點離根很近 二 重要概念 三 赫夫曼樹建立思路 乙個數列,要求轉成一顆赫夫曼樹 1...