求助 哈夫曼树 过不了编译
  • 板块学术版
  • 楼主Drind
  • 当前回复2
  • 已保存回复2
  • 发布时间2021/12/24 22:18
  • 上次更新2023/10/28 13:44:56
查看原帖
求助 哈夫曼树 过不了编译
305854
Drind楼主2021/12/24 22:18
#include<bits/stdc++.h>
using namespace std;

typedef struct
{
	int parent;
	int lchild;
	int rchild;
	double weight;
	char ch;
}node;

typedef struct
{
	int bit[100001];
	int start;
}code;

node hnode[100001];
code hcode[100001];

void huffman_tree(node hnode[100001],int n)
{
	
	double t1,t2;
	int t1t,t2t;
	for(int i=0;i<2*n-1;i++)
	{
		hnode[i].weight=0;
		hnode[i].lchild=-1;
		hnode[i].rchild=-1;
		hnode[i].parent=-1;
	}
	for(int i=0;i<n;i++)
	{
		cout<<"Input the value and the weight of node "<<i<<endl;
		cin>>hnode[i].ch;
		cin>>hnode[i].weight;
	}
	for(int i=0;i<n-1;i++)
	{
		t1=t2=1e9;
		t1t=t2t=-1;
		for(int j=0;j<n+i;j++)
		{
			if(hnode[j].weight<t1&&hnode[j].parent==-1)
			{
				t2=t1;
				t2t=t1t;
				t1=hnode[j].weight;
				t1t=j;
			}
			else if(hnode[j].weight<t2&&hnode[j].parent==-1)
			{
				t2=hnode[j].weight;
				t2t=j;
			}
		}
		hnode[t1t].parent=n+i;
		hnode[t2t].parent=n+i;
		hnode[n+i].lchild=t1t;
		hnode[n+i].rchild=t2t;
		hnode[n+i].weight=t1+t2;
	}
}

void huffman_code(code hcode[100001],int n)
{
	code temp;
	int c,p;
	for(int i=0;i<n;i++)
	{
		temp.start=n-1;
		c=i;
		p=hnode[c].parent;
		while(p!=-1)
		{
			if(hnode[p].lchild==c)
				temp.bit[temp.start]=0;
			else
				temp.bit[temp.start]=1;
			temp.start--;
			c=p;
			p=hnode[c].parent;
		}
		for(int j=0;j<n;j++)
		{
			hcode[i].bit[j]=temp.bit[i];
		}
		hcode[i].start=temp.start;
	}
}

int main()
{
	int n;
	cin>>n;
	huffman_tree(hnode,n);
	huffman_code(hcode,n);
	for(int i=0;i<n;i++)
	{
		cout<<hnode[i].ch<<"'s huffman code is";
		for(int j=hcode[i].start;j<n;j++)
			cout<<hcode[i].bit[j];
		cout<<endl;
	}
}

错误信息:

Error: value of 000000095072b986 too large for field of 4 bytes at 00000000000005de

IDE:Dev-C++ 5.10

2021/12/24 22:18
加载中...