Qinteens 2022-11-29 18:27 采纳率: 50%
浏览 11
已结题

哈夫曼树循环寻找最小下标无法找到T??

哈夫曼树循环寻找最小下标无法找到'T'。

The Chinese official said he viewed the Trump Presidency not as an aberration but as the product of a failing political system.This jibes with other accounts.The Chinese leadership believes that the United States, and Western democracies in general, haven't risen to the challenge of a globalized economy, which necessitates big changes in production patterns, as well as major upgrades in education and public infrastructure. In Trump and Trumpism, the Chinese see an inevitable backlash to this failure.

以下是主要代码,编码之后发现只有‘T’编不出来。

//判断字符是否重复出现过
bool JudgeWords(char ch, int j)
{ 
    int i = 0;
    for (i = 0; i < j; i++)//重复出现
    {
        if(HT[i].data==ch)
            return true;//1
    }
    return false;//0
}
//计算各个字符个数以及频率
int CountWords(char* s)
{
    int j = 0,Total=0;
    for (int i = 0;s[i]!='\0'; i++)
    {
        char ch = s[i];
        if (!JudgeWords(ch, j))//没有重复
        {
            HT[j].data = ch;
            HT[j].rate = 1;//出现频率
            j++;
        }
        else//有重复
        {
            for (int k = 0; k < j; k++)
            {
                if (HT[k].data == ch)
                    HT[k].rate++;
            }
        }
    }
    for (int q = 0; q < j; q++)//计算总字符个数
    {
        Total = HT[q].rate + Total;
    }
    cout << "该篇文章不同的字符的个数为:" << j << endl;
    cout << "该篇文章总字符的个数为:" << Total << endl;
    cout << endl;
    for (int i = 0; i < j; i++)
    {
        cout << HT[i].data << "的频率为:" << HT[i].rate << endl;
    }
    return j;
}
void InputWeight(HuffmanTree*& T, int len, int& n1, int& n2)
{
    int min1 = 32767, min2 = 32767;//先赋最大值,不影响后面运行
    for (int i = 1; i <= len; i++)//寻找第一个最小值
    {
        if (T[i]->parent == 0 && T[i]->weight <= min1)
        {
            min1 = T[i]->weight;
            n1 = i;//记录是第几个结点,以免后面重复寻找
        }
    }
    //cout <<"min1:"<< min1 << endl;
    int tmp = T[n1]->weight;
    T[n1]->weight = 32767;//防止后面重复查找
    for (int i = 1; i <= len; i++)
    {
        if (T[i]->parent == 0 && T[i]->weight <= min2)
        {
            min2 = T[i]->weight;
            n2 = i;
        }
        
    }
    //cout << "min2: "<< min2 << endl;

    T[n1]->weight = tmp;//恢复原来的值 
}
//构造哈夫曼树,T[m-1]为根节点
void CreateHuffmanTree(HuffmanTree*& T,int num1)//num1为叶子结点
{
    int n1, n2;//n1,n2为任意两个结点
    if (n <=1)    /////////
        return;
    //m = 2 * n - 1;//m为结点个数
    T = new HuffmanTree[m + 1];//动态创建,范围[1,m]=[0,m-1]
    for (int i = 1; i <= m; i++)//初始化//////////
    {
        T[i]->parent = NULL; 
        T[i]->rchild = NULL; 
        T[i]->lchild = NULL;
    }    
    for (int i = 0; i <=n; i++)//////////
    {
        T[i]->weight = HT[i].rate;//注意:这两个在不同的结构体中
        cout << HT[i].data << ":" << T[i]->weight<<endl;
    }
    for (int i = n + 1; i <= m; i++)//建第n+1个结点为父结点,循环一次建一次
    {
        InputWeight(T, i -1, n1, n2);
        T[n1]->parent = i;
        T[n2]->parent = i;//再次调用InputWeight函数时不会重复找寻
        //T[i]->parent = 0;//新结点的父结点初始化
        T[i]->lchild = n1;
        T[i]->rchild = n2;//T[i]为返回的两个权值最小的结点的父结点
        T[i]->weight = T[n1]->weight + T[n2]->weight;
    }
}

void CharSetHuffmanEnCoding(HuffmanTree*& T, int num1, const string& codingData)
{
    if (T[num1]->rchild == 0 && T[num1]->lchild == 0) {
        cout << HT[num1].data << " : " << codingData << endl;
        T[num1]->word = codingData;
        //string sen = T[num1]->word;
    }
    else {
        string data = codingData + "0";
        CharSetHuffmanEnCoding(T, T[num1]->lchild, codingData + "0");
        CharSetHuffmanEnCoding(T, T[num1]->rchild, codingData + "1");
    }
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 12月7日
    • 创建了问题 11月29日

    悬赏问题

    • ¥15 需要在vitis下实现彩调视频图像累加,并输出
    • ¥15 解决不了的LNK2019错误
    • ¥20 MATLAB仿真三相桥式全控整流电路
    • ¥15 EDA技术关于时序电路设计
    • ¥15 百度文心一言流式返回sse失败
    • ¥15 由于远程方已关闭传输流,身份验证失败
    • ¥15 rt-detr,PCB,目标检测
    • ¥15 有偿求指导实证代码。cfps清洗合并后,无论是构建平衡面板还是非平衡面板,都是只剩几百个样本量。求指导一下哪里出问题了,不要潦草回复
    • ¥15 mutlinichenet
    • ¥50 Qt5.14.2怎样使用qlistwidget存储指针类数据并更新?