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编码树 }