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

相关推荐
白茶等风121385 分钟前
C#_结构(Struct)详解
开发语言·c#
ephemerals__8 分钟前
【c++】STL简介
开发语言·c++
说文科技13 分钟前
【LeetCode】289.生命游戏
算法·leetcode
UestcXiye16 分钟前
Leetcode16. 最接近的三数之和
c++·leetcode·排序·双指针·数据结构与算法
zyq~16 分钟前
【自学笔记】支持向量机(2)——核函数
笔记·算法·支持向量机·核函数
赤橙红的黄37 分钟前
代理模式-动态代理
java·开发语言·代理模式
luluvx37 分钟前
LeetCode[中等] 155. 最小栈
算法·leetcode·职场和发展
Au_ust40 分钟前
go的结构体、方法、接口
开发语言·golang
三月七(爱看动漫的程序员)1 小时前
Exploring Large Language Models for Knowledge Graph Completion
人工智能·深度学习·算法·语言模型·chatgpt·langchain·知识图谱
-VE-1 小时前
模板初阶(c++)
开发语言·c++