无栈非递归求赫夫曼编码算法

	//-----------无栈非递归遍历赫夫曼树,求赫夫曼编码----------------
	HC=(HuffmanCode)malloc((n+1)*sizeof(char *));
	p=m;
	cdlen=0;
	for(i=1;i<=m;++i)
		HT[i].weight=0;
	while(p)
	{	if(HT[p].weight==0)
		{	HT[p].weight==1;
			if(HT[p].lchild!=0)
			{	p=HT[p].lchild;
				cd[cdlen++]="0";
			}
			else if(HT[p].rchild==0)
			{	HC[p]=(char *)malloc((cdlen+1)*sizeof(char));
				cd[cdlen]="\0";
				strcpy(HC[p],cd);
			}
		}
		else if(HT[p].weight==1)
		{	HT[p].weight=2;
			if(HT[p].rchild!=0)
			{	p=HT[p].rchild;
				cd[cdlen++]="1";
			}
		}
		else
		{	HT[p].weight=0;
			p=HT[p].parent;
			--cdlen;
		}
	}

你可能感兴趣的:(Algorithm,数据结构)