求大神看看我的破代码,哈夫曼树问题

一墨千里无色 2018-05-03 07:12:30
构建和哈夫曼树,并且遍历哈夫曼树。。
不报错,但是遍历不出来是怎么回事。。。
#include<iostream.h>
#include<stdlib.h>
#include<stdio.h>
typedef struct{
int weight;
int parent,lchild,rchild;
}HNode,*HuffmanTree;
HuffmanTree creat(HuffmanTree ht,int n);
void PutInScreen(HuffmanTree ht,int n);//遍历哈夫曼树
int main()
{
int n;
HuffmanTree ht;
ht=creat(ht,n);
PutInScreen(ht,n);
return 0;
}
HuffmanTree creat(HuffmanTree ht,int n)
{ int *select(HuffmanTree ht,int n);
int m,i;
int *a;
cout<<"开始构建哈夫曼树"<<endl;
cout<<"请输入哈夫曼树的叶子节点个数:"<<endl;
cin>>n;
m=2*n-1;
ht=(HuffmanTree)malloc(sizeof(HNode)*(m+1));
for(i=1;i<=m+1;i++)
{
ht[i].parent=0;
ht[i].lchild=0;
ht[i].rchild=0;
}
cout<<"请输入每个叶子节点的权值:"<<endl;
for(i=1;i<=n;i++)
cin>>ht[i].weight;
for(i=n+1;i<=m;i++)
{ a=select(ht,i-1);//在里面(1~n-1)选择两个其双亲域为0且权值最小的结点,并返回它们在HT中的序号s1和s2;
//删除
ht[i].lchild=(*a);
ht[i].rchild=*(a+1);
ht[*a].parent=i;
ht[*(a+1)].parent=i;
ht[i].weight=ht[*a].weight+ht[*(a+1)].weight;
}
return ht;
}

int *select(HuffmanTree ht,int n)
{
int i,j;
int *a;
a=(int *)malloc(sizeof(int)*2);//
for(i=1;i<=n;i++)
{
if(ht[i].parent!=0) continue;
if(ht[i].weight>ht[i+1].weight) (*a)=i;
else (*a)=i+1;
}
for(i=1;i<=n;i++)
{
if(ht[i].parent!=0) continue;
if(ht[i].weight>ht[i+1].weight&&*(a+1)!=(*a)) *(a+1)=i;
else *(a+1)=i+1;
}
return a;
}
void PutInScreen(HuffmanTree ht,int n)
{
int i;
for(i=1;i<=2*n-1;i++)
{
cout<<"结点序号"<<i;
cout<<"权重 "<<ht[i].weight<<" "<<"双亲 "<<ht[i].parent;
cout<<"左孩子 "<<ht[i].lchild<<" "<<"右孩子 "<<ht[i].rchild;

}
}


...全文
836 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
一墨千里无色 2018-05-06
  • 打赏
  • 举报
回复
引用 2 楼 paschen 的回复:
你的n是按值传递的,函数中和函数外是不同的变量,你在函数中输入的n并不影响main中n
谢谢大佬,但是,我的输入一个树,比如:5 29 7 8 14 23 3 11 但是吧,从第九个开始,它就错了,权重就错了。。。为何呢
paschen 2018-05-06
  • 打赏
  • 举报
回复
引用 6 楼 weixin_40884861的回复:
[quote=引用 5 楼 paschen 的回复:] [quote=引用 4 楼 weixin_40884861的回复:][quote=引用 2 楼 paschen 的回复:] 你的n是按值传递的,函数中和函数外是不同的变量,你在函数中输入的n并不影响main中n
谢谢大佬,但是,我的输入一个树,比如:5 29 7 8 14 23 3 11 但是吧,从第九个开始,它就错了,权重就错了。。。为何呢[/quote] 因为你的n没有初始,发你修改后的代码[/quote]#include<iostream.h> #include<stdlib.h> #include<stdio.h> typedef struct{ int weight; int parent,lchild,rchild; }HNode,*HuffmanTree; HuffmanTree creat(HuffmanTree ht,int n); void PutInScreen(HuffmanTree ht,int n);//遍历哈夫曼树 int main() { int n; HuffmanTree ht; cout<<"开始构建哈夫曼树"<<endl; cout<<"请输入哈夫曼树的叶子节点个数:"<<endl; cin>>n; ht=creat(ht,n); PutInScreen(ht,n); return 0; } HuffmanTree creat(HuffmanTree ht,int n) { int *select(HuffmanTree ht,int n); int m,i; int *a; m=2*n-1; ht=(HuffmanTree)malloc(sizeof(HNode)*(m+1)); for(i=1;i<=m+1;i++) { ht[i].parent=0; ht[i].lchild=0; ht[i].rchild=0; } cout<<"请输入每个叶子节点的权值:"<<endl; for(i=1;i<=n;i++) cin>>ht[i].weight; for(i=n+1;i<=m;i++) { a=select(ht,i-1);//在里面(1~n-1)选择两个其双亲域为0且权值最小的结点,并返回它们在HT中的序号s1和s2; //删除 ht[i].lchild=(*a); ht[i].rchild=*(a+1); ht[*a].parent=i; ht[*(a+1)].parent=i; ht[i].weight=ht[*a].weight+ht[*(a+1)].weight; } return ht; } int *select(HuffmanTree ht,int n) { int i,j; int *a; a=(int *)malloc(sizeof(int)*2);// for(i=1;i<=n;i++) { if(ht[i].parent!=0) continue; if(ht[i].weight>ht[i+1].weight) (*a)=i; else (*a)=i+1; } for(i=1;i<=n;i++) { if(ht[i].parent!=0) continue; if(ht[i].weight>ht[i+1].weight&&*(a+1)!=(*a)) *(a+1)=i; else *(a+1)=i+1; } return a; } void PutInScreen(HuffmanTree ht,int n) { int i; for(i=1;i<=2*n-1;i++) { cout<<"结点序号 "<<i; cout<<" 权重 "<<ht[i].weight<<" "<<"双亲 "<<ht[i].parent; cout<<"左孩子 "<<ht[i].lchild<<" "<<"右孩子 "<<ht[i].rchild; cout<<endl; } } [/quote] for (i = 1; i <= m + 1; i++) 当i=m+1时已经越界了
paschen 2018-05-06
  • 打赏
  • 举报
