数据结构-树-定义,哈夫曼

//树和二叉树
//孩子链表表示法
const int MAXND=20;
typedef struct bnode
{
	int child;
	struct bnode *next;
}node,*childlink;
typedef struct
{
	DataType data;
	//方便找双亲可以加int parent;
	childlink hp;
}headnode;
headnode link[MAXND];


//孩子兄弟链表表示法
typedef struct tnode
{
	DataType data;
	struct tnode *son,*brother;
} *Tree;

//双亲表示法
const int size=10;
typedef struct
{
	DataType data;
	int parent;
}Node;
Node slist[size];


//哈夫曼树
//定义:二叉树顺序存储,四个域组成
const int n=10;
typedef struct
{
	float w;
	int parent,lchild,rchild;
}node;
typedef node hftree [2*n-1];
//算法
void Huffman (int k,float w[],hftree T)
{
	int i,j,x,y;
	float m,n;
	for (i=0;i<2*k-1;i++)
	{
		T[i].parent=-1;
		T[i].lchild=-1;
		T[i].rchild=-1;
		if (i<k)
			T[i].w=w[i];
		else
			T[i].w=0;
	}
	for (i=0;i<k-1;i++)
	{
		x=0;
		y=0;
		m=MAX;
		n=MAX;
		for (j=0;j<k+i;j++)
			if((T[j].w<m)&&(T[j].parent==-1))
			{
				n=m;
				y=x;
				m=T[j].w;
				x=j;
			}
			else if ((T[j].w<n)&&(T[j].parent==-1))
			{
				n=T[j].w;
				y=j;
			}
		T[x].parent=k+i;T[y].parent=k+i;
		T[k+i].w=m+n;
		T[k+i].lchild=x;
		T[k+i].rchild=y;
	}
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值