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

相关推荐
我真的不会C2 分钟前
QT窗口相关控件及其属性
开发语言·qt
CodeCraft Studio2 分钟前
Excel处理控件Aspose.Cells教程:使用 Python 在 Excel 中进行数据验
开发语言·python·excel
火柴盒zhang8 分钟前
websheet之 编辑器
开发语言·前端·javascript·编辑器·spreadsheet·websheet
景天科技苑15 分钟前
【Rust】Rust中的枚举与模式匹配,原理解析与应用实战
开发语言·后端·rust·match·enum·枚举与模式匹配·rust枚举与模式匹配
知来者逆16 分钟前
计算机视觉——速度与精度的完美结合的实时目标检测算法RF-DETR详解
图像处理·人工智能·深度学习·算法·目标检测·计算机视觉·rf-detr
阿让啊21 分钟前
C语言中操作字节的某一位
c语言·开发语言·数据结构·单片机·算法
এ᭄画画的北北21 分钟前
力扣-160.相交链表
算法·leetcode·链表
椰羊~王小美26 分钟前
LeetCode -- Flora -- edit 2025-04-25
java·开发语言
草莓啵啵~1 小时前
搜索二叉树-key的搜索模型
数据结构·c++
共享家95271 小时前
深入理解C++ 中的list容器
c++