ITeye博客

哈夫曼编码

J2EE大鸟 · 2013-02-17 00:07 · javaweb应用问题解决 · 编程语言 · Markdown

装载请注明涞源chengyaogen.blog.chinaunix.net

 

前面一节我们知道了,怎样去创建一个哈夫曼树,这一节我们来看看哈夫曼编码。

 

思想:得到哈夫曼树后,自顶向下按路径编号,指向左节点的边编号0,指向右节点的边编号1,从根到叶节点的所有边上的0和1连接起来,就是叶子节点中字符的哈夫曼编码。

 

下图体现了哈夫曼编码的过程:

 

 

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. //哈夫曼树结点
  5. typedef struct HuffNode
  6. {
  7. int weight;
  8. char ch;
  9. char code[20];
  10. struct HuffNode *rchild;
  11. struct HuffNode *lchild;
  12. }HuffMan;
  13. //队列设计
  14. typedef struct _node_
  15. {
  16. HuffMan *data;
  17. struct _node_ *next;
  18. }ListNode;
  19. typedef struct
  20. {
  21. ListNode *front;
  22. ListNode *rear;
  23. }Queue;
  24. //create empty queue
  25. Queue *create_empty_queue()
  26. {
  27. ListNode *HList;
  28. Queue *Hqueue;
  29. HList = (ListNode *)malloc(sizeof(ListNode));
  30. HList->next = NULL;
  31. Hqueue = (Queue *)malloc(sizeof(Queue));
  32. Hqueue->front = Hqueue->rear = HList;
  33. return Hqueue;
  34. }
  35. //入队
  36. int EnterQueue(Queue *head,HuffMan *data)
  37. {
  38. ListNode *temp;
  39. temp = (ListNode *)malloc(sizeof(ListNode));
  40. temp->data = data;
  41. temp->next = NULL;
  42. head->rear->next = temp;
  43. head->rear = temp;
  44. return 0;
  45. }
  46. //有序插入结点
  47. int OrderEnterQueue(Queue *head,HuffMan *p)
  48. {
  49. ListNode *m = head->front->next;
  50. ListNode *n = head->front;
  51. ListNode *temp;
  52. while(m)
  53. {
  54. if(m->data->weight < p->weight)
  55. {
  56. m = m->next;
  57. n = n->next;
  58. }
  59. else{
  60. break;
  61. }
  62. }
  63. //插到最后一个结点
  64. if(m == NULL)
  65. {
  66. temp = (ListNode *)malloc(sizeof(ListNode));
  67. temp->data = p;
  68. temp->next = NULL;
  69. n->next = temp;
  70. head->rear = temp;
  71. return 0;
  72. }
  73. //插入中间结点
  74. temp = (ListNode *)malloc(sizeof(ListNode));
  75. temp->data = p;
  76. n->next = temp;
  77. temp->next = m;
  78. return 0;
  79. }
  80. //判断队列是否为空(注意,我们认为队列含有一个结点为空,想想为什么
  81. //这样做?
  82. int _is_empty_queue(Queue *head)
  83. {
  84. if(head->front->next->next == NULL)
  85. {
  86. printf("is_empty_queue\n");
  87. return 1;
  88. }
  89. return 0;
  90. }
  91. //判断队列是否为空
  92. int is_empty_queue(Queue *head)
  93. {
  94. if(head->front == head->rear)
  95. return 1;
  96. else
  97. return 0;
  98. }
  99. //出队
  100. HuffMan *DeleteQueue(Queue * head)
  101. {
  102. ListNode *temp;
  103. temp = head->front;
  104. head->front = temp->next;
  105. free(temp);
  106. temp = NULL;
  107. return head->front->data;
  108. }
  109. //创建哈夫曼树
  110. HuffMan *create_huffman_tree(Queue *head)
  111. {
  112. HuffMan *right,*left,*current;
  113. //循环结束时,队列只含有一个结点
  114. while(!_is_empty_queue(head))
  115. {
  116. left = DeleteQueue(head);
  117. right = DeleteQueue(head);
  118. current = (HuffMan *)malloc(sizeof(HuffMan));
  119. current->weight = left->weight + right->weight;
  120. current->rchild = right;
  121. current->lchild = left;
  122. OrderEnterQueue(head,current);
  123. }
  124. return head->front->next->data;
  125. }
  126. //哈夫曼编码
  127. int HuffmanCode(HuffMan *root)
  128. {
  129. HuffMan *current = NULL;
  130. Queue *queue = NULL;
  131. queue = create_empty_queue();
  132. EnterQueue(queue, root);
  133. while(!is_empty_queue(queue))
  134. {
  135. current = DeleteQueue(queue);
  136. if(current->rchild == NULL && current->lchild == NULL)
  137. {
  138. printf("%c:%d %s\n",current->ch,current->weight,current->code);
  139. }
  140. if(current->lchild)
  141. {
  142. strcpy(current->lchild->code,current->code);
  143. strcat(current->lchild->code,"0");
  144. EnterQueue(queue, current->lchild);
  145. }
  146. if(current->rchild)
  147. {
  148. strcpy(current->rchild->code,current->code);
  149. strcat(current->rchild->code,"1");
  150. EnterQueue(queue, current->rchild);
  151. }
  152. }
  153. return 0;
  154. }

运行结果:

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。