C語言數據結構之哈夫曼樹及哈夫曼編碼的實現

代碼清單以下:編碼

 1 #pragma once
 2 #include<stdio.h>
 3 #include"stdlib.h"
 4 #include <string.h>
 5 
 6 typedef int ElemType1;  7 
 8 struct BTreeNode  9 {  10  ElemType1 data;  11     struct BTreeNode* left;  12     struct BTreeNode* right;  13 };  14 //遍歷哈夫曼樹 
 15 void PrintBTree_int(struct BTreeNode* BT)  16 {  17     if (BT != NULL)  18  {  19         printf("%d", BT->data);  20         if (BT->left != NULL || BT->right != NULL)  21  {  22             printf(" ( ");  23             PrintBTree_int(BT->left); //輸出左子樹 
 24             if (BT->right != NULL)  25                 printf(" , ");  26             PrintBTree_int(BT->right); //輸出右子樹 
 27             printf(" ) ");  28  }  29  }  30 }  31 
 32 //建立哈夫曼樹 
 33 struct BTreeNode* CreateHuffman(ElemType1 a[], int n)  34 {  35     int i, j;  36     struct BTreeNode **b, *q;  37     b = (BTreeNode **)malloc(n * sizeof(struct BTreeNode));  38     for (i = 0; i < n; i++)                          //動態內存分配 
 39  {  40         b[i] = (BTreeNode *)malloc(sizeof(struct BTreeNode));  41         b[i]->data = a[i];  42         b[i]->left = b[i]->right = NULL;  43  }  44     for (i = 1; i < n; i++)  45  {  46         //k1表示森林中具備最小權值的樹根結點的下標,k2爲次最小的下標 
 47         int k1 = -1, k2;  48         for (j = 0; j < n; j++)                      //讓k1初始指向森林中第一棵樹,k2指向第二棵 
 49  {  50             if (b[j] != NULL && k1 == -1)  51  {  52                 k1 = j;  53                 continue;  54  }  55             if (b[j] != NULL)  56  {  57                 k2 = j;  58                 break;  59  }  60  }  61         for (j = k2; j < n; j++)                 //構造最優解 
 62  {  63             if (b[j] != NULL)  64  {  65                 if (b[j]->data < b[k1]->data)  66  {  67                     k2 = k1;  68                     k1 = j;  69  }  70                 else if (b[j]->data < b[k2]->data)  71                     k2 = j;  72  }  73  }  74         q = (BTreeNode *)malloc(sizeof(struct BTreeNode));  75         q->data = b[k1]->data + b[k2]->data;  76         q->left = b[k1];  77         q->right = b[k2];  78 
 79         b[k1] = q;  80         b[k2] = NULL;  81  }  82     free(b);  83     return q;  84 }  85 //計算帶權路徑 
 86 ElemType WeightPathLength(struct BTreeNode* FBT, int len)//len初始爲0 
 87 {  88     if (FBT == NULL) //空樹返回0 
 89         return 0;  90     else
 91  {  92         if (FBT->left == NULL && FBT->right == NULL)  93             return FBT->data * len;  94         else
 95             return WeightPathLength(FBT->left, len + 1) + WeightPathLength(FBT->right, len + 1);  96  }  97 }  98 
 99 //構造哈夫曼編碼 
100 void HuffManCoding(struct BTreeNode* FBT, int len) 101 { 102     static int a[10]; 103     if (FBT != NULL) 104  { 105         if (FBT->left == NULL && FBT->right == NULL) 106  { 107             int i; 108             printf("結點的值爲%d的編碼:", FBT->data); 109             for (i = 0; i < len; i++) 110                 printf("%d", a[i]); 111             printf("\n"); 112  } 113         else
114  { 115             a[len] = 0; 116             HuffManCoding(FBT->left, len + 1); 117             a[len] = 1; 118             HuffManCoding(FBT->right, len + 1); 119  } 120  } 121 }
相關文章
相關標籤/搜索