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

相关推荐
rit843249911 小时前
MATLAB实现图像PCA降噪
开发语言·计算机视觉·matlab
爪哇部落算法小助手11 小时前
每日两题day44
算法
*小雪11 小时前
uniapp写H5授权登录及分享,返回到目标页面
开发语言·javascript·uni-app
不穿格子的程序员12 小时前
从零开始写算法——二分-搜索二维矩阵
线性代数·算法·leetcode·矩阵·二分查找
ghie909012 小时前
C#语言中使用“using“关键字的介绍
开发语言·c#
七夜zippoe12 小时前
Java性能调优工具篇:JMH基准测试与Profiler(JProfiler/Async-Profiler)使用指南
java·开发语言·jprofiler·jmh·async-profiler
龙泉寺天下行走12 小时前
MinGW-w64 工具链(GCC 编译器) 的不同构建版本的区别
c++
Kuo-Teng12 小时前
LeetCode 19: Remove Nth Node From End of List
java·数据结构·算法·leetcode·链表·职场和发展·list
Kuo-Teng12 小时前
LeetCode 21: Merge Two Sorted Lists
java·算法·leetcode·链表·职场和发展
代码AC不AC13 小时前
【C++】异常
c++·学习·异常