Huffman Compression 437
mask >>= 1;103
}104
}105
s t a t i c void Tree_ heade rHelp er ( TreeNode * tn ,106
FILE * outfptr ,107
unsigned char * whichbit ,108
unsigned char * curbyte )109
{110
i f ( tn == NULL )111
{112
return ; // should not get here113
}114
TreeNode * lc = tn -> left ;115
TreeNode * rc = tn -> right ;116
i f (( lc == NULL ) && ( rc == NULL ) )117
{118
// leaf node119
writeBit ( outfptr , 1 , whichbit , curbyte ) ;120
char2bits ( outfptr , tn -> value , whichbit , c u r b y t e ) ;121
return ;122
}123
Tree _head erHel per ( lc , outfptr , whichbit , curbyte );124
Tree _head erHel per ( rc , outfptr , whichbit , curbyte );125
writeBit ( outfptr , 0 , whichbit , curbyte ) ;126
}127
void Tree_he ader ( TreeNode * tn , unsigned int numChar ,128
char * outfile )129
{130
FILE * outfptr = fopen ( outfile , "w ");131
i f ( outfptr == NULL )132
{133
return ;134
}135
unsigned char whichbit = 0;136
unsigned char curbyte = 0;137
Tree _head erHel per ( tn , outfptr , & whichbit , & curbyte );138
// add one more 0 to end the header139
writeBit ( outfptr , 0 , & whichbit , & c urby t e );140
padZero ( outfptr , & whichbit , & curbyte );141
// write the number of characte r s142
fwrite (& numChar , s i z e o f ( unsigned in t ), 1 , outfptr ) ;143
// add ’ n ’ at the end of the header144
unsigned char newline = ’n ’;145
fwrite (& newline , s i z e o f ( unsigned char ) , 1, outfptr );146
fclose ( outfptr );147
}148
void Tree_d estroy ( T r e eNode * tn )149
{150
i f ( tn == NULL )151
{152
return ;153