Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tree #6

Open
ColderCoder opened this issue May 21, 2019 · 2 comments
Open

Tree #6

ColderCoder opened this issue May 21, 2019 · 2 comments

Comments

@ColderCoder
Copy link
Owner

http://wangmengds.ml/tree/

@wangmengsd
Copy link
Collaborator

以下是汪铮沁同学的霍夫曼树代码,解码部分会报错,谁来帮她解决一下?

#include
#include
using namespace std;
struct Node {
char str;
int data;
char num;
char code[10];
Node* father;
Node* left;
Node* right;
};
struct cmp1 {
bool operator()(Node* x, Node* y){
return x->data > y->data;
}
};
priority_queue<Node*, vector<Node*>, cmp1> q1;
int lth = 0;
int point = 0;
char sentense[100] = {};
void print(char array[], Node* root){
Node* temp = root;
if ((temp->left == NULL) && (temp->right == NULL)){
sentense[lth] = temp->str;
lth++;
return;
}
else if (array[point] == '0'){
temp = temp->left;
point++;
print(array, temp);
}
else if (array[point] == '1'){
temp = temp->right;
point++;
print(array, temp);
}
}
int main()
{
char array[] = { "choose me let me make you happy" };//array[] represent the input statement
char array0[] = { " abcdefghijklmnopqrstuvwxyz" };//array0[] represent the ' ' and 26 alphabet
int f[27] = {};//f[] represent the frequency of array0[]
int s = 0;//s represent the length of array[]
for (; array[s] != '\0'; s++);
for (int i = 0; i < s; i++){ //i represent the number of circle
for (int n = 0; n < 27; n++){ //n represent the nth
if (array0[n] == array[i]) f[n]++;
}
}
Node* a[100];//a[] represent the old Node*
for (int n = 0; n < 27; n++){
a[n] = new Node;
a[n]->data = f[n];
for (int i = 0; i < 10; i++) a[n]->code[i] = ' ';
if (a[n]->data != 0) a[n]->str = array0[n];
else a[n]->str = '!';
a[n]->num = '-1';
a[n]->left = a[n]->right = a[n]->father = NULL;
if (f[n] != 0) cout << array0[n] << "-" << f[n] << endl;
}
for (int n = 27; n < 100; n++){
a[n] = new Node;
a[n]->data = 0;
for (int i = 0; i < 10; i++) a[n]->code[i] = ' ';
a[n]->str = '!';
a[n]->num = '-1';
a[n]->left = a[n]->right = a[n]->father = NULL;
}
for (int n = 0; n < 27; n++){
if (a[n]->data != 0) q1.push(a[n]);
}
int t = 27;
while (q1.size() != 1){
Node* a0;
Node* b0;
a0 = q1.top();
q1.pop();
b0 = q1.top();
q1.pop();
a[t]->data = a0->data + b0->data;
a[t]->left = a0;
a[t]->right = b0;
a0->father = b0->father = a[t];
a0->num = '0';
b0->num = '1';
q1.push(a[t]);
t++;
}
t = t - 1;
int c = 0;
for (; c < 27; c++){
Node* ar = a[c];
if (ar->str != '!') {
int n = 0;
while (ar->father != NULL) {
a[c]->code[n] = ar->num;
ar = ar->father;
n++;
}
cout << a[c]->str << "'s huffman code is : ";
for (int i = 9; i>=0; i--) {
if(a[c]->code[i] != ' ') cout << a[c]->code[i];
}
cout << endl;
}
}
cout << "The code of sentence is:" << endl;
for (int d = 0; d < s; d++){
for (int n = 0; n < 27; n++){
if (array[d] == a[n]->str){
for (int i = 9; i >= 0; i--){
if (a[n]->code[i] != ' ') cout << a[n]->code[i];
}
}
}
}
cout << endl;
char array2[] = {01110};
//char array2[] = {01110101111011101100111110001011100100001111000100010111000101010011111110001101101100100010111010110011000110};
for (; array[point] != '\0';) print(array2, a[t]);
cout << sentense;
system("pause");
}

@ColderCoder
Copy link
Owner Author

@wangmengsd
以下是汪铮沁同学的霍夫曼树代码,解码部分会报错,谁来帮她解决一下?

首先需要

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

下面这段代码循环上限写错了导致z丢了,一错到底
好在内置的那句话没有包含"z"...

for (int n = 0; n < 27; n++) 
{
	a[n] = new Node;
	a[n]->data = f[n];
	for (int i = 0; i < 10; i++) a[n]->code[i] = ' ';
	if (a[n]->data != 0) a[n]->str = array0[n];
	else a[n]->str = '!';
	a[n]->num = '-1';
	a[n]->left = a[n]->right = a[n]->father = NULL;
	if (f[n] != 0) cout << array0[n] << "-" << f[n] << endl;
}

for (int n = 27; n < 100; n++) 
{
	a[n] = new Node;
	a[n]->data = 0;
	for (int i = 0; i < 10; i++) a[n]->code[i] = ' ';
	a[n]->str = '!';
	a[n]->num = '-1';
	a[n]->left = a[n]->right = a[n]->father = NULL;
}

而且最后还没释放a指向的内存…


char array2[] = {01110};

不加引号真的令人窒息……
01110是一个八进制数,二进制表示为001001001000,
因为是char,截断末八位二进制位后对应为'H'的ASCII码


加上引号应该有可能输出正确结果了
print函数建议汪同学自己检查
PS:请取合理的变量名,请写正常的注释。
避免不必要的操作(总感觉东一个变量西一个变量,参数传来传去)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants