0001 static void generateCT //通过遍历获取各字符的编码 0002 ( Bitmap* code, int length, HuffTable* table, BinNodePosi <HuffChar> v ) { 0003 if ( IsLeaf ( *v ) ) //若是叶节点(还有多种方法可以判断) 0004 { table->put ( v->data.ch, code->bits2string ( length ) ); return; } 0005 if ( HasLChild ( *v ) ) //Left = 0 0006 { code->clear ( length ); generateCT ( code, length + 1, table, v->lc ); } 0007 if ( HasRChild ( *v ) ) //Right = 1 0008 { code->set ( length ); generateCT ( code, length + 1, table, v->rc ); } 0009 } 0010 0011 HuffTable* generateTable ( HuffTree* tree ) { //将各字符编码统一存入以散列表实现的编码表中 0012 HuffTable* table = new HuffTable; Bitmap* code = new Bitmap; 0013 generateCT ( code, 0, table, tree->root() ); release ( code ); return table; 0014 }; //release()负责释放复杂结构,与算法无直接关系,具体实现详见代码包