C 實現Huffman編碼和解碼

2021-04-19 19:27:34 字數 1510 閱讀 4694

using system;

using system.collections;

using system.collections.generic;

using system.linq;

using system.text;

namespace stringcompresser

public huffman(char chararray, int weight);}

for (int i = weight.length + 1; i < huffmantree.length; i++)

if (min2 == null)

else

continue;

}if(min1 != null && min2 != null)

else

if (huffmantree[j].weight < min2)}}

} #endregion

huffmantree[min1pos].parent = huffmantree[min2pos].parent = i;

huffmantree[i].lchild = min1pos;

huffmantree[i].rchild = min2pos;

huffmantree[i].weight = min1.value + min2.value;                

}//get huffman code

int p = 0,c =0;

list values = null;

for (int i = 1; i <= weight.length; i++)

else

//1

}values.reverse();

huffmancode.add(chararray[i - 1], new bitarray(values.toarray()));

}            

}//encode a string to bitarray

public bitarray encode(string input)

}return

new bitarray(list.toarray());

}//decode a bitarray to a string

public

string decode(bitarray bitarray)

rtnstring = string.concat(rtnstring, current.character);

current = huffmantree[ic];

}return rtnstring;

}//get char from a char bitarray

private

char getcharacter(bitarray array)

else

c = huffmantree[ic];

}return c.character;}}

}

huffman編碼和解碼實現

寫資料結構的實驗確實是蠻麻煩的,下面提供乙個還可以執行的源程式給大家參考,應付實驗老師是綽綽有餘的了 link.h檔案內容 pragma once class link link的實現檔案什麼內容也沒有,所以就不寫啦 huffman.h內容 pragma once include link.h cl...

Huffman編碼與解碼的實現

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

Huffman編碼與解碼

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