0001 void huffCT(Stack<char>& theseus, HuffTable& table, BNP<HuffChar> v) { 0002 if (IsLeaf(v)) { //每遇到一个叶节点,便导出对应的编码串,并添加到编码表中 0003 Rank n = theseus.size()-1; Stack<char> sueseht; 0004 for (Rank k = 0; k < n; k++) sueseht.push(theseus.pop()); 0005 char* code = new char[n+1]; code[n] = '\0'; 0006 for (Rank k = 0; k < n; k++) theseus.push(code[k] = sueseht.pop()); 0007 table.put(v->data.ch, code); 0008 } else { //否则,继续递归地遍历 0009 if (v->lc) { theseus.push('0'); huffCT(theseus, table, v->lc); } 0010 if (v->rc) { theseus.push('1'); huffCT(theseus, table, v->rc); } 0011 } //else 0012 theseus.pop(); 0013 } //huffCT 0014 0015 HuffTable huffCodeTable(HuffTree& tree) { //由编码树,导出编码表 0016 HuffTable table; Stack<char> theseus; theseus.push('x'); 0017 huffCT(theseus, table, tree.root()); 0018 return table; 0019 } //huffCodeTable