haffman编码正确性判定

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#include <iostream>
#include <queue>
#include <algorithm>
#include <vector>

typedef char Bit;
typedef char* Bits;
typedef int Weight;
typedef char DataType;
typedef struct _BinTrNode BinTrNode,*pBinTrNode;
enum TAG{
	LEAF = 0,
	NON_LEAF = 1,
	Null = 'A'-1,
};
struct _BinTrNode{
	int tag;
	union UReg{
		DataType Ch;
	}uReg;
	Weight W;
	struct _BinTrNode* LftChild;
	struct _BinTrNode* RghtChild;
}; 

typedef pBinTrNode BinTr;

class CmpPred{
	public:
		bool operator()(const pBinTrNode &P,const pBinTrNode &Q)const
		{
			return P->W <= Q->W;
		}
};

static BinTr _Creat_()
{
	std::priority_queue<pBinTrNode,std::vector<pBinTrNode>,CmpPred> prQue; 
	int N;
	std::cin >> N;
	
	Weight W;
	DataType Ch;
	
	while(N--)
	{
		std::cin >> Ch >> W;
		pBinTrNode p = (pBinTrNode)malloc(sizeof(BinTrNode));
		p->W = W;
		p->uReg.Ch = Ch;
		
		p->LftChild = NULL;
		p->RghtChild = NULL;
		p->tag = TAG::LEAF;
		
		prQue.push(p); 
	}
	while(prQue.size() > 1)
	{
		pBinTrNode p = (pBinTrNode)malloc(sizeof(BinTrNode));
		
		p->LftChild = NULL;
		p->RghtChild = NULL;
		p->tag = TAG::NON_LEAF;
		p->uReg.Ch = Null;
		
		p->LftChild = prQue.top();prQue.pop();
		p->RghtChild = prQue.top();prQue.pop();
		
		p->W = p->LftChild->W + p->RghtChild->W;
		
		prQue.push(p);
	}
	return prQue.top();
}

static int _WPL(const BinTr t,int Depth);

static int WPL(const BinTr t)
{
	if(t == NULL)return -1;
	return _WPL(t,0);
}
static int _WPL(const BinTr t,int Depth)
{
	if(!t)return 0;
	if(t->LftChild == NULL && t->RghtChild == NULL)
		return Depth*(t->W);
	return _WPL(t->LftChild,Depth+1)+_WPL(t->RghtChild,Depth+1);
}
//N最大时:哈夫曼树完全偏斜 有N-1个外部路径中间节点 
//因此长度最大为N-1 
								  
static void Destr(BinTr t)
{
	if(t)
	{
		Destr(t->LftChild);
		Destr(t->RghtChild);
		free(t);
	}
}
static bool Judge(int minWPL,const Bits BitsCol[],const int N)
{
	BinTr t = (BinTr)malloc(sizeof(BinTrNode));
	t->LftChild = NULL;t->RghtChild = NULL;
	t->tag = TAG::NON_LEAF;
	t->uReg.Ch = TAG::Null;
	t->W = 0;

	for(int i = 0;i < N;++i)
	{
		Bits p = BitsCol[i];
		if(!strlen(p))
		{
			Destr(t);
			return false;
		}
		BinTr q = t;
		while((*p)!='\0')
		{
			if((*p) == '1')
			{
				if(q->RghtChild == NULL)
				{
					q->RghtChild = (BinTr)malloc(sizeof(BinTrNode));
					q = q->RghtChild;
					q->LftChild = NULL;
					q->RghtChild = NULL;
					q->tag = TAG::NON_LEAF;
				}else{
					if(q->RghtChild->tag == TAG::NON_LEAF)
					{
						q = q->RghtChild;
					}else{
						Destr(t);
						return true;
					} 
					
				}
			}else{
				if(q->LftChild == NULL)
				{
					q->LftChild = (BinTr)malloc(sizeof(BinTrNode));
					q = q->LftChild;
					q->LftChild = NULL;
					q->LftChild = NULL;
					q->tag = TAG::NON_LEAF;
				}else{
					if(q->LftChild->tag == TAG::NON_LEAF)
					{
						q = q->LftChild;
					}else{
						Destr(t);
						return true;
					} 
				}
			}
			++p;
		}
		if(q->LftChild == NULL && q->RghtChild == NULL)q->tag = TAG::LEAF;
		else{
			Destr(t);
			return false;
		}
		
	}
	Destr(t);
	return true;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XNB's Not a Beginner

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值