05-树9-Huffman Codes-编程题

05-树9-Huffman Codes-编程题

解题代码

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXN 63
#define MAXM 1000
#define MAXL 1000
typedef enum { false, true } bool;
typedef struct TNode *PtrToTNode;
typedef PtrToTNode Tree;
struct TNode {
	int W;
	Tree Left;
	Tree Right;
};
typedef Tree Data[MAXN+1];
typedef struct HNode *PtrToHeap;
struct HNode {
	Data D;
	int Size;
	int Capacity;
	int* Base;
};
typedef PtrToHeap Heap;
Heap BuildHeap(void);
Heap CreatMinHeap(int N);
void TransMinHeap(Heap H);
void Turn(Heap H, int i);
Tree BuildTree(Heap H);
Tree CreatTree(int x);
Tree DeletHeap(Heap H);
void InsertHeap(Heap H, Tree T);
int GetSWPL(Tree T, int level);
void Student(Heap H, int WPL, int N);
bool Judge(Heap H, int WPL, int N);
Tree BuildTree2(Heap H, int N);
bool InsertTNode(Heap H, Tree T, char* S, int index);
int main()
{
	Heap H = BuildHeap();
	Tree T = BuildTree(H);
	int WPL = GetSWPL(T,0);
	Student(H,WPL,H->Capacity);
	return 0;
}
Heap BuildHeap(void) {
	int N, i;
	char temp;
	scanf("%d", &N);
	Heap H = CreatMinHeap(N);
	for (i = 1; i <= N; i++) {
		temp = getchar();
		scanf("%c %d", &temp, &H->D[++H->Size]->W);
		H->Base[i - 1] = H->D[H->Size]->W;
	}
	TransMinHeap(H);
	return H;
}
Heap CreatMinHeap(int N) {
	Heap H = (Heap)malloc(sizeof(struct HNode));
	H->Capacity = N;
	H->Size = 0;
	for (int i = 0; i <= H->Capacity; i++) {
		H->D[i] = (Tree)malloc(sizeof(struct TNode));
		H->D[i]->W = 0;
		H->D[i]->Left = H->D[i]->Right = NULL;
	}
	H->D[0]->W = 0;
	H->Base = (int*)malloc(N * sizeof(int));
	for (int i = 0; i < N; i++) H->Base[i] = 0;
	return H;
}
void TransMinHeap(Heap H) {
	int i;
	for (i = (H->Size) / 2; i >= 1; i--)
		Turn(H, i);
}
void Turn(Heap H, int i) {
	int parent, child;
	Tree S = H->D[i];
	for (parent = i; parent * 2 <= H->Capacity; parent = child) {
		child = 2 * parent;
		if (child != H->Capacity&&H->D[child + 1]->W < H->D[child]->W) child++;
		if (H->D[parent]->W > H->D[child]->W) H->D[parent] = H->D[child];
		else break;
	}
	H->D[parent] = S;
}
Tree BuildTree(Heap H) {
	for (int i = 1; i < H->Capacity; i++) {
		Tree T = CreatTree(0);
		T->Left = DeletHeap(H);
		T->Right = DeletHeap(H);
		T->W = T->Left->W + T->Right->W;
		InsertHeap(H, T);
	}
	Tree T = DeletHeap(H);
	return T;
}
Tree CreatTree(int x) {
	Tree T = (Tree)malloc(sizeof(struct TNode));
	T->Left = T->Right = NULL;
	T->W = x;
	return T;
}
Tree DeletHeap(Heap H) {
	Tree T = H->D[1];
	Tree S = H->D[H->Size--];
	int parent, child;
	for (parent = 1; parent * 2 <= H->Size; parent = child) {
		child = 2 * parent;
		if (child != H->Size&&H->D[child + 1]->W < H->D[child]->W) child++;
		if(S->W>H->D[child]->W) H->D[parent] = H->D[child];
		else break;
	}
	H->D[parent] = S;
	return T;
}
void InsertHeap(Heap H, Tree T) {
	int i;
	for (i = ++H->Size; i > 1; i /= 2) {
		if (H->D[i / 2]->W > T->W) H->D[i] = H->D[i / 2];
		else break;
	}
	H->D[i] = T;
}
int GetSWPL(Tree T,int level) {
	if (!T) return 0;
	if ((T->Left==NULL) && (T->Right==NULL)) return level*T->W;
	return GetSWPL(T->Left, level + 1) + GetSWPL(T->Right, level + 1);
}
void Student(Heap H,int WPL,int N) {
	int M, flag = 1;
	scanf("%d\n", &M);
	for (int i = 0; i < M; i++) {
		if (flag) flag = 0;
		else printf("\n");
		if (Judge(H,WPL,N)) {
			printf("Yes");
		}
		else printf("No");
	}
}
bool Judge(Heap H,int WPL,int N) {
	Tree T = BuildTree2(H,N);
	int L = GetSWPL(T, 0);
	if (L != WPL || L == 0) return false;
	else return true;
}
Tree BuildTree2(Heap H,int N) {
	int flag = 1;
	Tree T = CreatTree(0);
	char S[MAXL + 1];
	for (int i = 0; i < N; i++) 
		if (!InsertTNode(H, T, S, i)) flag=0;
	if (!flag) return false;
	return T;
}
bool InsertTNode(Heap H,Tree T, char* S,int index) {
	char temp;
	temp = getchar();
	temp = getchar();
	scanf("%s", S);
	int i;
	for (i = 0; i < (int)strlen(S)-1; i++) {
		if (S[i] != '0') {
			if (!T->Left) {
				T->Left = CreatTree(0);
				T = T->Left;
			}
			else {
				if (!T->Left->W) T = T->Left;
				else return false;
			}
		}
		else {
			if (!T->Right) {
				T->Right = CreatTree(0);
				T = T->Right;
			}
			else {
				if (!T->Right->W) T = T->Right;
				else return false;
			}
		}
	}
	if (S[i]!='0') {
		if (T->Left) return false;
		else T->Left = CreatTree(H->Base[index]);
	}
	else {
		if (T->Right) return false;
		else T->Right = CreatTree(H->Base[index]);
	}
	return true;
}

测试结果

在这里插入图片描述

问题整理

1.这题做完神清气爽,卡了好长时间了,讨论一下注意要点,其实没什么不需要注意的。。。
2.在堆的系列操作中一定要记着else break;
3.malloc的区域不易debug,要typedef成数组状;
4.不要一激动在#include后边加个分号。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值