回复
引用 4 楼 weixin_40884861的回复:
[quote=引用 2 楼 paschen 的回复:] 你的n是按值传递的,函数中和函数外是不同的变量,你在函数中输入的n并不影响main中n
谢谢大佬,但是,我的输入一个树,比如:5 29 7 8 14 23 3 11 但是吧,从第九个开始,它就错了,权重就错了。。。为何呢[/quote] 因为你的n没有初始,发你修改后的代码
一墨千里无色 2018-05-06
  • 打赏
  • 举报
回复
引用 5 楼 paschen 的回复:
[quote=引用 4 楼 weixin_40884861的回复:][quote=引用 2 楼 paschen 的回复:] 你的n是按值传递的,函数中和函数外是不同的变量,你在函数中输入的n并不影响main中n
谢谢大佬,但是,我的输入一个树,比如:5 29 7 8 14 23 3 11 但是吧,从第九个开始,它就错了,权重就错了。。。为何呢[/quote] 因为你的n没有初始,发你修改后的代码[/quote]#include<iostream.h> #include<stdlib.h> #include<stdio.h> typedef struct{ int weight; int parent,lchild,rchild; }HNode,*HuffmanTree; HuffmanTree creat(HuffmanTree ht,int n); void PutInScreen(HuffmanTree ht,int n);//遍历哈夫曼树 int main() { int n; HuffmanTree ht; cout<<"开始构建哈夫曼树"<<endl; cout<<"请输入哈夫曼树的叶子节点个数:"<<endl; cin>>n; ht=creat(ht,n); PutInScreen(ht,n); return 0; } HuffmanTree creat(HuffmanTree ht,int n) { int *select(HuffmanTree ht,int n); int m,i; int *a; m=2*n-1; ht=(HuffmanTree)malloc(sizeof(HNode)*(m+1)); for(i=1;i<=m+1;i++) { ht[i].parent=0; ht[i].lchild=0; ht[i].rchild=0; } cout<<"请输入每个叶子节点的权值:"<<endl; for(i=1;i<=n;i++) cin>>ht[i].weight; for(i=n+1;i<=m;i++) { a=select(ht,i-1);//在里面(1~n-1)选择两个其双亲域为0且权值最小的结点,并返回它们在HT中的序号s1和s2; //删除 ht[i].lchild=(*a); ht[i].rchild=*(a+1); ht[*a].parent=i; ht[*(a+1)].parent=i; ht[i].weight=ht[*a].weight+ht[*(a+1)].weight; } return ht; } int *select(HuffmanTree ht,int n) { int i,j; int *a; a=(int *)malloc(sizeof(int)*2);// for(i=1;i<=n;i++) { if(ht[i].parent!=0) continue; if(ht[i].weight>ht[i+1].weight) (*a)=i; else (*a)=i+1; } for(i=1;i<=n;i++) { if(ht[i].parent!=0) continue; if(ht[i].weight>ht[i+1].weight&&*(a+1)!=(*a)) *(a+1)=i; else *(a+1)=i+1; } return a; } void PutInScreen(HuffmanTree ht,int n) { int i; for(i=1;i<=2*n-1;i++) { cout<<"结点序号 "<<i; cout<<" 权重 "<<ht[i].weight<<" "<<"双亲 "<<ht[i].parent; cout<<"左孩子 "<<ht[i].lchild<<" "<<"右孩子 "<<ht[i].rchild; cout<<endl; } }
一墨千里无色 2018-05-03
  • 打赏
  • 举报
回复
怎么没有人啊
paschen 2018-05-03
  • 打赏
  • 举报
回复
你的n是按值传递的,函数中和函数外是不同的变量,你在函数中输入的n并不影响main中n

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