Huffman Compression 409
return 0;25
}26
// merge the top two list nodes until only one list node27
while (( head -> next ) != NULL )28
{29
List_pr i nt ( head ); printf (" - --- --- --- - n") ;30
ListNode * second = head -> next ;31
// second must not be NULL , otherwise , will not enter32
ListNode * third = second -> next ;33
// third may be NULL34
// get the tree nodes of the first two list nodes35
TreeNode * tn1 = head -> tnptr ;36
TreeNode * tn2 = second -> tnptr ;37
// remove the first two nodes38
free ( head );39
free ( second );40
head = third ;41
TreeNode * mrg = Tree_me r ge ( tn1 , tn2 ) ;42
ListNode * ln = L istNod e_crea te ( mrg );43
head = List_i nsert ( head , ln );44
}45
List_pr i nt ( head );46
return 1;47
}48
Line 46 prints the linked list, and it should have only one list node. Otherwise, the
function should continue inside while. Calling Tree print prints the tree nodes using a
post-order traversal. Below is the output. Note that it matches Fig. 24.7.
freq = 73 value = 104, ’h’
freq = 46 value = 100, ’d’
freq = 23 value = 112, ’p’
freq = 7 value = 84, ’T’
freq = 1 value = 69, ’E’
freq = 2 value = 78, ’N’
freq = 3
freq = 7 value = 71, ’G’
freq = 10
freq = 17
freq = 19 value = 103, ’g’
freq = 36
freq = 59
freq = 105
freq = 178
Fig. 24.8 shows the list of tree nodes as displayed in the debugging program DDD. DDD
can help you visualize how the list nodes and the tree nodes change as the program makes
progress. Fig. 24.9 shows the tree in DDD after the tree has been completely built. It is the
same tree as Fig. 24.7. The visualization function in DDD can help you see the nodes of the
tree.