经典算法——Huffuman树(Huffman编码)

#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;

//definition of the Huffuman tree node.
struct TreeNode
{
       TreeNode *left;
       TreeNode *right;
       string operatorMark;
       string huffmanCode;
       float probability;   
};

//definition of the list
struct LinkNode
{
       LinkNode *next;
       string operatorMark;
       float probability;
       TreeNode *treeNodePointer;
};

//definition of function createLink
struct LinkNode* createLink(int number)
{
     struct LinkNode *head;
     struct LinkNode *pointer;
     head = new LinkNode;
     head->next = NULL;
     head->treeNodePointer = NULL;
     int count = 1;
     cout<<"Please input the data of the No.1 LinkNode.\n";
     cout<<"Please input the operatorMark : ";
     cin>>head->operatorMark;
     cout<<"Please input the probability : ";
     cin>>head->probability;
     while(count!=number)
     {
         pointer = new LinkNode;
         pointer->next = NULL;
         cout<<"Please input the data of the No."<<count+1<<" LinkNode.\n";
         cout<<"Please input the operatorMark : ";
         cin>>pointer->operatorMark;
         cout<<"Please input the probability : ";
         cin>>pointer->probability;
         pointer->treeNodePointer = NULL;
         pointer->next = head;
         head = pointer;
         count++;
     }
     return head;
}

//definition of the function of linkSort which is used to sort the link.
struct LinkNode* linkSort(struct LinkNode *head)
{
       LinkNode *current;
       LinkNode *beforeCurrent;
       LinkNode *newHead;
       LinkNode *newNode;
       LinkNode *minimum;
       newHead = NULL;
       while(head != NULL)
       {
           for(current=head,minimum=head;current->next!=NULL;current=current->next)
           {
               if((current->next->probability) > (minimum->probability))
               {
                   beforeCurrent = current;
                   minimum = current->next;
               }
           }
           if(newHead == NULL)
           {
              newHead = new LinkNode;
              newHead->treeNodePointer = NULL;
              newHead->next = NULL;
              newHead->operatorMark = minimum->operatorMark;
              newHead->probability = minimum->probability;
              newHead->treeNodePointer = minimum->treeNodePointer;
              newHead->next = NULL;
           }
           else{
                  newNode = new LinkNode;
                  newNode->treeNodePointer = NULL;
                  newNode->next = NULL;
                  newNode->operatorMark = minimum->operatorMark;
                  newNode->probability = minimum->probability;
                  newNode->treeNodePointer = minimum->treeNodePointer;
                  newNode->next = newHead;
                  newHead = newNode;
               }
           if(minimum == head)
           {
               head = head->next;
           }
           else
           {
               beforeCurrent->next = minimum->next;
           }
       }
       return newHead;
}

//definition of the function of outputLink.
void outputLink(struct LinkNode *head)
{
     int count = 1;
     struct LinkNode *pointer;
     cout<<"----------------------------The data of the Link.-------------------------------\n";
     pointer = head;
     if(head != NULL)
     while(pointer != NULL)
     {
         cout<<"The operatorMark and the probability of No."<<count++<<" node are : ";
         cout<<pointer->operatorMark<<" and "<<pointer->probability;
         cout<<endl;
         pointer = pointer->next;
     }

}

//definition of the function of deleteNode
struct LinkNode* deleteNode(struct LinkNode *head,struct LinkNode *deletionNode)
{   
    if(head != NULL)
    {
       deletionNode->operatorMark = head->operatorMark;
       deletionNode->probability = head->probability;
       deletionNode->treeNodePointer = head->treeNodePointer;
       head = head->next;
    }
    return head;
}

struct LinkNode* addNode(struct LinkNode *head,struct LinkNode *addtionNode)
{
       addtionNode->next = head;
       head = addtionNode;
       return head;
}
//definition of the function of build the link
struct TreeNode* createHuffmanTree(struct LinkNode *head,int flag)
{
       struct TreeNode *parentNode;
       struct TreeNode *leftNode;
       struct TreeNode *rightNode;
       struct LinkNode *firstNode;
       struct LinkNode *secondNode;
       struct LinkNode *composeNode;
       while(flag>0)
       {
          firstNode = new LinkNode;           //The First LinkNode deleted from the Link.
          firstNode->next = NULL;
          firstNode->treeNodePointer = NULL;
          secondNode = new LinkNode;          //The Second LinkNode deleted from the Link.
          secondNode->next = NULL;
          secondNode->treeNodePointer = NULL;
          head = deleteNode(head,firstNode);
          head = deleteNode(head,secondNode);
          composeNode = new LinkNode;
          composeNode->next = NULL;
          composeNode->treeNodePointer = NULL;
          composeNode->operatorMark = firstNode->operatorMark + secondNode->operatorMark;
          composeNode->probability = firstNode->probability + secondNode->probability;
          parentNode = new TreeNode;
          parentNode->left = NULL;
          parentNode->right = NULL;
          parentNode->operatorMark = composeNode->operatorMark;
          parentNode->probability = composeNode->probability;
          if(firstNode->treeNodePointer == NULL)
          {
              leftNode = new TreeNode;
              leftNode->left = NULL;
              leftNode->right = NULL;
              leftNode->probability = firstNode->probability;
              leftNode->operatorMark = firstNode->operatorMark;
              parentNode->left = leftNode;
          }
          else
          {
              parentNode->left = firstNode->treeNodePointer;
          }
          if(secondNode->treeNodePointer == NULL)
          {
              rightNode = new TreeNode;
              rightNode->left = NULL;
              rightNode->right = NULL;
              rightNode->probability = secondNode->probability;
              rightNode->operatorMark = secondNode->operatorMark;
              parentNode->right = rightNode;
          }
          else
          {
              parentNode->right = secondNode->treeNodePointer;
          }
          composeNode->treeNodePointer = parentNode;
          head = addNode(head,composeNode);
          head = linkSort(head);
          flag--;
       }
       return parentNode;
}

//The function that can be used to traval the Huffman Tree.
float visitHuffmanTree(struct TreeNode *root,float &total)
{    
     if(root != NULL)
     {
             if(root->left != NULL)
             {
                root->left->huffmanCode = root->huffmanCode + "0";
             }
             if(root->right != NULL)
             {
                root->right->huffmanCode = root->huffmanCode + "1";
             }
             if((root->left == NULL)&&(root->right == NULL))
             {
                cout<<"The Node "<<root->operatorMark<<"'s huffmanCode is : ";
                cout<<root->huffmanCode<<endl;
                total += (root->huffmanCode.size())*(root->probability);
             }
             visitHuffmanTree(root->left,total);
             visitHuffmanTree(root->right,total);
     }
     return total;
}

//The main function.
int main()
{
    int number;
    float total = 0;
    struct LinkNode *head;
    struct TreeNode *root;
    cout<<"-----------------------The Huffman Tree Coding Program.-------------------------\n";
    cout<<"Please input the number of the Link : ";
    cin>>number;
    head = createLink(number);
    outputLink(head);
    head = linkSort(head);
    cout<<"-------------------------------After Sorting.-----------------------------------\n";
    outputLink(head);
    root = createHuffmanTree(head,number-1);
    root->huffmanCode = "";
    cout<<"---------------------------The Data Of Huffman Code.----------------------------\n";
    total = visitHuffmanTree(root,total);
    cout<<"\nThe Huffman Code's Average Length is : "<<total<<endl;
    cout<<"\nPlease Enter Or Press Any Key To Continue.\n";
    cin.get();
    system("PAUSE");
    return 0;

}


运行结果如下:




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值