數據結構第五節(樹(下))

上次咱們說了二叉搜索樹與平衡二叉樹,此次我能說樹的另外一個應用:堆。node

什麼是堆

咱們想象一個情景,須要將任務優先級高的放在前面(優先隊列),咱們如何實現呢?ios

若是用數組作的話,插入咱們從尾部插入,尋找優先級最高的元素,以及刪除該元素後移動須要的操做都是巨大的,很明顯不是一個好方法。數組

若是咱們直接在插入時保證整個數組有序,那麼在刪除時咱們只須要刪除最後一個節點,但插入時雖然可用二分查找下降時間複雜度,但移動人須要很大的時間複雜度。數據結構

若是用鏈表作的話插入咱們從尾部插入,查找時須要的時間複雜度同數組同樣,但刪除時只須要\(O(1)\)ide

若是採用有序鏈表,咱們發現與從尾部插入毫無差別,只是查找複雜度,從刪除時轉移到了插入時。測試

這些方法都會有使人不滿意的地方,那麼有沒有一種方法,比以上方法都好?那就是用徹底二叉樹構成一個堆,(使根結點的值大於它的左右節點,這樣的堆咱們又叫它大頂堆,相反的若是小於,它就是小頂堆)。容易發現,從根節點向下畫任何一條線,都是有序的,選取下標爲1的位置爲根結點,樹中全部結點的左右兒子爲他的二倍和二倍加一,也就是除根結點外,每一個節點的下邊除以2就是它的父節點,很容易的咱們發現,這樣的數據結構在插入和刪除操做時,最壞的時間複雜度都爲\(O(logN)\)this

堆的結構表示

#define MAXDATA 99999999999
typedef struct HeapStruct* Heap;

struct HeapStruct
{
	ElementType* Elements;
	int Size;
	int Capacity;
};
//creat heap
Heap CreatHeap(int MaxSize) {
	Heap H = (Heap)malloc(sizeof(struct HeapStruct));
	H->Elements = malloc((MaxSize + 1)*sizeof(ElementType));
	H->Size = 0;
	H->Capacity = MaxSize;
	H->Elements[0] = MAXDATA;
	return H;
}

堆的插入

在進行插入操做時,直接插入堆的末尾,由於堆的性質保證路線有序,只需調整插入位置沿復節點到根結點的路徑有序便可。編碼

void Insert(Heap H,ElementType X) {
	//check the heap is full
	if (IsFull(H)) {
		printf("the heap is full.\n");
		return;
	}
	int i = ++H->Size;
	//if X big than his father,X become father
	for ( ; H->Elements[i / 2]<X; i/=2)
	{
		H->Elements[i] = H->Elements[i / 2];
	}
	H->Elements[i] = X;
}

堆的刪除

再進行刪除操做時,直接取走頭部(這個位置一定最大),並將堆的末尾賦值頭部,接着循環比較左右兒子的大小,選出一個大的與其交換,直到路徑有序後中止。spa

//delete
ElementType Delete(Heap H) {
	if (IsEmpty(H)) {
		printf("the heap is empty.\n");
		return;
	}
	ElementType MaxItem = H->Elements[1];
	ElementType temp = H->Elements[H->Size--];
	int parent, child;
	for (parent = 1; parent * 2 <= H->Size; parent = child) {
		child = parent * 2;
		//if his right son big the the left son
		if ((child != H->Size) && (H->Elements[child] < H->Elements[child + 1])) {
			child++;
		}
		//the temp is the max item int this sub tree
		if (temp >= H->Elements[child]) {
			break;
		}
		//move the child to the parent location
		else {
			H->Elements[parent] = H->Elements[child];
		}
	}
	H->Elements[parent] = temp;
	return MaxItem;
}

哈夫曼樹

什麼是哈弗曼樹呢?讓咱們想象一個情景,須要給不一樣分數段區間的學生打分A,B,C,D。每一個分數段的人數不一樣。在構造這個斷定程序時,如何讓比較的次數最少,很天然的想法哪一個分數段的人多,就先從那個條件判斷,出現越少的分數段,越晚去判斷那個條件。(貪心的思想)
定義:給定N個權值做爲N個葉子結點,構造一棵二叉樹,若該樹的帶權路徑長度達到最小,稱這樣的二叉樹爲最優二叉樹,也稱爲哈夫曼樹(Huffman Tree)。code

哈夫曼樹的構造

