| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2741 人关注过本帖
标题:急!哈夫曼树的构造,select函数在CreatHaffumanTree 函数里引用了之后之后 ...
只看楼主 加入收藏
YUkoni
Rank: 2
等 级:论坛游民
帖 子:17
专家分:22
注 册:2018-11-25
结帖率:50%
收藏
已结贴  问题点数:10 回复次数:2 
急!哈夫曼树的构造,select函数在CreatHaffumanTree 函数里引用了之后之后的操作无法继续
以下是代码,printf("AAA")这样的语句是用来看哪里出错的函数。


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
typedef int Status;

typedef struct
{
    int weight;
    int parent,lchild,rchild;
    char name;
}HTNode,*HuffmanTree;

Status Select(HuffmanTree HT,int len,int s1,int s2)//选最小的两个
{
    int x1=0,x2=0,i;
    int m1=1000;
    int m2=1000;
    for(i=1;i<=len;i++)
    {
        if(HT[i].parent==0&&HT[i].weight<m1)//两个最小中的最小
        {
            m2=m1;
            x2=x1;
            m1=HT[i].weight;
            x1=i;
            printf("x1=%d,m1=%d",x1,m1);
        }
        else if(HT[i].parent==0&&HT[i].weight<m2)//两个最小中的最大
        {
            m2=HT[i].weight;
            x2=i;
            printf("x2=%d,m2=%d",x2,m2);
        }
        printf("\n");
    }
    s1=x1;
    s2=x2;
}

void CreatHuffmanTree(HuffmanTree HT,int n)//建树
{
    int m,i,s1,s2;
    if(n<=1) return;
    m=2*n-1;
    HT=new HTNode[m+1];
    for(i=1;i<=m;++i)//把输入的结点弄成只有该结点的树
    {
        HT[i].parent=0;HT[i].lchild=0;HT[i].rchild=0;
    }
    printf("请输入该字符集:");
    for(i=1;i<=n;++i)
        cin>>HT[i].name;
    for(i=1;i<=n;++i)//输入树对应的结点
        {
            printf("结点%c对应的权值为:",HT[i].name);
            scanf("%d",&HT[i].weight);
        }
    for(i=n+1;i<=m;++i)
    {
        Select(HT,i-1,s1,s2);
        printf("AAAA");
        HT[s1].parent=i;HT[s2].parent=i;
        HT[i].lchild=s1;HT[i].rchild=s2;
        HT[i].weight=HT[s1].weight+HT[s2].weight;
        printf("BBBB");
    }
}


int main()
{
    HuffmanTree HT;
    char ch;
    int n,i;
    printf("请输入字符集大小:");
    scanf("%d",&n);
    CreatHuffmanTree(HT,n);
    printf("AAAA");
}
搜索更多相关主题的帖子: 函数 printf int parent for 
2019-06-15 16:28
YUkoni
Rank: 2
等 级:论坛游民
帖 子:17
专家分:22
注 册:2018-11-25
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册
2019-06-15 16:54
幻紫灵心
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:山咔咔里面
等 级:贵宾
威 望:53
帖 子:395
专家分:2640
注 册:2018-3-30
收藏
得分:10 
Status Select(HuffmanTree HT,int len,int& s1,int& s2)//这样不会运行错误了。算法对不对没看。

saber,别哭.
2019-06-15 19:58
快速回复:急!哈夫曼树的构造,select函数在CreatHaffumanTree 函数里引用了之后 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018229 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved