Example #1
0
int main(){
    HuffTree h;
    char c[7] = {'e', 'f', 'd', 'a', 'g'};
    int f[7] = {12, 4, 2, 3, 1};
    h.buildTree(c,f,5);

    cout << h.getCode('e') << endl;
    cout << h.getCode('f') << endl;
    cout << h.getCode('a') << endl;
    cout << h.getCode('g') << endl;
    cout << h.getCode('d') << endl;
}
Example #2
0
void decompress(QString name, QString local){
    QFile file(name);
    Q_ASSERT_X(file.open(QIODevice::ReadOnly), Q_FUNC_INFO, "file not found.");
    QByteArray head =file.read(3);
    int Ttrash = (((uchar(head[0]))&0xe0)>>5);
    int Tsize =  (((qint16(head[0]))&0x1f)<<8)| ((quint16(head[1]))&0xff);
    int Tname = head.at(2);
    //qDebug() <<Ttrash<<Tsize<<Tname ;

    QString OriginalName= file.read(Tname);
    if(!local.isEmpty()){
       OriginalName = changesLocal(OriginalName,local);
    }
    qDebug()<<"Descomprimindo!";
    QByteArray TreeRep= file.read(Tsize);
    HuffTree* tree = new HuffTree();
    tree->Rebuild(TreeRep);
   
    QByteArray data;
    //Lendo o arquivo e passando para bits.
    while (!file.atEnd()) {
         QByteArray line = file.read(1024);
         data.append(line);
    }
    file.close();
    QFile Original (OriginalName);
    Q_ASSERT_X(Original.open(QIODevice::WriteOnly), Q_FUNC_INFO, "file not found.");


    //Bits-Bytes
    QBitArray bit= ToBitArray(data);
    bit.resize(bit.size()-Ttrash);

    int bitsize=bit.size();
    QByteArray RetrieveData;
    //Lendo a arvore e copiando o nó para o arquivo

    for(int i=0;i<bitsize;++i){
        if(tree->transverse(bit.at(i))){
            RetrieveData.append( tree->current()->m_content);
            tree->setCurrent();
        }
    }
    Original.write(RetrieveData);
    Original.close();
    qDebug()<< "Arquivo Descomprimido! :)";
}
HuffTree* generateTree ( HuffForest* forest ) { //Huffman编码算法
   while ( 1 < forest->size() ) {
      HuffTree* T1 = minHChar ( forest ); HuffTree* T2 = minHChar ( forest );
      HuffTree* S = new HuffTree(); /*DSA*/printf ( "\n################\nMerging " ); print ( T1->root()->data ); printf ( " with " ); print ( T2->root()->data ); printf ( " ...\n" );
      S->insertAsRoot ( HuffChar ( '^', T1->root()->data.weight + T2->root()->data.weight ) );
      S->attachAsLC ( S->root(), T1 ); S->attachAsRC ( S->root(), T2 );
      forest->insertAsLast ( S ); /*DSA*/ //print(forest);
   } //assert: 循环结束时,森林中唯一(列表首节点中)的那棵树即Huffman编码树
   return forest->first()->data;
}
/******************************************************************************************
 * Huffman树构造算法:对传入的Huffman森林forest逐步合并,直到成为一棵树
 ******************************************************************************************
 * forest基于优先级队列实现,此算法适用于符合PQ接口的任何实现方式
 * 为Huffman_PQ_List、Huffman_PQ_ComplHeap和Huffman_PQ_LeftHeap共用
 * 编译前对应工程只需设置相应标志:DSA_PQ_List、DSA_PQ_ComplHeap或DSA_PQ_LeftHeap
 ******************************************************************************************/
HuffTree* generateTree(HuffForest* forest) {
   while (1 < forest->size()) {
      HuffTree* s1 = forest->delMax(); HuffTree* s2 = forest->delMax();
      HuffTree* s = new HuffTree();
      /*DSA*/printf("Merging "); print(s1->root()->data); printf(" with "); print(s2->root()->data); printf(" ...\n");
      s->insertAsRoot(HuffChar('^', s1->root()->data.weight + s2->root()->data.weight));
      s->attachAsLC(s->root(), s1); s->attachAsRC(s->root(), s2);
      forest->insert(s); //将合并后的Huffman树插回Huffman森林
   }
   HuffTree* tree = forest->delMax(); //至此,森林中的最后一棵树
   return tree; //即全局Huffman编码树
}