2024年华为OD机试真题-生成哈夫曼树 C/C++解法

基于哈夫曼树的规则生成哈夫曼树,其中用到了常见的二叉树生成逻辑,最后中序遍历使用递归实现。

cpp 复制代码
#include <iostream>
#include <algorithm>
#include <list>

using namespace std;

// 哈夫曼树节点
struct halfman_tree_node
{
    halfman_tree_node *down_left;
    halfman_tree_node *down_right;
    halfman_tree_node *parent;
    int value;
};

/// @brief 二叉树中序遍历
/// @param tnode
static void halfman_tree_middle_print(halfman_tree_node *tnode)
{
    if (tnode == nullptr)
    {
        return;
    }

    halfman_tree_middle_print(tnode->down_left);  // left
    cout << tnode->value << " ";                  // middle
    halfman_tree_middle_print(tnode->down_right); // right
}

// 生成哈夫曼树
static void HuaWei_OD_test26(void)
{
    int tnode_cnt;
    cin >> tnode_cnt;

    list<int> tnode_weight_list;
    for (int i = 0; i < tnode_cnt; i++)
    {
        int tmp;
        cin >> tmp;
        tnode_weight_list.push_back(tmp);
    }

    list<halfman_tree_node *> halfman_tree_list;

    // 第一次升序
    tnode_weight_list.sort();

    while (tnode_weight_list.size() >= 2)
    {
        // 找到最小的两个元素
        auto small_value1 = tnode_weight_list.begin(); // 最小
        auto small_value2 = tnode_weight_list.begin(); // 倒数第二小
        small_value2++;

        // 将两个元素合并为一个新元素
        int new_node_value = *small_value1 + *small_value2;
        halfman_tree_node *new_tnode = new halfman_tree_node;
        new_tnode->value = new_node_value;
        new_tnode->parent = nullptr;

        // 刚开始没有tnode,构建两个tnode
        if (halfman_tree_list.size() < 2)
        {
            halfman_tree_node *left_tnode = new halfman_tree_node;
            left_tnode->down_left = nullptr;
            left_tnode->down_right = nullptr;
            left_tnode->value = *small_value1;

            halfman_tree_node *right_tnode = new halfman_tree_node;
            right_tnode->down_left = nullptr;
            right_tnode->down_right = nullptr;
            right_tnode->value = *small_value2;

            new_tnode->down_left = left_tnode;
            new_tnode->down_right = right_tnode;

            left_tnode->parent = new_tnode;
            right_tnode->parent = new_tnode;

            halfman_tree_list.push_back(left_tnode);
            halfman_tree_list.push_back(right_tnode);
        }
        else
        {
            // 只需要申请一个新的tnode
            halfman_tree_node *x_tnode = new halfman_tree_node;
            x_tnode->down_left = nullptr;
            x_tnode->down_right = nullptr;

            // 此时halfman_tree_list的最后一个元素是目前的最长节点顶部
            auto current_top = halfman_tree_list.back();

            // 比较current_top节点的权重,和当前最小的权重来选择哪一个元素构建一个新的tnode
            if (current_top->value >= *small_value1)
            {
                x_tnode->value = *small_value1;
                new_tnode->down_left = x_tnode; // 更新顶部
                new_tnode->down_right = current_top;
            }
            else
            {
                x_tnode->value = *small_value2;
                new_tnode->down_left = current_top; // 更新顶部
                new_tnode->down_right = x_tnode;
            }

            x_tnode->parent = new_tnode;
            current_top->parent = new_tnode;

            halfman_tree_list.push_back(x_tnode);
        }

        halfman_tree_list.push_back(new_tnode); // 目前的顶部节点

        tnode_weight_list.pop_front(); // 权重列表去掉最小元素
        tnode_weight_list.pop_front(); // 权重列表去掉第二小元素

        tnode_weight_list.push_front(new_node_value); // 加入新的权重元素
        tnode_weight_list.sort();                     // 重新升序排列
    }

    // 到这里说明tnode_weight_list只有一个权重,也就是根节点的权重
    // 也就意味着halfman_tree_list最后一个元素是根节点

    // 开始进行中序遍历
    auto root = halfman_tree_list.back(); // 根节点

    halfman_tree_middle_print(root);

    // 清理内存
    for (const auto &tnode : halfman_tree_list)
    {
        delete tnode;
    }
}

int main()
{
    HuaWei_OD_test26();
    return 0;
}
相关推荐
Wuliwuliii11 分钟前
贡献延迟计算DP
数据结构·c++·算法·动态规划·dp
苦藤新鸡13 分钟前
2.字母异位词分组
c语言·c++·力扣·哈希算法
CryptoRzz20 分钟前
印度交易所 BSE 与 NSE 实时数据 API 接入指南
java·c语言·python·区块链·php·maven·symfony
AAA.建材批发刘哥29 分钟前
02--C++ 类和对象上篇
开发语言·c++
橘颂TA36 分钟前
【Linux】从 “抢资源” 到 “优雅控场”:Linux 互斥锁的原理与 C++ RAII 封装实战(Ⅰ)
linux·运维·服务器·c++·算法
枫叶丹41 小时前
【Qt开发】Qt系统(三)->事件过滤器
java·c语言·开发语言·数据库·c++·qt
Kris_LinSD1 小时前
算法小实验——分治算法快速排序问题实验(含报告)
c语言·算法
Super小白&1 小时前
十大经典排序算法详解(附C语言实现+复杂度分析)
c语言·算法·排序算法
坐怀不乱杯魂1 小时前
Linux - 缓存利用率
linux·c++·缓存
leiming61 小时前
c++ for_each算法
开发语言·c++·算法