哈夫曼树的建立,编码,译码,先中后遍历

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxN = 1024 + 5;
const int maxM = 1;
int n;
string code[maxN];
struct hufmtree{
    double val;
    char data;//结点的标号ABCDEF
    int lsn, rsn, root;
    int id;//结点的序号
    hufmtree(double a = 0, char b = NULL, int c = -1, int d = -1, int e = -1, int f = 0) : val(a), data(b), lsn(c), rsn(d), root(e), id(f) {}
    friend bool operator < (hufmtree h1, hufmtree h2) { return h1.val < h2.val; }
}tree[maxN];
multiset<hufmtree>st;
void In()
{
    scanf("%d", &n);
    for(int i = 0; i < n; i ++ )
    {
        double dis; scanf("%lf", &dis);
        getchar();
        char da; scanf("%c", &da);
        tree[i] = hufmtree(dis, da, -1, -1, -1, i);
        st.insert(tree[i]);
    }
}
void creat_hufm()
{
    int cnt = 0;
    while(!st.empty())
    {
        hufmtree min_ = *st.begin();
        st.erase(st.begin());
        hufmtree max_ = *st.begin();
        st.erase(st.begin());
        tree[n + cnt] = hufmtree(min_.val + max_.val, NULL, min_.id, max_.id, -1, n + cnt);
        if(tree[n + cnt].val < 1)
            st.insert(tree[n + cnt]);
        tree[min_.id].root = n + cnt;
        tree[max_.id].root = n + cnt;
        cnt ++;
    }
}
void HfumCode()
{
    for(int i = 0; i < n; i ++)
    {
        int now = i;
        for(int pos = tree[i].root; ~pos ; pos = tree[pos].root)
        {
            if(tree[pos].lsn == now)
                code[i] += '0';
            else
                code[i] += '1';
            now = pos;
        }
        reverse(code[i].begin(), code[i].end());
        printf("%c:", tree[i].data);
        cout << code[i] << endl;
    }
}
void TransCode(string ts)
{
    int m = 2 * n - 1;
    int tmp = m - 1;
    int pos = 0;
    while(ts[pos] != '\0')
    {
        if(ts[pos] == '0')
            tmp = tree[tmp].lsn;
        else if(ts[pos] == '1')
            tmp = tree[tmp].rsn;
        if(tree[tmp].lsn == -1)
        {
            putchar(tree[tmp].data);
            tmp = m - 1;
        }
        pos ++;
    }
    if(tree[tmp].lsn != -1 && tmp != m - 1)
        printf("\nERROR\n");
    else
        printf("\n");
}
void pre(int u)   //先序遍历
{
    printf("%d ", u);
    if(tree[u].lsn != -1) pre(tree[u].lsn);
    if(tree[u].rsn != -1) pre(tree[u].rsn);
}
void mid(int u)   //中序遍历
{
    if(tree[u].lsn != -1) mid(tree[u].lsn);
    printf("%d ", u);
    if(tree[u].rsn != -1) mid(tree[u].rsn);
}
void last(int u)   //后序遍历
{
    if(tree[u].lsn != -1) last(tree[u].lsn);
    if(tree[u].rsn != -1) last(tree[u].rsn);
    printf("%d ", u);
}
int main()
{
    In();
    creat_hufm();
    HfumCode();
    string ts; cin >> ts;
    TransCode(ts);
    int rt = 2 * n - 2;
    pre(rt); putchar('\n');
    mid(rt); putchar('\n');
    last(rt); putchar('\n');
    return 0;
}
/*
6
0.4 A
0.3 B
0.1 C
0.1 D
0.02 E
0.08 F
111010110001101
 */
 CBEAF
发布了180 篇原创文章 · 获赞 54 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_44049850/article/details/103130682