知道了哈夫曼樹的性質那咱們該如何寫出一個哈夫曼樹呢?上面咱們已經說過,出現機率最小的越晚判斷,很天然的,首先咱們找出2個權值最小的點,做爲樹的初始葉節點,同時計算這兩個點的權值和。接着找兩個最小的權值點,範圍也包括咱們上次生成權值和,遞歸的作合併直到全部的節點都被鏈接 。找最小的兩個點,咱們能夠用上面所講到的堆,每次出兩個最小的(Delete),再將他們的權值和插入。

typedef struct HuffmanNode* HuffmanTree;
struct HuffmanNode
{
	ElementType1 weight;
	HuffmanTree Left;
	HuffmanTree Right;
};
//creat huffmantree
HuffmanTree creat(Heap H) {
	HuffmanTree T;
	for (int i = 1; i < H->Size; i++)
	{
		T = (HuffmanTree)malloc(sizeof(struct HuffmanNode));
		T->Left = Delete(H);
		T->Right = Delete(H);
		T->weight = T->Left->weight + T->Right->weight;
		Insert(H, T);
	}
	T = Delete(H);
	return T;
}

哈夫曼樹的特色

  1. 沒有度爲1的節點
    這個很容易想到,由於在構造哈夫曼樹的過程當中,老是用左右兩個節點去合併,全部的節點都有左右兒子或者沒有兒子
  2. N個葉子節點的哈夫曼樹有2N-1個節點
    咱們以前說過對於任何一二叉樹,度爲0的節點等於度爲2的節點+1(\(n_0\)=\(n_2\)+1),根據上一條性質沒有度爲1的節點。故整個樹擁有的節點爲\(n_0\)+\(n_2\) = 2*\(n_0\) - 1,即\(2N-1\)
  3. 哈弗曼樹任何一個節點的左右指數相交換仍然是哈弗曼樹
    由於咱們在生成時並無規定左右哪一個大哪一個小,因此很天然該條成立。

哈夫曼編碼的實現

上面咱們已經說到如何構建一棵哈夫曼樹,接下來經過對一顆哈弗曼樹的遍歷,咱們就能夠實現哈弗曼編碼。遞歸的去作中序遍歷,若是遍歷到的節點既沒有左兒子也沒有右兒子,那麼能夠說明,該節點爲葉節點也就是咱們須要輸出編碼的節點,同時,在遞歸的遍歷過程當中,進入左邊就0,進入右邊就加1,下面給出代碼實現。

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<string>
#define ElementType HuffmanTree
#define ElementType1 int
#define MAXSIZE 100
#define MAXDATA -99999999
using namespace std;
typedef struct HuffmanNode* HuffmanTree;
struct HuffmanNode
{
	ElementType1 weight;
	char c ;
	HuffmanTree Left;
	HuffmanTree Right;
};

typedef struct HeapStruct* Heap;
struct HeapStruct 
{
	ElementType Elements[MAXSIZE];
	int Size;
	int Capacity;
};

//creat heap
Heap CreatHeap(int MaxSize) {
	HuffmanTree M;
	M = (HuffmanTree)malloc(sizeof(struct HuffmanNode));
	M->Left = NULL;
	M->Right = NULL;
	M->weight = MAXDATA;
	Heap H = (Heap)malloc(sizeof(struct HeapStruct));
	H->Size = 0;
	H->Capacity = MaxSize;
	H->Elements[0] = M;
	return H;
}

//check is full
bool IsFull(Heap H) {
	if (H->Capacity <= H->Size) {
		return true;
	}
	else {
		return false;
	}
}

//check is empty
bool IsEmpty(Heap H) {
	if (H->Size == 0) {
		return true;
	}
	else {
		return false;
	}
}

//insert
void Insert(Heap H, ElementType X) {
	if (IsFull(H)) {
		printf("the heap is full.\n");
		return;
	}
	
	int i = ++H->Size;
	//if X big than his father,X become father
	for (; (H->Elements[i / 2])->weight> X->weight; i /= 2)
	{
		H->Elements[i] = H->Elements[i / 2];
	}
	H->Elements[i] = X;
	return;
}

