【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);

相关推荐
Yhame.2 分钟前
【使用层次序列构建二叉树(数据结构C)】
c语言·开发语言·数据结构
言之。8 分钟前
【Go语言】RPC 使用指南(初学者版)
开发语言·rpc·golang
雾月5526 分钟前
LeetCode 1292 元素和小于等于阈值的正方形的最大边长
java·数据结构·算法·leetcode·职场和发展
投笔丶从戎1 小时前
Kotlin Multiplatform--01:项目结构基础
android·开发语言·kotlin
OpenC++1 小时前
【C++QT】Buttons 按钮控件详解
c++·经验分享·qt·leetcode·microsoft
杜小暑1 小时前
动态内存管理
c语言·开发语言·动态内存管理
想不明白的过度思考者1 小时前
Java从入门到“放弃”(精通)之旅——JavaSE终篇(异常)
java·开发语言
YuforiaCode2 小时前
第十二届蓝桥杯 2021 C/C++组 直线
c语言·c++·蓝桥杯
我真的不会C2 小时前
QT窗口相关控件及其属性
开发语言·qt
CodeCraft Studio2 小时前
Excel处理控件Aspose.Cells教程:使用 Python 在 Excel 中进行数据验
开发语言·python·excel