Huffman樹

從樹中一個結點到另一個結點之間的分支構成這兩個結點之間的路徑,路徑上的分支數目稱作路徑長度。樹的路徑長度是從樹根到每個結點的路徑長度之和。結點的帶權路徑長度爲結點到樹根之間的路徑長度與結點上權的乘機,樹的帶權路徑長度爲樹中所有葉子節點的帶權路徑長度之和。

頭文件:

/***************************************************************************************************** 
 *Copyright: Yue Workstation 
 * 
 *FileName: HuffmanTree.h 
 * 
 *Function: Huffman樹的數據結構定義 
 * 
 *Author: Abel Lee 
 * 
 *CreateOn: 2012-2-19 
 * 
 *Log: 2012-2-19 由Abel Lee創建 
 *****************************************************************************************************/ 

#ifndef HUFFMAN_TREE_H 
#define HUFFMAN_TREE_H 

#include "global.h" 

#define NUMBER 4 

typedef struct 
{ 
          int weight; 
          int parent, lchild, rchild; 
}HTNode, *HuffmanTree; 

void PrintHuffmanTree(HuffmanTree ht, int n); 

#endif

源文件:

/***************************************************************************************************** 
 *Copyright:Yue Workstation 
 * 
 *FileName: HuffmanTree.c 
 * 
 *Function: Huffman樹的操作 
 * 
 *Author:Abel Lee 
 * 
 *CreateOn:2012-2-19 
 * 
 *Log:2011-5-3 由Abel Lee創建 
 *****************************************************************************************************/ 

#include "../inc/HuffmanTree.h" 

//最小數 
static int _min(HuffmanTree ht, int i) 
{ 
          int j=0; 
          int temp=0; 

          for(j = 0; j<i; ++j) 
          { 
              if (ht[j].parent == 0) 
              { 
                   temp = j; 
                   break; 
              } 
          } 
          for(j = temp; j<i; ++j) 
          { 
              if (ht[j].parent == 0 && ht[j].weight<ht[temp].weight) 
             { 
                  temp = j; 
             } 
          } 
          return temp; 
} 

//次小數 
static int _sec_min(HuffmanTree ht, int i) 
{ 
         int min = _min(ht, i); 
         int j=0; 
         int temp=0; 
         for(j = 0; j<i; ++j) 
         { 
               if (ht[j].parent == 0 && j != min) 
              { 
                  temp = j; 
                  break; 
               } 
          } 
         for(j = temp; j<i; ++j) 
        { 
             if (ht[j].parent == 0 && ht[j].weight<ht[temp].weight && j != min) 
             { 
                  temp = j; 
            } 
         } 
     return temp; 
} 

//打印赫夫曼樹 
void PrintHuffmanTree(HuffmanTree ht, int n) 
{ 
     int i = 0; 
    int temp = 0; 
    int val = 0; 
    for (i = 0; i < n; ++i) 
    { 
        printf("%d:", ht[i].weight); 
        val = i; 
        temp = ht[i].parent; 
        while (temp) 
        { 
            if (ht[temp].lchild == val) 
            { 
                printf("0"); 
            } 
            else if (ht[temp].rchild == val) 
            { 
                printf("1"); 
            } 
            else 
            { 

            } 
            val = temp; 
            temp = ht[temp].parent; 
        } 
        printf("\n"); 
    } 
} 

/**************************************************************************************************** 
 *Function Name: Huffmancoding 
 * 
 *Function: 獲取霍夫曼編碼 
 * 
 *Parameter:    ht:huffman樹 
 *              w:權值 
 *              n:數量 
 * 
 *Return Value: 無 
 * 
 *Author:Abel Lee 
 * 
 *Log:2012-2-19 
 ***************************************************************************************************/ 
void Huffmancoding(HuffmanTree ht,  int *w, int n) 
{//w 存放 n 個字符的權值,構造赫夫曼樹 ht, 並求出 n 個字符的赫夫曼編碼 hc 

 int m; 
 int i; 
 int min=0, sec_min=0; 
 HuffmanTree p; 

 if (n<=1) return; 
 m = 2*n-1; 
 ht = (HuffmanTree)malloc(m*sizeof(HTNode)); 
 p = ht; 

 for(i=0; i<n; ++i, ++p, ++w)//給葉子節點賦值,前n個爲葉子節點 
 { 
        p->weight = *w; 
        p->parent = 0; 
        p->lchild = 0; 
        p->rchild = 0; 
 } 
 for(; i<m; ++i,++p)//給非葉子節點賦值,第n個到(2*n - 1)個爲葉子節點 
 { 
        p->weight = 0; 
        p->parent = 0; 
        p->lchild = 0; 
        p->rchild = 0; 
 } 

 for(i = n; i<m; ++i) 
 { 
       min = _min(ht, i); 
       sec_min = _sec_min(ht, i); 
       ht[min].parent = ht[sec_min].parent = i; 
       ht[i].lchild = min; 
       ht[i].rchild = sec_min; 
       ht[i].weight = ht[min].weight + ht[sec_min].weight; 
 } 
 PrintHuffmanTree(ht, n); 
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章