428 Intermediate C Programming
#in clude < stdlib .h >9
#in clude < values .h >10
#d ef in e ENCODE MODE 011
#d ef in e DECODE MODE 112
void prin tFre q uenc yLate x ( CharFreq * freq u encies );13
void buil dCod e Book Helpe r ( TreeNode * tn , i n t * * codebook ,14
int * row , in t col )15
{16
i f ( tn == NULL )17
{18
return ;19
}20
// is it a leaf node ?21
TreeNode * lc = tn -> left ;22
TreeNode * rc = tn -> right ;23
i f (( lc == NULL ) && ( rc == NULL ) )24
{25
// finish one code26
codebook [* row ][0] = tn -> value ;27
(* row ) ++;28
return ;29
}30
i f ( lc != NULL )31
{32
// populat e this column of the entire s u b t r e e33
int numRow = Tree_le af ( lc );34
int ind ;35
for ( ind = * row ; ind < (* row ) + numRow ; ind ++)36
{37
codebook [ ind ][ col ] = 0;38
}39
buil dCod e Book Helpe r ( lc , codebook , row , col + 1) ;40
}41
i f ( rc != NULL )42
{43
int numRow = Tree_le af ( rc );44
int ind ;45
for ( ind = * row ; ind < (* row ) + numRow ; ind ++)46
{47
codebook [ ind ][ col ] = 1;48
}49
buil dCod e Book Helpe r ( rc , codebook , row , col + 1) ;50
}51
}52
void build CodeBoo k ( T reeNode * root , i n t * * cod e book )53
{54
int row = 0;55
// column start at 1 because [0] stores the char acter56
buil dCod e Book Helpe r ( root , codebook , & row , 1) ;57
}58
void print CodeBoo k ( i n t * * codebook , i n t numRow )59