【C++算法】构建最优哈夫曼树

【C++算法】构建最优哈夫曼树

作者:爱写代码的刚子

时间:2024.1.20

前言:本篇博客的代码均为自己独立完成,可能会有瑕疵

代码实现

c 复制代码
#include <iostream>
#include <vector>
#include <queue>
using namespace std;


template <class T>
class TreeNode
{
public:
    TreeNode(const T&value)
            :_parent(nullptr)
            ,_left(nullptr)
            ,_right(nullptr)
            ,_value(value)
            ,_a(0)
    {}

    TreeNode(const T&value,const int& a)
            :_parent(nullptr)
            ,_left(nullptr)
            ,_right(nullptr)
            ,_value(value)
            ,_a(a)
    {}
    TreeNode* _parent;
    TreeNode* _left;
    TreeNode* _right;
    T _value;
    int _a;

};


template <class T>
class Huffman_tree
{
    typedef TreeNode<T> Node;
public:
    Huffman_tree()
            :_root(nullptr)
    {}

    template<class F>
    struct greater
    {
        bool operator()(const F & x,const F & y) const
        {
            if(x->_value==y->_value)
            {
                printf("equal\n");
                return x->_a>y->_a;
            }

            return x->_value>y->_value;
        }
    };


    void creat_Huffman_tree(vector<T> v)
    {
        sort(v.begin(),v.end());
        priority_queue<Node* ,vector<Node* >,greater<Node*>> pq;

        for(auto& e:v)
        {
            Node* node=new Node(e);
            pq.push(node);
        }
        while(!pq.empty()) {
            Node *left = pq.top();
            pq.pop();
            Node *right = nullptr;
            if (!pq.empty()) {
                right = pq.top();
                pq.pop();

            } else {
                _root = left;
                return;
            }
            Node *parent = new Node(left->_value + right->_value, 1);
            left->_parent = parent;
            right->_parent = parent;

            parent->_left = left;
            parent->_right = right;
            //test(parent);

            pq.push(parent);

        }
    }

    void Print()
    {
        Node* copy=_root;
        _Print(copy);
        cout<<"这是前序遍历\n"<<endl;
    }
    void test(Node* test)
    {
        _test(test);
        cout<<endl;
    }


private:
    Node* _root;

    void _Print(Node* root)
    {
        if(root==nullptr)
        {
            return;
        }
        cout<<root->_value<<" ";

        _Print(root->_left);
        _Print(root->_right);

    }
    void _test(Node* test)
    {
        if(test==nullptr)
        {
            return;
        }
        cout<<test->_value<<" ";

        _Print(test->_left);
        _Print(test->_right);
    }
};
c 复制代码
int main()
{
    vector<int> v{19,21,32,2,3,6,7,10};
    vector<int> v1{3,4,5,6,7,8,9};

    Huffman_tree<int> hf;
    hf.creat_Huffman_tree(v);
    hf.Print();

    return 0;
}

思路

采用优先级队列,将数据进行插入处理。

这个代码还有优化的地方:creat_Huffman_tree(v);采用了vector的拷贝构造,可以换成迭代器直接构造。

核心函数: void creat_Huffman_tree(vector<T> v);

相关推荐
Jane-66677719 分钟前
C语言——栈与队列
c语言·开发语言
“抚琴”的人20 分钟前
C# 取消机制(CancellationTokenSource/CancellationToken)
开发语言·c#·wpf·1024程序员节·取消机制
Shinom1ya_25 分钟前
算法 day 32
算法
Halo_tjn25 分钟前
Java Map集合
java·开发语言·计算机
lsx20240627 分钟前
DOM 创建节点
开发语言
richxu202510011 小时前
C语言<<超全.超重要>>知识点总结
c语言·开发语言
2501_916008891 小时前
用多工具组合把 iOS 混淆做成可复用的工程能力(iOS混淆|IPA加固|无源码混淆|Ipa Guard|Swift Shield)
android·开发语言·ios·小程序·uni-app·iphone·swift
胎粉仔1 小时前
Swift 初阶 —— inout 参数 & 数据独占问题
开发语言·ios·swift·1024程序员节
WBluuue2 小时前
数据结构与算法:摩尔投票算法
c++·算法·leetcode
Lizhihao_2 小时前
Python如何写Selenium全攻略
开发语言·python