//delete
ElementType Delete(Heap H) {
	if (IsEmpty(H)) {
		printf("the heap is empty.\n");
		return NULL;
	}
	HuffmanTree MinItem = H->Elements[1];
	HuffmanTree temp = H->Elements[H->Size--];
	int parent, child;
	for (parent = 1; parent * 2 <= H->Size; parent = child) {
		child = parent * 2;
		//if his right son small than the left son
		if ((child != H->Size) && (H->Elements[child]->weight > H->Elements[child + 1]->weight)) {
			child++;
		}
		//the temp is the min item int this sub tree
		if (temp->weight <= H->Elements[child]->weight) {
			break;
		}
		//move the child to the parent location
		else {
			H->Elements[parent] = H->Elements[child];
		}
	}
	H->Elements[parent] = temp;
	return MinItem;
}

//creat huffmantree
HuffmanTree creat(Heap H) {
	HuffmanTree T;
	int end = H->Size;
	for (int i = 1; i < end; i++)
	{
		T = (HuffmanTree)malloc(sizeof(struct HuffmanNode));
		T->Left = Delete(H);
		T->Right = Delete(H);
		T->weight = T->Left->weight + T->Right->weight;
		Insert(H, T);
	}
	T = Delete(H);
	return T;
}

//中序遍歷
void InOrderTraversal(HuffmanTree T,string s) {
	//若是是空樹就不遍歷
	if (T) {
		InOrderTraversal(T->Left,s+"0");
		if (!T->Left&&!T->Right) {
			printf("%c :%s\n", T->c,s.c_str());
		}
		InOrderTraversal(T->Right,s+"1");
	}
}

int main() {
	Heap H = CreatHeap(MAXSIZE);
	int n;
	scanf_s("%d", &n);
	for (int i = 0; i < n; i++)
	{
		int temp;
		char c;
		scanf_s(" %c",&c,1);
		scanf_s("%d",&temp);
		HuffmanTree M;
		M = (HuffmanTree)malloc(sizeof(struct HuffmanNode));
		M->c = c;
		M->Left = NULL;
		M->Right = NULL;
		M->weight = temp;
		Insert(H, M);
	}
	HuffmanTree huff = creat(H);
	InOrderTraversal(huff,"");
	return 0;
}
//運行結果
5
A 5
E 10
B 15
D 30
C 40
C :0
D :10
B :110
A :1110
E :1111

課後練習題(三個小題)

05-樹7 堆中的路徑 (25point(s))

將一系列給定數字插入一個初始爲空的小頂堆H[]。隨後對任意給定的下標i,打印從H[i]到根結點的路徑。

輸入格式:
每組測試第1行包含2個正整數N和M(≤1000),分別是插入元素的個數、以及須要打印的路徑條數。下一行給出區間[-10000, 10000]內的N個要被插入一個初始爲空的小頂堆的整數。最後一行給出M個下標。

輸出格式:
對輸入中給出的每一個下標i,在一行中輸出從H[i]到根結點的路徑上的數據。數字間以1個空格分隔,行末不得有多餘空格。

輸入樣例:

5 3
46 23 26 24 10
5 4 3

輸出樣例:

24 23 10
46 23 10
26 10

題解
就是簡單模擬,構造一個最小堆後,對指定的下標位置不斷除以2,直到根節點便可。

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#define ElementType int
#define MAXDATA -1215752191
typedef struct HeapStruct* Heap;

struct HeapStruct
{
	ElementType* Elements;
	int Size;
	int Capacity;
};
//creat heap
Heap CreatHeap(int MaxSize) {
	Heap H = (Heap)malloc(sizeof(struct HeapStruct));
	H->Elements = malloc((MaxSize + 1) * sizeof(ElementType));
	H->Size = 0;
	H->Capacity = MaxSize;
	H->Elements[0] = MAXDATA;
	return H;
}
//check is full
bool IsFull(Heap H) {
	if (H->Capacity <= H->Size) {
		return true;
	}
	else {
		return false;
	}
}
//check is empty
bool IsEmpty(Heap H) {
	if (H->Size == 0) {
		return true;
	}
	else {
		return false;
	}
}
//insert
void Insert(Heap H, ElementType X) {
	if (IsFull(H)) {
		printf("the heap is full.\n");
		return;
	}
	int i = ++H->Size;
	//if X big than his father,X become father
	for (; H->Elements[i / 2] > X; i /= 2)
	{
		H->Elements[i] = H->Elements[i / 2];
	}
	H->Elements[i] = X;
}
//delete
ElementType Delete(Heap H) {
	if (IsEmpty(H)) {
		printf("the heap is empty.\n");
		return;
	}
	ElementType MaxItem = H->Elements[1];
	ElementType temp = H->Elements[H->Size--];
	int parent, child;
	for (parent = 1; parent * 2 <= H->Size; parent = child) {
		child = parent * 2;
		//if his right son small the the left son
		if ((child != H->Size) && (H->Elements[child] > H->Elements[child + 1])) {
			child++;
		}
		//the temp is the min item int this sub tree
		if (temp <= H->Elements[child]) {
			break;
		}
		//move the child to the parent location
		else {
			H->Elements[parent] = H->Elements[child];
		}
	}
	H->Elements[parent] = temp;
	return MaxItem;
}
void printL(Heap H,int n) {
	bool isF = true;
	for (int i = n; i >=1; i/=2)
	{
		if (isF) {
			printf("%d", H->Elements[i]);
			isF = false;
		}else{
			printf(" %d", H->Elements[i]);
		}
	}
	printf("\n");
}
int main() {
	int n, k;
	scanf("%d %d\n", &n, &k);
	Heap H = CreatHeap(n);
	for (int i = 0; i < n; i++)
	{
		int temp;
		scanf("%d", &temp);
		Insert(H, temp);
	}
	for (int i = 0; i < k; i++)
	{
		int p;
		scanf("%d", &p);
		printL(H, p);
	}
}

