哈夫曼树(Huffman-Tree)的C++实现

哈夫曼树的定义

在这里插入图片描述

哈夫曼树的构造

在这里插入图片描述

补充:

当选择的两颗树的权值相同时,则要比较树的高度,更矮的树的头节点应当成为新节点的左子树。

哈夫曼树的代码实现

基于上面的构造过程,这里使用了小根堆来辅助实现。

#include<iostream>
#include<vector>
#include<queue>
using namespace std;

struct TreeNode{

    char val;
    int weight;
    TreeNode* left;
    TreeNode* right;
    TreeNode(char x, int y ) : val(x), weight(y), left(nullptr), right(nullptr){}
    TreeNode(char x, int y, TreeNode *left, TreeNode *right) : val(x), weight(y), left(left), right(right) {}
};


class cmp{
public:
    bool operator()(TreeNode* a, TreeNode* b){
		if(a->weight == b->weight){
            return getheight(a) >= getheight(b);
		}
		return a->weight > b ->weight;
	}

	int getheight(TreeNode* node){
        return node==nullptr ? 0 : max(getheight(node->left),getheight(node->right))+1;
    }
};

class Huffman{
private:
    TreeNode* root;
    priority_queue<TreeNode*,vector<TreeNode*>,cmp> que;

    void midOrderVisit(TreeNode* node){
        if(node){
            midOrderVisit(node->left);
            cout<<"("<<node->val<<", "<<node->weight<<")";
            midOrderVisit(node->right);
        }
    }

    void preOrderVisit(TreeNode* node){
        if(node){
            cout<<"("<<node->val<<", "<<node->weight<<")";
            midOrderVisit(node->left);
            midOrderVisit(node->right);
        }
    }

    int getheight(TreeNode* node){
        return node==nullptr ? 0 : max(getheight(node->left),getheight(node->right))+1;
    }

public:

    void createHuffman(vector<pair<char,int>> data);
    void midOrder();
    void preOrder();

};

void Huffman::createHuffman(vector<pair<char,int>> data){
	for(auto &[ch,weight] : data){
        TreeNode* node = new TreeNode(ch,weight);
        que.push(node);
    }
    while(!que.empty()){
        TreeNode* node1 = que.top();
        que.pop();
        if(que.empty()){
            que.push(node1);
            break;
        }
        TreeNode* node2 = que.top();
        que.pop();
        TreeNode* node3;
        if(node1->weight > node2->weight){
            node3 = new TreeNode('#',node1->weight+node2->weight,node2,node1);
        }else if(node1->weight < node2->weight){
            node3 = new TreeNode('#',node1->weight+node2->weight,node1,node2);
        }else{
            if(getheight(node1)>getheight(node2)){
                node3 = new TreeNode('#',node1->weight+node2->weight,node2,node1);
            }else{
                node3 = new TreeNode('#',node1->weight+node2->weight,node1,node2);
            }
        }
        que.push(node3);
    }
    this->root = que.top();
}

void Huffman::midOrder(){
    cout<<"中序遍历"<<endl;
    midOrderVisit(root);
    cout<<endl;
}

void Huffman::preOrder(){
    cout<<"前序遍历"<<endl;
    preOrderVisit(root);
    cout<<endl;
}

int main(){
    Huffman test;
    vector<pair<char,int>> data;
    data.emplace_back('a',7);
    data.emplace_back('b',5);
    data.emplace_back('c',2);
    data.emplace_back('d',4);
    test.createHuffman(data);
    test.midOrder();
    test.preOrder();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值