哈夫曼树(Huffman Tree),又叫最优二叉树,指的是对于一组具有确定权值的叶子结点的具有最小带权路径长度的二叉树。

(1)路劲(Path):从树中的一个结点到另一个结点之间的分支构成两个结点间的路径。
(2)路径长度(Path Length):路径上的分支树。
(3)树的路径长度(Path Length of Tree):从树的根结点到每个结点的路径长度之和。在结点数目相同的二叉树中,完全二叉树的路径长度最短。
(4)结点的权(Weight of  Node):在一些应用中,赋予树中结点的一个有实际意义的树。
(5)结点的带权路径长度(Weight Path Length of Node):从该结点到树的根结点的路径长度与该结点的权的乘积。
(6)树的带权路径长度(WPL):树中所有叶子结点的带权路径长度之和,记为          

26833883_1333284213e48q.jpeg


在下图所示的四棵二叉树,都有4个叶子结点,其权值分别1、2、3、4,他们的带权路径长度分别为:

(a)WPL = 1 x 2 + 2 x 2 + 3 x 2 + 4 X 2 = 20
(b)WPL = 1 x 1 + 2 x 2 + 3 x 3 + 4 x 3 = 28
(c)WPL  = 1 x 3 + 2 x 3 + 3 x 2 + 4 x 1 = 19
(d)WPL = 2 x 1 + 1 x 2 + 3 x 3 + 4 x 3 = 29
26833883_1333284327h7cB.jpeg
其中,(c)图所示的二叉树的带权路径长度最小,这棵树就是哈夫曼树。可以验证,哈夫曼树的带权路径长度最小。

哈夫曼树的构造算法

假设有n个权值,则构造出得哈夫曼树有n个叶子结点。n个权值分别设为w1,w2,...,wn,则哈夫曼树的构造规则为:

(1)将w1,w2,...,wn看成是有n棵树的森林(每棵树仅有一个结点);
(2)在森林中选出两个根结点的权值最小的树合并,作为一棵新树的左、右子树,且新树的根结点权值为其左、右子树根结点权值之和;
(3)从森林中删除选取的两棵树,并将新树加入森林;
(4)重复(2)、(3)步,直到森林中只剩一棵树为止,该树即为所求得的哈夫曼树。