05-樹8 File Transfer (25point(s))

We have a network of computers and a list of bi-directional connections. Each of these connections allows a file transfer from one computer to another. Is it possible to send a file from any computer on the network to any other?

Input Specification:
Each input file contains one test case. For each test case, the first line contains N (2≤N≤10​^4), the total number of computers in a network. Each computer in the network is then represented by a positive integer between 1 and N. Then in the following lines, the input is given in the format:

I c1 c2

where I stands for inputting a connection between c1 and c2; or

C c1 c2

where C stands for checking if it is possible to transfer files between c1 and c2; or

S

where S stands for stopping this case.

Output Specification:
For each C case, print in one line the word "yes" or "no" if it is possible or impossible to transfer files between c1 and c2, respectively. At the end of each case, print in one line "The network is connected." if there is a path between any pair of computers; or "There are k components." where k is the number of connected components in this network.

Sample Input 1:

5
C 3 2
I 3 2
C 1 5
I 4 5
I 2 4
C 3 5
S

Sample Output 1:

no
no
yes
There are 2 components.

Sample Input 2:

5
C 3 2
I 3 2
C 1 5
I 4 5
I 2 4
C 3 5
I 1 3
C 1 5
S

Sample Output 2:

no
no
yes
yes
The network is connected.

題解:
利用並查集,由於給定了 N臺電腦以及這N臺電腦分別用1~N表示,固開一個大小爲N+1的數組com[N+1],初始將全部電腦的值設爲-1,在鏈接時,讀入兩臺電腦的編號先找到他們的根,若是根不一樣則將一臺電腦的根指向另外一臺電腦根(小的指向大的,在這以前還須要把他們共有的電腦累加,由於對於一臺指向另外一臺電腦存的必定是正數,鼓沒有指向的電腦設爲負數,他的絕對值表明了他的集合中包含幾臺電腦 )。

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
int getroot(int com[],int k) {
	while (com[k] > 0) {
		k = com[k];
	}
	return k;
}
void magre(int com[],int root1,int root2) {
	//root1 has more computer
	if (com[root1] < com[root2]) {
		com[root1] += com[root2];
		com[root2] = root1;
	}
	else {
		com[root2] += com[root1];
		com[root1] = root2;
	}
	return;
}
int main() { 
	int N,c1,c2;
	char a;
	scanf("%d\n", &N);
	int com[10001];
	memset(com, -1, sizeof(com));
	while (scanf("%c",&a)!=EOF) {
		if (a == 'I') {
			scanf("%d %d\n", &c1, &c2);
			int root1 = getroot(com, c1);
			int root2 = getroot(com, c2);
			if (root1 != root2) {
				magre(com,root1, root2);
			}
		}
		else if (a == 'C') {
			scanf("%d %d\n", &c1, &c2);
			int root1 = getroot(com, c1);
			int root2 = getroot(com, c2);
			if (root1 != root2) {
				printf("no\n");
			}
			else {
				printf("yes\n");
			}
		}
		else {
			int count = 0;
			for (int i = 1; i <= N; i++)
			{
				if (com[i] <0) {
					count++;
				}
			}
			if (count > 1) {
				printf("There are %d components.\n", count);
			}
			else {
				printf("The network is connected.\n");
			}
			break;
		}
	}
	return 0;
}

