---
title: "哈夫曼编码"
author: "J2EE大鸟"
date: "2013-02-17T00:07:09Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2013-02-17T00:07:09Z"
id: 1801061
url: "https://benworld.iteye.com/blog/1801061"
---

装载请注明涞源[chengyaogen.blog.chinaunix.net](http://blog.chinaunix.net/link.php?url=http%3A%2F%2Fchengyaogen.blog.chinaunix.net%20)

 

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

 

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

 

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

 

[![](http://blog.chinaunix.net/attachment/201204/4/26833883_1333527378Zto6.gif)](http://blog.chinaunix.net/attachment/201204/4/26833883_1333527378Zto6.gif)

 

001. #include &lt;stdio.h&gt;
002. #include &lt;stdlib.h&gt;
003. #include &lt;string.h&gt;
004. //哈夫曼树结点
005. typedef struct HuffNode
006. {
007. int weight;
008. char ch;
009. char code\[20];
010. struct HuffNode \*rchild;
011. struct HuffNode \*lchild;
012. }HuffMan;
013. //队列设计
014. typedef struct \_node_
015. {
016. HuffMan \*data;
017. struct \_node_ \*next;
018. }ListNode;
019. typedef struct
020. {
021. ListNode \*front;
022. ListNode \*rear;
023. }Queue;
024. //create empty queue
025. Queue \*create\_empty\_queue()
026. {
027. ListNode \*HList;
028. Queue \*Hqueue;
029. HList = (ListNode \*)malloc(sizeof(ListNode));
030. HList-&gt;next = NULL;
031. Hqueue = (Queue \*)malloc(sizeof(Queue));
032. Hqueue-&gt;front = Hqueue-&gt;rear = HList;
033. return Hqueue;
034. }
035. //入队
036. int EnterQueue(Queue \*head,HuffMan \*data)
037. {
038. ListNode \*temp;
039. temp = (ListNode \*)malloc(sizeof(ListNode));
040. temp-&gt;data = data;
041. temp-&gt;next = NULL;
042. head-&gt;rear-&gt;next = temp;
043. head-&gt;rear = temp;
044. return 0;
045. }
046. //有序插入结点
047. int OrderEnterQueue(Queue \*head,HuffMan \*p)
048. {
049. ListNode \*m = head-&gt;front-&gt;next;
050. ListNode \*n = head-&gt;front;
051. ListNode \*temp;
052. while(m)
053. {
054. if(m-&gt;data-&gt;weight &lt; p-&gt;weight)
055. {
056. m = m-&gt;next;
057. n = n-&gt;next;
058. }
059. else{
060. break;
061. }
062. }
063. //插到最后一个结点
064. if(m == NULL)
065. {
066. temp = (ListNode \*)malloc(sizeof(ListNode));
067. temp-&gt;data = p;
068. temp-&gt;next = NULL;
069. n-&gt;next = temp;
070. head-&gt;rear = temp;
071. return 0;
072. }
073. //插入中间结点
074. temp = (ListNode \*)malloc(sizeof(ListNode));
075. temp-&gt;data = p;
076. n-&gt;next = temp;
077. temp-&gt;next = m;
078. return 0;
079. }
080. //判断队列是否为空(注意，我们认为队列含有一个结点为空，想想为什么
081. //这样做?
082. int \_is\_empty\_queue(Queue \*head)
083. {
084. if(head-&gt;front-&gt;next-&gt;next == NULL)
085. {
086. printf("is\_empty\_queue\\n");
087. return 1;
088. }
089. return 0;
090. }
091. //判断队列是否为空
092. int is\_empty\_queue(Queue \*head)
093. {
094. if(head-&gt;front == head-&gt;rear)
095. return 1;
096. else
097. return 0;
098. }
099. //出队
100. HuffMan \*DeleteQueue(Queue * head)
101. {
102. ListNode \*temp;
103. temp = head-&gt;front;
104. head-&gt;front = temp-&gt;next;
105. free(temp);
106. temp = NULL;
107. return head-&gt;front-&gt;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-&gt;weight = left-&gt;weight + right-&gt;weight;
120. current-&gt;rchild = right;
121. current-&gt;lchild = left;
122. OrderEnterQueue(head,current);
123. }
124. return head-&gt;front-&gt;next-&gt;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-&gt;rchild == NULL && current-&gt;lchild == NULL)
137. {
138. printf("%c:%d %s\\n",current-&gt;ch,current-&gt;weight,current-&gt;code);
139. }
140. if(current-&gt;lchild)
141. {
142. strcpy(current-&gt;lchild-&gt;code,current-&gt;code);
143. strcat(current-&gt;lchild-&gt;code,"0");
144. EnterQueue(queue, current-&gt;lchild);
145. }
146. if(current-&gt;rchild)
147. {
148. strcpy(current-&gt;rchild-&gt;code,current-&gt;code);
149. strcat(current-&gt;rchild-&gt;code,"1");
150. EnterQueue(queue, current-&gt;rchild);
151. }
152. }
153. return 0;
154. }

运行结果:

[![](http://blog.chinaunix.net/attachment/201204/4/26833883_1333527672GKzO.jpg)](http://blog.chinaunix.net/attachment/201204/4/26833883_1333527672GKzO.jpg)