下面我们实现一下吧!
  1. #include <stdio.h>

  2. #include <stdlib.h>


  3. #define MAX 4


  4. int input_weight(int*p)

  5. {

  6. int i = 0;


  7. for(i = 0;i < MAX;i ++)

  8. {

  9.        scanf("%d",p + i);

  10. }


  11. while(getchar()!='\n');


  12.    return 0;

  13. }


  14. int order_weight(int*p)

  15. {

  16. int i = 0;

  17. int j = 0;

  18. int temp;


  19. for(i = 0;i < MAX;i ++)

  20. {

  21. for(j = 0;j < MAX - i - 1;j ++)

  22. {

  23. if(p[j]> p[j+1])

  24. {

  25.                temp = p[j];

  26.                p[j]= p[j+1];

  27.                p[j+1]= temp;

  28. }

  29. }

  30. }


  31.    return 0;

  32. }


  33. //哈夫曼树结点

  34. typedef struct HuffNode

  35. {

  36. int weight;

  37.    struct HuffNode *rchild;

  38.    struct HuffNode *lchild;


  39. }HuffMan;


  40. //队列设计

  41. typedef struct _node_

  42. {

  43.    HuffMan *data;

  44.    struct _node_ *next;

  45. }ListNode;


  46. typedef struct

  47. {

  48.    ListNode *front;

  49.    ListNode *rear;

  50. }Queue;


  51. //create empty queue

  52. Queue *create_empty_queue()

  53. {

  54.    ListNode *HList;

  55.    Queue *Hqueue;


  56.    HList =(ListNode *)malloc(sizeof(ListNode));

  57.    HList->next=NULL;


  58.    Hqueue =(Queue *)malloc(sizeof(Queue));

  59.    Hqueue->front = Hqueue->rear = HList;


  60.    return Hqueue;

  61. }


  62. //入队

  63. int EnterQueue(Queue *head,HuffMan *data)

  64. {

  65.    ListNode *temp;


  66.    temp =(ListNode *)malloc(sizeof(ListNode));

  67.    temp->data = data;

  68.    temp->next=NULL;


  69.    head->rear->next= temp;

  70.    head->rear = temp;


  71.    return 0;

  72. }


  73. //有序插入结点

  74. int OrderEnterQueue(Queue *head,HuffMan *p)

  75. {

  76.    ListNode *m = head->front->next;

  77.    ListNode *n = head->front;

  78.    ListNode *temp;

  79. #if 0

  80. if(head->front->next==NULL)

  81. {

  82.        printf("emty!\n");

  83.        temp =(ListNode *)malloc(sizeof(ListNode));

  84.        temp->data = p;

  85.        temp->next=NULL;

  86.        n->next= temp;

  87.        head->rear = temp;

  88. }

  89. #endif

  90. while(m)

  91. {

  92. if(m->data->weight < p->weight)

  93. {

  94.            m = m->next;

  95.            n = n->next;

  96. }

  97. else{


  98.            break;

  99. }

  100. }


  101. //插到最后一个结点

  102. if(m ==NULL)

  103. {

  104.        temp =(ListNode *)malloc(sizeof(ListNode));

  105.        temp->data = p;

  106.        temp->next=NULL;

  107.        n->next= temp;

  108.        head->rear = temp;

  109. }


  110. //插入中间结点

  111.    temp =(ListNode *)malloc(sizeof(ListNode));

  112.    temp->data = p;

  113.    n->next= temp;

  114.    temp->next= m;


  115.    return 0;

  116. }


  117. //判断队列是否为空(注意,我们认为队列含有一个结点为空,想想为什么

  118. //这样做?

  119. int is_empty_queue(Queue *head)

  120. {

  121. if(head->front->next->next==NULL)

  122. {

  123.        printf("is_empty_queue\n");

  124.        return 1;

  125. }


  126.    return 0;

  127. }


  128. //出队

  129. HuffMan *DeleteQueue(Queue * head)

  130. {

  131.    ListNode *temp;


  132.    temp = head->front;

  133.    head->front = temp->next;

  134.    free(temp);

  135.    temp =NULL;


  136.    return head->front->data;

  137. }


  138. //将结点按权值从小到大放入队列

  139. int create_forest(Queue *head,int*p)

  140. {

  141. int i = 0;

  142.    HuffMan *temp;


  143. for(i = 0;i < MAX;i ++)

  144. {

  145.        temp =(HuffMan *)malloc(sizeof(HuffMan));

  146.        temp->weight = p[i];

  147.        temp->rchild = temp->rchild =NULL;


  148.        EnterQueue(head,temp);

  149. }


  150.    return 0;

  151. }


  152. //创建哈夫曼树

  153. HuffMan *create_huffman_tree(Queue *head)

  154. {

  155.    HuffMan *right,*left,*current;


  156. //循环结束时,队列只含有一个结点

  157. while(!is_empty_queue(head))

  158. {

  159. left= DeleteQueue(head);

  160. right= DeleteQueue(head);

  161.        current =(HuffMan *)malloc(sizeof(HuffMan));

  162.        current->weight =left->weight +right->weight;

  163.        current->rchild =right;

  164.        current->lchild =left;

  165. #if 0

  166.        printf("%d\n",left->weight);

  167.        printf("%d\n",right->weight);

  168.        printf("%d\n",current->weight);

  169. #endif

  170.        OrderEnterQueue(head,current);


  171. }


  172.    return head->front->next->data;

  173. }



  174. //访问结点

  175. int vist_node(HuffMan *p)

  176. {

  177.    printf("%d ",p->weight);


  178.    return 0;

  179. }


  180. //树的中序遍历

  181. int InOrder(HuffMan *p)

  182. {

  183. if(p !=NULL)

  184. {

  185.        InOrder(p->lchild);//

  186.        vist_node(p);//

  187.        InOrder(p->rchild);//

  188. }


  189.    return 0;

  190. }


  191. int main()

  192. {

  193. int Wbuf[MAX];

  194.    Queue *head;

  195.    HuffMan *root;


  196. //输入权值

  197.    input_weight(Wbuf);

  198. //排序权值

  199.    order_weight(Wbuf);

  200. //创建空队列

  201.    head = create_empty_queue();

  202. //将结点按权值从小到大放入队列

  203.    create_forest(head,Wbuf);

  204. //创建哈夫曼树

  205.    root = create_huffman_tree(head);

  206. //中序遍历

  207.    InOrder(root);

  208.    printf("\n");


  209.    return 0;

  210. }

运行结果:
26833883_133328447127KS.png