05-樹9 Huffman Codes (30point(s))

In 1953, David A. Huffman published his paper "A Method for the Construction of Minimum-Redundancy Codes", and hence printed his name in the history of computer science. As a professor who gives the final exam problem on Huffman codes, I am encountering a big problem: the Huffman codes are NOT unique. For example, given a string "aaaxuaxz", we can observe that the frequencies of the characters 'a', 'x', 'u' and 'z' are 4, 2, 1 and 1, respectively. We may either encode the symbols as {'a'=0, 'x'=10, 'u'=110, 'z'=111}, or in another way as {'a'=1, 'x'=01, 'u'=001, 'z'=000}, both compress the string into 14 bits. Another set of code can be given as {'a'=0, 'x'=11, 'u'=100, 'z'=101}, but {'a'=0, 'x'=01, 'u'=011, 'z'=001} is NOT correct since "aaaxuaxz" and "aazuaxax" can both be decoded from the code 00001011001001. The students are submitting all kinds of codes, and I need a computer program to help me determine which ones are correct and which ones are not.

Input Specification:
Each input file contains one test case. For each case, the first line gives an integer N (2≤N≤63), then followed by a line that contains all the N distinct characters and their frequencies in the following format:

c[1] f[1] c[2] f[2] ... c[N] f[N]

where c[i] is a character chosen from {'0' - '9', 'a' - 'z', 'A' - 'Z', '_'}, and f[i] is the frequency of c[i] and is an integer no more than 1000. The next line gives a positive integer M (≤1000), then followed by M student submissions. Each student submission consists of N lines, each in the format:

c[i] code[i]

where c[i] is the i-th character and code[i] is an non-empty string of no more than 63 '0's and '1's.

Output Specification:
For each test case, print in each line either "Yes" if the student's submission is correct, or "No" if not.

Note: The optimal solution is not necessarily generated by Huffman algorithm. Any prefix code with code length being optimal is considered correct.

Sample Input:

7
A 1 B 1 C 1 D 3 E 3 F 6 G 6
4
A 00000
B 00001
C 0001
D 001
E 01
F 10
G 11
A 01010
B 01011
C 0100
D 011
E 10
F 11
G 00
A 000
B 001
C 010
D 011
E 100
F 101
G 110
A 00000
B 00001
C 0001
D 001
E 00
F 10
G 11

Sample Output:

Yes
Yes
No
No

題解:
首先根據他所給出的字符出現頻率,構建一顆哈弗曼樹,計算它的wpl(最優儲存空間大小),對於他提供的每一種編碼,用string數組保存,讀入時同時計算wpl。若是他所給出的編碼wpl和咱們以前計算的不一樣,說明此並不是最優編碼。反之,開始驗證他所給出的編碼是否有前綴衝突,經過構建一棵哈夫曼樹來檢查。
代碼:

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<string>
#include<iostream>
#define ElementType HuffmanTree
#define ElementType1 int
#define MAXSIZE 100
#define MAXDATA -99999999
using namespace std;

int WPL = 0;
typedef struct HuffmanNode* HuffmanTree;
struct HuffmanNode
{
	ElementType1 weight;
	char c;
	HuffmanTree Left;
	HuffmanTree Right;
};

typedef struct HeapStruct* Heap;
struct HeapStruct
{
	ElementType Elements[MAXSIZE];
	int Size;
	int Capacity;
};

//creat heap
Heap CreatHeap(int MaxSize) {
	HuffmanTree M;
	M = (HuffmanTree)malloc(sizeof(struct HuffmanNode));
	M->Left = NULL;
	M->Right = NULL;
	M->weight = MAXDATA;
	Heap H = (Heap)malloc(sizeof(struct HeapStruct));
	H->Size = 0;
	H->Capacity = MaxSize;
	H->Elements[0] = M;
	return H;
}

//check is full
bool IsFull(Heap H) {
	if (H->Capacity <= H->Size) {
		return true;
	}
	else {
		return false;
	}
}

//check is empty
bool IsEmpty(Heap H) {
	if (H->Size == 0) {
		return true;
	}
	else {
		return false;
	}
}

//insert
void Insert(Heap H, ElementType X) {
	if (IsFull(H)) {
		printf("the heap is full.\n");
		return;
	}

	int i = ++H->Size;
	//if X big than his father,X become father
	for (; (H->Elements[i / 2])->weight > X->weight; i /= 2)
	{
		H->Elements[i] = H->Elements[i / 2];
	}
	H->Elements[i] = X;
	return;
}

