實(shí)現(xiàn)最優(yōu)二叉樹的構(gòu)造;在此基礎(chǔ)上完成哈夫曼編碼器與譯碼器。 假設(shè)報(bào)文中只會(huì)出現(xiàn)如下表所示的字符:
字符 A B C D E F G H I J K L M N
頻度 186 64 13 22 32 103 21 15 47 57 1 5 32 20 57
字符 O P Q R S T U V W X Y Z , .
頻度 63 15 1 48 51 80 23 8 18 1 16 1 6 2
要求完成的系統(tǒng)應(yīng)具備如下的功能:
1.初始化。從終端(文件)讀入字符集的數(shù)據(jù)信息,。建立哈夫曼樹。
2.編碼:利用已建好的哈夫曼樹對明文文件進(jìn)行編碼,并存入目標(biāo)文件(哈夫曼碼文件)。
3.譯碼:利用已建好的哈夫曼樹對目標(biāo)文件(哈夫曼碼文件)進(jìn)行編碼,并存入指定的明文文件。
4.輸出哈夫曼編碼文件:輸出每一個(gè)字符的哈夫曼編碼。
function [U,center,result,w,obj_fcn]= fenlei(data)
[data_n,in_n] = size(data)
m= 2 % Exponent for U
max_iter = 100 % Max. iteration
min_impro =1e-5 % Min. improvement
c=3
[center, U, obj_fcn] = fcm(data, c)
for i=1:max_iter
if F(U)>0.98
break
else
w_new=eye(in_n,in_n)
center1=sum(center)/c
a=center1(1)./center1
deta=center-center1(ones(c,1),:)
w=sqrt(sum(deta.^2)).*a
for j=1:in_n
w_new(j,j)=w(j)
end
data1=data*w_new
[center, U, obj_fcn] = fcm(data1, c)
center=center./w(ones(c,1),:)
obj_fcn=obj_fcn/sum(w.^2)
end
end
display(i)
result=zeros(1,data_n) U_=max(U)
for i=1:data_n
for j=1:c
if U(j,i)==U_(i)
result(i)=j continue
end
end
end