Huffman - coding with matlab

%%近學習圖像處理,書中的huufman編碼代碼實在難以恭維,網上又未尋得matlab版以供參考,遂自己動手。。。

%%
% Function   :huffman 編碼
% Author     :joooogoofooo(09071308)
% Date       : 2012-05-10
%%

%%
function  huffmanCoding(~)

 p = [0.4, 0.18, 0.10, 0.10 , 0.07, 0.06, 0.05, 0.04]; 

%%構建huffman樹:
n   = length(p);
idx = zeros(1,2*n-1);
%初始化葉:
idx (1:n) = 1;
pnum = 0;
for i=1:n
    pnum            = pnum+1;
    HT(pnum).value  = p(i);%節點值
    HT(pnum).left   = 0;   %左子樹索引
     HT(pnum).right  = 0;  %右子樹索引
    HT(pnum).parent = 0;   %父節點索引
end
%建造huffman樹非葉節點:
for i=1:n-1
    [l,r]           = least2(HT,idx);%找到最小兩個節點
    pnum            = pnum+1;
    HT(pnum).value  = HT(l).value + HT(r).value;%合併節點
    HT(pnum).left   = l;
    HT(pnum).right  = r;
    HT(pnum).parent = 0; %父節點默認爲空(0)
    HT(l).parent    = pnum;
    HT(r).parent    = pnum;
    idx(pnum)       = 1;
    idx(l)          = 0;
    idx(r)          = 0;
end
% 從huffman樹讀出各個葉子的編碼:
for i=1:n 
    child = i;
    parent = HT(child).p
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章