//delete
ElementType Delete(Heap H) {
	if (IsEmpty(H)) {
		printf("the heap is empty.\n");
		return NULL;
	}
	HuffmanTree MinItem = H->Elements[1];
	HuffmanTree temp = H->Elements[H->Size--];
	int parent, child;
	for (parent = 1; parent * 2 <= H->Size; parent = child) {
		child = parent * 2;
		//if his right son small than the left son
		if ((child != H->Size) && (H->Elements[child]->weight > H->Elements[child + 1]->weight)) {
			child++;
		}
		//the temp is the min item int this sub tree
		if (temp->weight <= H->Elements[child]->weight) {
			break;
		}
		//move the child to the parent location
		else {
			H->Elements[parent] = H->Elements[child];
		}
	}
	H->Elements[parent] = temp;
	return MinItem;
}

//creat huffmantree
HuffmanTree creat(Heap H) {
	HuffmanTree T;
	int end = H->Size;
	for (int i = 1; i < end; i++)
	{
		T = (HuffmanTree)malloc(sizeof(struct HuffmanNode));
		T->Left = Delete(H);
		T->Right = Delete(H);
		T->weight = T->Left->weight + T->Right->weight;
		Insert(H, T);
	}
	T = Delete(H);
	return T;
}

//中序遍歷計算WPL
void InOrderTraversal(HuffmanTree T,int p) {
	//若是是空樹就不遍歷
	if (T) {
		InOrderTraversal(T->Left,p+1);
		if (!T->Left && !T->Right) {
			WPL += (T->weight * p);
		}
		InOrderTraversal(T->Right,p+1);
	}
}
HuffmanTree creathuffmannode() {
	HuffmanTree huff = (HuffmanTree)malloc(sizeof(struct HuffmanNode));
	huff->Left = NULL;
	huff->Right = NULL;
	huff->weight = 0;
	return huff;
}
//檢查編碼是否有問題
bool check(HuffmanTree M,string s) {
	HuffmanTree temp = M;
	for (int i = 0; i < s.length(); i++)
	{
		if (s[i] == '0') {
			if (temp->Left == NULL) {
				temp->Left = creathuffmannode();
				//已經到了葉節點,使其標記.
				if (i== s.length()-1) {
					temp->Left->weight = -1;
				}
			}
			else if(temp->Left->weight==-1){
				return false;
			}
			else {
				if (i == s.length() - 1) {
					return false;
				}
			}
			temp = temp->Left;
		}
		else if (s[i] == '1') {
			if (temp->Right == NULL) {
				temp->Right = creathuffmannode();
				//已經到了葉節點,使其標記.
				if (i == s.length() - 1) {
					temp->Right->weight = -1;
				}
			}
			else if (temp->Right->weight == -1) {
				return false;
			}
			else {
                if (i == s.length() - 1) {
					return false;
				}
			}
			temp = temp->Right;
		}
		else {
			return false;
		}
	}
	return true;
}
int main() {
	Heap H = CreatHeap(MAXSIZE);
	int n,t;
	int weight[100];
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		int temp;
		char c;
		scanf(" %c", &c);
		scanf("%d", &temp);
		weight[i] = temp;
		HuffmanTree M;
		M = (HuffmanTree)malloc(sizeof(struct HuffmanNode));
		M->c = c;
		M->Left = NULL;
		M->Right = NULL;
		M->weight = temp;
		Insert(H, M);
	}
	HuffmanTree huff = creat(H);
	InOrderTraversal(huff, 0);
	scanf("%d", &t);
	for (int i = 0; i < t; i++)
	{
		int sWPL = 0;
		char c;
		string s[100];
		for (int i = 0; i < n; i++)
		{
			cin >> c >> s[i];
			sWPL += s[i].length()*weight[i];
		}
		if (sWPL!=WPL) {
			printf("No\n");
		}
		else {
			bool isRight = true;
			HuffmanTree huff = creathuffmannode();
			for (int i = 0; i < n; i++)
			{
				if (!check(huff,s[i])) {
					isRight = false;
					break;
				}
			}
			if (isRight) {
				printf("Yes\n");
			}
			else {
				printf("No\n");
			}
		}
	}
	return 0;
}
相關文章
相關標籤/搜索