Huffman-Tree

  1 #include<iostream>
   2 #include<stdlib.h>
   3  using  namespace std;
   4  
  5  // - - - Define the node structure of the Huffman tree- - - 
  6 typedef struct 
  7  {
   8      int weight;      // Weight 
  9      int parent;      // Parent node 
10      int lchild ;
 11      int rchild ;
 12  }HTNode;
 13  
14 HTNode HfNode[ 1000 ];     // Structure array, just open a range╮(╯▽╰)╭
 15  
16  //- - - Define Huffman coding structure- - - 
17 typedef struct 
18  {
 19      int code[ 1000 ];    // Store code 
20      int start;         // Code subscript 
21  }HufCode;
 22  
23 HufCode HfCode[ 1000 ];
 24  
25  // - - - Construct Huffman tree- - - 
26  void CreateHuffmanTree(HTNode HfNode[ 1000 ], int n)
 27  {
 28      int i,j;
 29      int min1,min2;   //Represents the two smallest weights 
30      int x1,x2;     // Represents the subscript number corresponding to the two smallest weight nodes
 31  
32      // A Huffman tree with n leaves has a total of 2n-1 nodes
 33      // Initialize node information 
34      for (i= 0 ;i< 2 *n- 1 ;i++ )
 35      {
 36          HfNode[i].weight= 0 ;
 37          HfNode[i].parent= 0 ;
 38          HfNode[i].lchild = 0 ;
 39          HfNode[i].rchild= 0 ;
 40      }
 41      //Enter the weight of n leaf nodes 
42      cout<< " Please enter the weight of each leaf: " << endl;
 43      for (i= 0 ;i<n;i++ )
 44      {
 45          cin>> HfNode[i] .weight;
 46      }
 47      cout<< "The Huffman tree node from bottom (leaf) to top (follow) from left to right, the node information is: " << endl;
 48      // Start to construct 
49      for (i = 0 ;i<n- 1 ;i++ )
 50      {
 51          // initialize 
52          min1=min2= 666666; // Because min1 and min2 represent the two smallest weights, the initialization is set to a large number 
53          x1=x2=- 666666 ;
 54  
55          for (j= 0 ;j<i+n;j++ )
 56          {
 57              if (HfNode[j].weight<min1&&HfNode[j].parent== 0 )
 58              {
 59                  // Iterative update of information is achieved through this 
60                  min2= min1;
 61                  min1= HfNode[j].weight;
 62                  x2 = x1;
 63                  x1= j;
 64              }
 65             else if(HfNode[j].weight<min2&&HfNode[j].parent==0)
 66             {
 67                 min2=HfNode[j].weight;
 68                 x2=j;
 69             }
 70         }
 71         //更新信息
 72         HfNode[x1].parent=n+i;
 73         HfNode[x2].parent=n+i;
 74         //新结点信息
 75         HfNode[n+i].weight=min1+min2;
 76         HfNode[n+i].lchild=x1;
 77         HfNode[n+i].rchild= x2;
 78          cout<<HfNode[x1].weight<< "  " <<HfNode[x2].weight<< endl;
 79      }
 80  }
 81  // - - - Huffman Encoding - - - 
82  void CreateHuffmanCode(HufCode HfCode[ 1000 ], int n)
 83  {
 84      HufCode cd;       // Define a temporary variable to store information 
85      int i,j;
 86      for (i= 0 ;i<n;i++ )
 87      {
 88          cd.start=n- 1 ;
89          int xb=i;     // Subscript 
90          int p=HfNode[xb].parent;      // Record parent node information 
91          while (p!= 0 )
 92          {
 93              // Left son 0, right son 1 
94              if (HfNode[p].lchild== xb)
 95                  cd.code[cd.start]= 0 ;
 96              else 
97                  cd.code[cd.start]= 1 ;
 98              cd.start--; // code table direction Move one bit before 
99              xb=p;         // Iteratively replace 
100             p=HfNode[xb].parent;
101         }
102         for(j=cd.start+1;j<n;j++)
103         {
104             HfCode[i].code[j]=cd.code[j];
105         }
106         HfCode[i].start=cd.start;
107     }
108 }
109 
110 int main()
111 {
112     int n;
113     cout<<"请输入叶子数n:"<<endl;
114     cin>>n;
115     CreateHuffmanTree(HfNode,n);
116     CreateHuffmanCode(HfCode,n);
117     for(int i=0;i<n;i++)
118     {
119         cout<<HfNode[i].weight<<"对应的哈夫曼编码是: ";
120         for(int j=HfCode[i].start+1;j<n;j++)
121         {
122             cout<<HfCode[i].code[j];
123         }
124         cout<<"\n";
125     }
126     return 0;
127 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325179326&siteId=291194637