#include<iostream>
using namespace std;
//函数定义在 main 之前不用声明
typedef int TelemType;//方便替换成其他类型的数据
typedef struct BinaryTreeBode {
TelemType data;
struct BinaryTreeBode *left;
struct BinaryTreeBode *right;
}Node; // typedef 表示可以用 Node 来代替结构名来定义结构体
//创建二叉树,先中间再左右,结点数据为0表示叶子结点
Node* createBinaryTree() {
Node* p;
TelemType ch;
cin >> ch;
if (ch == 0) {
p = NULL;
}
else {
p = new Node();
p->data = ch;
p->left = createBinaryTree();
p->right = createBinaryTree();
}
return p;
}
//先序遍历
void preOrderTraverse(Node* root) {
if (root) {
cout << root->data;
preOrderTraverse(root->left);
preOrderTraverse(root->right);
}
}
//中序遍历
void inOrderTraverse(Node* root) {
if (root) {
preOrderTraverse(root->left);
cout << root->data;
preOrderTraverse(root->right);
}
}
//后序遍历
void lastOrderTraverse(Node* root) {
if (root) {
preOrderTraverse(root->left);
preOrderTraverse(root->right);
cout << root->data;
}
}
//结点总数目
int Nodenum(Node* root) {
if (root == NULL) {
return 0;
}
else {
return 1 + Nodenum(root->left) + Nodenum(root->right);
}
}
//二叉树深度
int DepthofTree(Node* root) {
if (root == NULL) {
return 0;
}
else {
return DepthofTree(root->left) >DepthofTree(root->right) ? DepthofTree(root->left) + 1 : DepthofTree(root->right) + 1;
}
}
//叶子结点数
int Leafnum(Node*root) {
if (!root) {
return 0;
}
else if((root->left==NULL&&root->right==NULL)){
return 1;
}else{
return (Leafnum(root->left) + Leafnum(root->right));
}
}
int main(){
Node *root = NULL;
root = createBinaryTree();
printf("二叉树建立成功");
cout << endl;
cout << "二叉树总节点数为:" << Nodenum(root) << endl;
cout << "二叉树深度为:" << DepthofTree(root) << endl;
cout << "二叉树叶子节点数为:" << Leafnum(root) << endl;
cout << "前序遍历结果:" << endl;
preOrderTraverse(root);
cout << endl;
cout << "中序遍历结果:" << endl;
inOrderTraverse(root);
cout << endl;
cout << "后序遍历结果:" << endl;
lastOrderTraverse(root);
cout << endl;
return 0;
}
#include<iostream>
#include<stack>
using namespace std;
typedef char TelemType;
typedef struct BinaryTreeBode {
TelemType data;
struct BinaryTreeBode *left;
struct BinaryTreeBode *right;
}Node;
Node* createBinaryTree() {
Node* p;
TelemType ch;
cin >> ch;
if (ch == '0'){
p = NULL;
}
else {
p = new Node();
p->left = createBinaryTree();
p->data = ch;
p->right = createBinaryTree();
}
return p;
}
void PreOrder(Node* root,int &num)
{
if (root)
{
if (root->left) {
PreOrder(root->left,num);
}
else if (!root->right) num++;
if (root->right&&root->right->left) PreOrder(root->right->left,num);
}
}
int main() {
int t;
cin >> t;
while (t--) {
int num = 0;
Node *root = createBinaryTree(NULL);
PreOrder(root,num);
cout << num << endl;
}
return 0;
}
#include<iostream>
#include<stack>
using namespace std;
typedef char TelemType;
typedef struct BinaryTreeBode {
TelemType data;
struct BinaryTreeBode *left;
struct BinaryTreeBode *right;
struct BinaryTreeBode *father;
}Node;
Node* createBinaryTree(Node* q) {
Node* p;
TelemType ch;
cin >> ch;
if (ch == '0'){
p = NULL;
}
else {
p = new Node();
p->left = createBinaryTree(p);
p->data = ch;
p->right = createBinaryTree(p);
p->father = q;
}
return p;
}
void Leafnum(Node*root) {
if (!root) {
}
else if ((root->left == NULL && root->right == NULL)) {
cout << root->data << " ";
}
else {
Leafnum(root->left);
Leafnum(root->right);
}
}
void Fathernum(Node*root) {
if (!root) {
}
else if ((root->left == NULL && root->right == NULL)) {
cout << root->father->data << " ";
}
else {
Fathernum(root->left);
Fathernum(root->right);
}
}
int main() {
int t;
cin >> t;
while (t--) {
Node *root = createBinaryTree(NULL);
Leafnum(root);
cout << endl;
Fathernum(root);
cout << endl;
}
return 0;
}
二叉树的数组储存:
https://blog.csdn.net/zhangwang010/article/details/73415966//完全没有用,只是格式化输出而已
数组储存
#include<iostream>
#include<stack>
using namespace std;
typedef int TelemType;
typedef struct BinaryTreeBode {
TelemType data;
struct BinaryTreeBode *left;
struct BinaryTreeBode *right;
}Node;
void preOrderTraverse(Node* root) {
if (root) {
cout << root->data;
preOrderTraverse(root->left);
preOrderTraverse(root->right);
}
}
Node* createTree(int *g,int n,int max) {
if (n > max) {
return NULL;
}
Node* p = new Node();
if (g[n] == 0) {
p = NULL;
}
else {
p->data = g[n];
p->left = createTree(g, 2 * n,max);
p->right = createTree(g, 2 * n+1,max);
}
return p;
}
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int* group = new int[n+1];
group[0] = -1;
for (int i = 1; i <= n; i++) {
cin >> group[i];
}
int max = n;
Node *root = createTree(group,1,max);
preOrderTraverse(root);
cout << endl;
}
return 0;
}
没有考虑递归的终止条件,导致问题
#include<iostream>
#include<stack>
#include<queue>
using namespace std;
typedef char TelemType;
typedef struct BinaryTreeBode {
TelemType data;
struct BinaryTreeBode *left;
struct BinaryTreeBode *right;
}Node;
Node* createBinaryTree() {
Node* p;
TelemType ch;
cin >> ch;
if (ch == '0') {
p = NULL;
}
else {
p = new Node();
p->data = ch;
p->left = createBinaryTree();
p->right = createBinaryTree();
}
return p;
}
void preOrderTraverse(Node* root) {
if (root) {
cout << root->data<<" ";
preOrderTraverse(root->left);
preOrderTraverse(root->right);
}
}
void LevelOrder(Node* t) {
queue<Node*> tq;
tq.push(t);
while (!tq.empty()) {
t = tq.front();
cout << t->data;
tq.pop();
if (t->left != NULL) {
tq.push(t->left);
}
if (t->right != NULL) {
tq.push(t->right);
}
}
}
int main() {
int t;
cin >> t;
while (t--) {
Node*root = createBinaryTree();
LevelOrder(root);
cout << endl;
}
return 0;
}
对队列、栈、串等数据结构的理解还欠深入
https://blog.csdn.net/Aray1234/article/details/75332661?locationNum=10&fps=1
huffman树:
好奇怪,程序没有任何输出,应该是基础的东西没有搞对
#include<iostream>
#include<string>
//#include<string.h>
#include<algorithm>
using namespace std;
typedef struct{
int weight;
int parent,lchild,rchild;
}HTNode;
int s1,s2;
void select(HTNode*HT,int num){
s1=HT[1].weight;
s2=HT[2].weight;
//s1,s2,parent==0
for(int i=1;i<=num;i++){
if(HT[i].parent!=0)
continue;
else{
int h=HT[i].weight;
if(h>=s1&&h>=s2)
continue;
else if(h<s1&&h<s2){
if(s1<=s2){
s2=h;
}else{
s1=h;
}
}else{
if(s1<=s2){
s2=h;
}else{
s1=h;
}
}
}
}
}
void HuffmanCoding(HTNode*HT,string HC,int *w,int n){
if(n<=1) return;
int m=2*n-1;
HT=new HTNode[m+1];
for(int i=1;i<=n;i++){
HT[i].weight=w[i-1];
cout<<w[i-1]<<endl;
HT[i].lchild=0;
HT[i].rchild=0;
HT[i].parent=0;
}
for(int i=n+1;i<=m;i++){
HT[i].weight=0;
HT[i].lchild=0;
HT[i].rchild=0;
HT[i].parent=0;
}
for(int i=n+1;i<=m;i++){
select(HT,i-1);//s1,s2,parent==0
HT[i].weight=HT[s1].weight+HT[s2].weight;
cout<<HT[i].weight<<endl;
int p1=HT[s1].weight;
int p2=HT[s2].weight;
if(p1<p2){
HT[i].lchild=p1;
HT[i].rchild=p2;
}else{
HT[i].lchild=p2;
HT[i].rchild=p1;
}
HT[s1].parent=i;
HT[s2].parent=i;
}
string cd;
for(int i=1;i<=n;i++){
int start =0;
for(int j=i,f=HT[i].parent;f!=0;j=f,f=HT[f].parent){
if(HT[j].weight==HT[f].lchild) cd[start++]='0';
else cd[start++]='1';
}
reverse(cd.begin(),cd.end());
HC.append(cd);
}
}
int main(){
int w[8]={5,29,7,8,14,23,3,11};
HTNode*HT;
string HC;
HuffmanCoding(HT,HC,w,8);
cout<<HC;
return 0;
}
终于搞定,自己写代码的时候还是有一些莫名其妙的不认真的情况,一些笔误导致编译虽然没错但是就是没有正确结果,这样太不好了
#include<iostream>
#include<string>
//#include<string.h>
#include<algorithm>
using namespace std;
typedef struct {
int weight;
int parent, lchild, rchild;
}HTNode;
int s1, s2;
void select(HTNode*HT, int num) {
int w1,w2;
int key=0;
for(int i=1;i<=num;i++){
if(key==2){
break;
}
if(HT[i].parent == 0&&key==0){
s1=i;
w1 = HT[i].weight;
key++;
continue;//没加这个,导致每一次s1,s2都相同,要是后面没有的话就不能修正
}
if(HT[i].parent == 0&&key==1){
s2=i;
w2 = HT[i].weight;
key++;
}
}
//s1,s2,parent==0
for (int i = 1; i <= num; i++) {
if (HT[i].parent != 0||i==s1||i==s2)//确保每一次都能遍历到
continue;
else {
int h = HT[i].weight;
if (h >= w1 && h >= w2)
continue;//有问题,最后
else if (h<w1&&h<w2) {
if (w1 <= w2) {
s2 = i;
w2=h;
}
else {
s1 = i;
w1=h;
}
}
else {
if (w1 <= w2) {
s2 = i;
w2=h;
}
else {
w1 = h;
s1=i;
}
}
}
}
}
void HuffmanCoding(HTNode*HT, string*HC, int *w, int n) {
if (n <= 1)
return;
int m = 2 * n - 1;
HT = new HTNode[m + 1];
for (int i = 1; i <= n; i++) {
HT[i].weight = w[i - 1];
//cout<<w[i-1]<<endl;调试的时候要设置跳过
HT[i].lchild = 0;
HT[i].rchild = 0;
HT[i].parent = 0;
}
for (int i = n + 1; i <= m; i++) {
HT[i].weight = 0;
HT[i].lchild = 0;
HT[i].rchild = 0;
HT[i].parent = 0;
}
for (int i = n + 1; i <= m; i++) {
select(HT, i - 1);//s1,s2,parent==0
HT[i].weight = HT[s1].weight + HT[s2].weight;
if (s1<s2) {
HT[i].lchild = s1;
HT[i].rchild = s2;
}
else {
HT[i].lchild = s2;
HT[i].rchild = s1;}
/*int p1 = HT[s1].weight;
int p2 = HT[s2].weight;
if (p1<p2) {
HT[i].lchild = s1;
HT[i].rchild = s2;
}
else {
HT[i].lchild = s2;
HT[i].rchild = s1;
}*/
HT[s1].parent = i;
HT[s2].parent = i;
}
for (int i = 1; i <= n; i++) {
string cd;
int start = 0;
for (int j = i, f = HT[i].parent; f != 0; j = f, f = HT[f].parent) {
if (j == HT[f].lchild) cd += '0';//cd[start++] = '0';导致cd一直为空
else cd += '1';
}
reverse(cd.begin(), cd.end());
cout<<cd<<endl;
HC[i-1]=cd;//在外面定义cd,并加上cd=NULL会报错
}
}
int main() {
int w[8] = { 5,29,7,8,14,23,3,11 };
HTNode*HT = new HTNode[9];//先分配空间再传进去 ,不然会卡崩
string HC[8];//HC是字符串数组
HuffmanCoding(HT, HC, w, 8);
for(int i=0;i<8;i++){
cout<<HC[i];
}
cout<<endl;
cout<<"01101011101111110000111010" <<endl;
return 0;
}
认为 select 函数还可以更加简单
#include<iostream>
#include<string>
#include<algorithm>
#include<string.h>
using namespace std;
const int Max = 10000;
typedef struct {
int weight;
int parent, lchild, rchild;
}HTNode;
typedef struct {
int nn;
char cc;
}Num;
int s1, s2;
void select(HTNode*HT, int num) {
int w1, w2;
w1 = Max;
w2 = Max;
for (int i = 1; i <= num; i++) {
if (HT[i].parent != 0)
continue;
else {
int h = HT[i].weight;
if (h >= w1 && h >= w2)
continue;
else if (h<w1&&h<w2) {
if (w1 <= w2) {
s2 = i;
w2 = h;
}
else {
s1 = i;
w1 = h;
}
}
else {
if (w1 <= w2) {
s2 = i;
w2 = h;
}
else {
w1 = h;
s1 = i;
}
}
}
}
}
void HuffmanCoding(HTNode*HT, string*HC, Num *w, int n) {
if (n <= 1)
return;
int m = 2 * n - 1;
HT = new HTNode[m + 1];
for (int i = 1; i <= n; i++) {
HT[i].weight = w[i - 1].nn;
HT[i].lchild = 0;
HT[i].rchild = 0;
HT[i].parent = 0;
}
for (int i = n + 1; i <= m; i++) {
HT[i].weight = 0;
HT[i].lchild = 0;
HT[i].rchild = 0;
HT[i].parent = 0;
}
for (int i = n + 1; i <= m; i++) {
select(HT, i - 1);
HT[i].weight = HT[s1].weight + HT[s2].weight;
int p1 = HT[s1].weight;
int p2 = HT[s2].weight;
if (p1 <= p2) {
HT[i].lchild = s1;
HT[i].rchild = s2;
}
else {
HT[i].lchild = s2;
HT[i].rchild = s1;
}
HT[s1].parent = i;
HT[s2].parent = i;
}
for (int i = 1; i <= n; i++) {
string cd;
int start = 0;
for (int j = i, f = HT[i].parent; f != 0; j = f, f = HT[f].parent) {
if (j == HT[f].lchild) cd += '0';
else cd += '1';
}
reverse(cd.begin(), cd.end());
HC[i - 1] = cd;
}
}
void answer(string s, HTNode*HT, string*HC, Num*w, int n) {
int b = 0;
string mm;
string ans;
int m = 0;
int max = n;
int Smax = 0;
for (int i = 0; i < max; i++) {
if (HC[i].length() > Smax) {
Smax = HC[i].length();
}
}
while (n) {
mm += s[b];
if (mm.length() > Smax) {
cout << "error" << endl;
return;
}
for (int i = 0; i<max; i++) {
string zz = HC[i];
if (zz.compare(mm)==0) {//1/*mm == HC[i]*/关键在这里
ans+= w[i].cc;
mm = "";//string mm;不能置零,只是重定义而已
n--;//n是否是常量判断错误
break;
}
}
b++;
}
cout << ans << endl;
}
int main() {
int t;
cin >> t;
while (t--) {
int l;
cin >> l;
Num*w = new Num[l];
for (int i = 0; i<l; i++) {
cin >> w[i].nn;
}
for (int i = 0; i<l; i++) {
cin >> w[i].cc;
}
HTNode*HT = new HTNode[l + 1];
string*HC = new string[l];
HuffmanCoding(HT, HC, w, l);
int k;
cin >> k;
while (k--) {
string s;
cin >> s;
answer(s, HT, HC, w, l);
}
}
return 0;
}
判断输入错的的地方有问题,自认为没有老师给的思路好,但是凭借这个发现了很多string类型的自己之前没有注意的地方,也算是值得了
#include<iostream>
#include<string>
#include<algorithm>
#include<string.h>
using namespace std;
const int Max = 10000;
typedef struct {
int weight;
int parent, lchild, rchild;
}HTNode;
typedef struct {
int nn;
char cc;
}Num;
int s1, s2;
void select(HTNode*HT, int num) {
int w1, w2;
w1 = Max;
w2 = Max;
for (int i = 1; i <= num; i++) {
if (HT[i].parent != 0)
continue;
else {
int h = HT[i].weight;
if (h >= w1 && h >= w2)
continue;
else if (h<w1&&h<w2) {
if (w1 <= w2) {
s2 = i;
w2 = h;
}
else {
s1 = i;
w1 = h;
}
}
else {
if (w1 <= w2) {
s2 = i;
w2 = h;
}
else {
w1 = h;
s1 = i;
}
}
}
}
}
void HuffmanCoding(HTNode*HT, string*HC, Num *w, int n) {
if (n <= 1)
return;
int m = 2 * n - 1;
HT = new HTNode[m + 1];
for (int i = 1; i <= n; i++) {
HT[i].weight = w[i - 1].nn;
HT[i].lchild = 0;
HT[i].rchild = 0;
HT[i].parent = 0;
}
for (int i = n + 1; i <= m; i++) {
HT[i].weight = 0;
HT[i].lchild = 0;
HT[i].rchild = 0;
HT[i].parent = 0;
}
for (int i = n + 1; i <= m; i++) {
select(HT, i - 1);
HT[i].weight = HT[s1].weight + HT[s2].weight;
int p1 = HT[s1].weight;
int p2 = HT[s2].weight;
if (p1 <= p2) {
HT[i].lchild = s1;
HT[i].rchild = s2;
}
else {
HT[i].lchild = s2;
HT[i].rchild = s1;
}
HT[s1].parent = i;
HT[s2].parent = i;
}
for (int i = 1; i <= n; i++) {
string cd;
int start = 0;
for (int j = i, f = HT[i].parent; f != 0; j = f, f = HT[f].parent) {
if (j == HT[f].lchild) cd += '0';
else cd += '1';
}
reverse(cd.begin(), cd.end());
HC[i - 1] = cd;
}
for (int i = 1; i <= 2 * n - 1; i++) {
cout << HT[i].weight << " " << HT[i].rchild << " " << HT[i].lchild << endl;
}
}
void answer(string s, HTNode*HT, string ans, Num*w, int n) {
for (int i = 1; i <= 2 * n - 1; i++) {
cout << HT[i].weight << " " << HT[i].rchild << " " << HT[i].lchild << endl;
}
cout << s << endl;
int i, k, c;
char ch;
c = 2 * n - 1;
k = 0;
for (int i = 0; i<s.length(); i++) {
ch = s[i];
if (ch == '0') {
c = HT[c].lchild;
}
else if (ch == '1') {
int numm = HT[c].weight;
c = HT[c].rchild;
}
else {
cout << "error" << endl;
return;
}
if (HT[c].lchild == 0 && HT[c].rchild == 0) {
ans[k] = w[c].cc;
k++;
c = 2 * n - 1;
}
else if (i == s.length() - 1) {
ch = '\0';//???
}
}
if (ch == '\0') {
cout << "error" << endl;
return;
}
else {
ans[k] = '\0';
}
}
int main() {
int t;
cin >> t;
while (t--) {
int l;
cin >> l;
Num*w = new Num[l];
for (int i = 0; i<l; i++) {
cin >> w[i].nn;
}
for (int i = 0; i<l; i++) {
cin >> w[i].cc;
}
HTNode*HT = new HTNode[l + 1];
string*HC = new string[l];
HuffmanCoding(HT, HC, w, l);
for (int i = 1; i <= 2 * l - 1; i++) {
cout << HT[i].weight << " " << HT[i].rchild << " " << HT[i].lchild << endl;
}
int k;
cin >> k;
while (k--) {
string s;
cin >> s;
string ans;
answer(s, HT, ans, w, l);
cout << ans << endl;
}
}
return 0;
}
为什么 HT数组没有传出来?在函数内赋值以后,难道它的值出函数以后就没了吗
#include<iostream>
#include<string>
#include<algorithm>
#include<string.h>
using namespace std;
const int Max = 10000;
typedef struct {
int weight;
int parent, lchild, rchild;
}HTNode;
typedef struct {
int nn;
char cc;
}Num;
int s1, s2;
void select(HTNode*HT, int num) {
int w1, w2;
w1 = Max;
w2 = Max;
for (int i = 1; i <= num; i++) {
if (HT[i].parent != 0)
continue;
else {
int h = HT[i].weight;
if (h >= w1 && h >= w2)
continue;
else if (h<w1&&h<w2) {
if (w1 <= w2) {
s2 = i;
w2 = h;
}
else {
s1 = i;
w1 = h;
}
}
else {
if (w1 <= w2) {
s2 = i;
w2 = h;
}
else {
w1 = h;
s1 = i;
}
}
}
}
}
HTNode* HuffmanCoding(HTNode*HT, string*HC, Num *w, int n) {
if (n <= 1)
return NULL;
int m = 2 * n - 1;
HT = new HTNode[m + 1];
for (int i = 1; i <= n; i++) {
HT[i].weight = w[i - 1].nn;
HT[i].lchild = 0;
HT[i].rchild = 0;
HT[i].parent = 0;
}
for (int i = n + 1; i <= m; i++) {
HT[i].weight = 0;
HT[i].lchild = 0;
HT[i].rchild = 0;
HT[i].parent = 0;
}
for (int i = n + 1; i <= m; i++) {
select(HT, i - 1);
HT[i].weight = HT[s1].weight + HT[s2].weight;
int p1 = HT[s1].weight;
int p2 = HT[s2].weight;
if (p1 <= p2) {
HT[i].lchild = s1;
HT[i].rchild = s2;
}
else {
HT[i].lchild = s2;
HT[i].rchild = s1;
}
HT[s1].parent = i;
HT[s2].parent = i;
}
for (int i = 1; i <= n; i++) {
string cd;
int start = 0;
for (int j = i, f = HT[i].parent; f != 0; j = f, f = HT[f].parent) {
if (j == HT[f].lchild) cd += '0';
else cd += '1';
}
reverse(cd.begin(), cd.end());
HC[i - 1] = cd;
}
for (int i = 1; i <= 2 * n - 1; i++) {
cout << HT[i].weight << " " << HT[i].rchild << " " << HT[i].lchild << endl;
}
return HT;
}
void answer(string s, HTNode*HT, char*ans, Num*w, int n) {
for (int i = 1; i <= 2 * n - 1; i++) {
cout << HT[i].weight << " " << HT[i].rchild << " " << HT[i].lchild << endl;
}
int i, k, c;
char ch;
c = 2 * n - 1;
k = 0;
for (int i = 0; i<s.length(); i++) {
ch = s[i];
if (ch == '0') {
c = HT[c].lchild;
}
else if (ch == '1') {
int numm = HT[c].weight;
c = HT[c].rchild;
}
else {
cout << "error" << endl;
return;
}
if (HT[c].lchild == 0 && HT[c].rchild == 0) {
ans[k] = w[c].cc;
cout << w[c].cc << endl;
cout << ans[k] << endl;
k++;
c = 2 * n - 1;
}
else if (i == s.length() - 1) {
ch = '\0';//???
}
}
if (ch == '\0') {
cout << "error" << endl;
return;
}
else {
ans[k] = '\0';
}
}
int main() {
int t;
cin >> t;
while (t--) {
int l;
cin >> l;
Num*w = new Num[l];
for (int i = 0; i<l; i++) {
cin >> w[i].nn;
}
for (int i = 0; i<l; i++) {
cin >> w[i].cc;
}
HTNode*HT = new HTNode[l + 1];
string*HC = new string[l];
HTNode* Ht = HuffmanCoding(HT, HC, w, l);
int k;
cin >> k;
while (k--) {
string s;
cin >> s;
char*ans = new char[l + 1];
answer(s, Ht, ans, w, l);
for (int i = 0; i<l; i++) {
cout << ans[i];
}
}
}
return 0;
}
乱七八糟,自己在崩溃的边缘徘徊
#include<iostream>
#include<string>
#include<algorithm>
#include<string.h>
using namespace std;
const int Max = 10000;
typedef struct {
int weight;
int parent, lchild, rchild;
}HTNode;
typedef struct {
int nn;
char cc;
}Num;
int s1, s2;
void select(HTNode*HT, int num) {
int w1, w2;
w1 = Max;
w2 = Max;
for (int i = 1; i <= num; i++) {
if (HT[i].parent != 0)
continue;
else {
int h = HT[i].weight;
if (h >= w1 && h >= w2)
continue;
else if (h<w1&&h<w2) {
if (w1 <= w2) {
s2 = i;
w2 = h;
}
else {
s1 = i;
w1 = h;
}
}
else {
if (w1 <= w2) {
s2 = i;
w2 = h;
}
else {
w1 = h;
s1 = i;
}
}
}
}
}
void HuffmanCoding(HTNode*HT, string*HC, Num *w, int n) {
if (n <= 1)
return;
int m = 2 * n - 1;
HT = new HTNode[m + 1];
for (int i = 1; i <= n; i++) {
HT[i].weight = w[i - 1].nn;
HT[i].lchild = 0;
HT[i].rchild = 0;
HT[i].parent = 0;
}
for (int i = n + 1; i <= m; i++) {
HT[i].weight = 0;
HT[i].lchild = 0;
HT[i].rchild = 0;
HT[i].parent = 0;
}
for (int i = n + 1; i <= m; i++) {
select(HT, i - 1);
HT[i].weight = HT[s1].weight + HT[s2].weight;
int p1 = HT[s1].weight;
int p2 = HT[s2].weight;
if (p1 <= p2) {
HT[i].lchild = s1;
HT[i].rchild = s2;
}
else {
HT[i].lchild = s2;
HT[i].rchild = s1;
}
HT[s1].parent = i;
HT[s2].parent = i;
}
for (int i = 1; i <= n; i++) {
string cd;
int start = 0;
for (int j = i, f = HT[i].parent; f != 0; j = f, f = HT[f].parent) {
if (j == HT[f].lchild) cd += '0';
else cd += '1';
}
reverse(cd.begin(), cd.end());
HC[i - 1] = cd;
}
}
void answer(string s, HTNode*HT, string*HC, Num*w, int n) {
int b = 0;
string mm;
string ans;
int m = 0;
int max = n;
int Smax = 0;
for (int i = 0; i < max; i++) {
if (HC[i].length() > Smax) {
Smax = HC[i].length();
}
}
while (1) {
if (b == s.length()) {
cout << "error" << endl;
return;
}
mm += s[b];
if (mm.length() > Smax) {
cout << "error" << endl;
return;
}
for (int i = 0; i<max; i++) {
string zz = HC[i];
if (zz.compare(mm) == 0) {//1/*mm == HC[i]*/关键在这里
ans += w[i].cc;
mm = "";//string mm;不能置零,只是重定义而已
//n--;//n是否是常量判断错误
break;
}
}
b++;
}
cout << ans << endl;
}
int main() {
int t;
cin >> t;
while (t--) {
int l;
cin >> l;
Num*w = new Num[l];
for (int i = 0; i<l; i++) {
cin >> w[i].nn;
}
for (int i = 0; i<l; i++) {
cin >> w[i].cc;
}
HTNode*HT = new HTNode[l + 1];
string*HC = new string[l];
HuffmanCoding(HT, HC, w, l);
int k;
cin >> k;
while (k--) {
string s;
cin >> s;
answer(s, HT, HC, w, l);
}
}
return 0;
}
解密的长度不确定
#include<iostream>
#include<string>
#include<algorithm>
#include<string.h>
using namespace std;
const int Max = 10000;
typedef struct {
int weight;
int parent, lchild, rchild;
}HTNode;
typedef struct {
int nn;
char cc;
}Num;
int s1, s2;
void select(HTNode*HT, int num) {
int w1, w2;
w1 = Max;
w2 = Max;
for (int i = 1; i <= num; i++) {
if (HT[i].parent != 0)
continue;
else {
int h = HT[i].weight;
if (h >= w1 && h >= w2)
continue;
else if (h<w1&&h<w2) {
if (w1 <= w2) {
s2 = i;
w2 = h;
}
else {
s1 = i;
w1 = h;
}
}
else {
if (w1 <= w2) {
s2 = i;
w2 = h;
}
else {
w1 = h;
s1 = i;
}
}
}
}
}
HTNode* HuffmanCoding(HTNode*HT, string*HC, Num *w, int n) {
if (n <= 1)
return NULL;
int m = 2 * n - 1;
HT = new HTNode[m + 1];
for (int i = 1; i <= n; i++) {
HT[i].weight = w[i - 1].nn;
HT[i].lchild = 0;
HT[i].rchild = 0;
HT[i].parent = 0;
}
for (int i = n + 1; i <= m; i++) {
HT[i].weight = 0;
HT[i].lchild = 0;
HT[i].rchild = 0;
HT[i].parent = 0;
}
for (int i = n + 1; i <= m; i++) {
select(HT, i - 1);
HT[i].weight = HT[s1].weight + HT[s2].weight;
int p1 = HT[s1].weight;
int p2 = HT[s2].weight;
if (p1 <= p2) {
HT[i].lchild = s1;
HT[i].rchild = s2;
}
else {
HT[i].lchild = s2;
HT[i].rchild = s1;
}
HT[s1].parent = i;
HT[s2].parent = i;
}
for (int i = 1; i <= n; i++) {
string cd;
int start = 0;
for (int j = i, f = HT[i].parent; f != 0; j = f, f = HT[f].parent) {
if (j == HT[f].lchild) cd += '0';
else cd += '1';
}
reverse(cd.begin(), cd.end());
HC[i - 1] = cd;
}
return HT;
}
void answer(string s, HTNode*HT, string*HC, Num*w, int n) {
string ans = "";
char cm;
int m = 2 * n - 1;
for (int i = 0; i < s.length(); i++) {
cm = s[i];
if (cm == '0') {
m = HT[m].lchild;
}else if (cm == '1') {
m = HT[m].rchild;
}
else {
cout << "error" << endl;
return;
}
if (HT[m].lchild == 0 && HT[m].rchild == 0) {
ans += w[m-1].cc;
m = 2 * n - 1;
}
else {
cm = '\0';
}
}
if (cm == '\0') {
cout << "error" << endl;
return;
}
else {
cout << ans << endl;
}
}
int main() {
int t;
cin >> t;
while (t--) {
int l;
cin >> l;
Num*w = new Num[l];
for (int i = 0; i<l; i++) {
cin >> w[i].nn;
}
for (int i = 0; i<l; i++) {
cin >> w[i].cc;
}
HTNode*HT = new HTNode[l + 1];
string*HC = new string[l];
HTNode*Ht=HuffmanCoding(HT, HC, w, l);
int k;
cin >> k;
while (k--) {
string s;
cin >> s;
answer(s, Ht, HC, w, l);
}
}
return 0;
}
奇奇怪怪就好了?并不是, w数组从0开始定义,你以为是从 1开始。
#include<iostream>
#include<string>
#include<algorithm>
#include<string.h>
using namespace std;
const int Max = 10000;
typedef char TelemType;
typedef struct BinaryTreeBode {
TelemType data;
struct BinaryTreeBode *left;
struct BinaryTreeBode *right;
}Node;
typedef struct {
int weight;
int parent, lchild, rchild;
}HTNode;
int s1, s2;
void select(HTNode*HT, int num) {
int w1, w2;
w1 = Max;
w2 = Max;
for (int i = 1; i <= num; i++) {
if (HT[i].parent != 0)
continue;
else {
int h = HT[i].weight;
if (h >= w1 && h >= w2)
continue;
else if (h<w1&&h<w2) {
if (w1 <= w2) {
s2 = i;
w2 = h;
}
else {
s1 = i;
w1 = h;
}
}
else {
if (w1 <= w2) {
s2 = i;
w2 = h;
}
else {
w1 = h;
s1 = i;
}
}
}
}
}
HTNode* HuffmanCoding(HTNode*HT, string*HC, int *w, int n) {
if (n <= 1)
return NULL;
int m = 2 * n - 1;
HT = new HTNode[m + 1];
for (int i = 1; i <= n; i++) {
HT[i].weight = w[i - 1];
HT[i].lchild = 0;
HT[i].rchild = 0;
HT[i].parent = 0;
}
for (int i = n + 1; i <= m; i++) {
HT[i].weight = 0;
HT[i].lchild = 0;
HT[i].rchild = 0;
HT[i].parent = 0;
}
for (int i = n + 1; i <= m; i++) {
select(HT, i - 1);
HT[i].weight = HT[s1].weight + HT[s2].weight;
int p1 = HT[s1].weight;
int p2 = HT[s2].weight;
if (p1 <= p2) {
HT[i].lchild = s1;
HT[i].rchild = s2;
}
else {
HT[i].lchild = s2;
HT[i].rchild = s1;
}
HT[s1].parent = i;
HT[s2].parent = i;
}
for (int i = 1; i <= n; i++) {
string cd;
int start = 0;
for (int j = i, f = HT[i].parent; f != 0; j = f, f = HT[f].parent) {
if (j == HT[f].lchild) cd += '0';
else cd += '1';
}
reverse(cd.begin(), cd.end());
// cout <<w[i-1]<<"-"<<cd << endl;
HC[i - 1] = cd;
}
}
Node* createBinaryTree() {
Node* p;
TelemType ch;
cin >> ch;
if (ch == '0') {
p = NULL;
}
else {
p = new Node();
p->data = ch;
p->left = createBinaryTree();
p->right = createBinaryTree();
}
return p;
}
void preOrderTraverse(Node* root) {
if (root) {
cout << root->data;
preOrderTraverse(root->left);
preOrderTraverse(root->right);
}
}
/*
void sum(HTNode*HT, int*w, int n) {
string ans = "";
char cm;
int m = 2 * n - 1;
for (int i = 0; i < s.length(); i++) {
cm = s[i];
if (cm == '0') {
m = HT[m].lchild;
}
else if (cm == '1') {
m = HT[m].rchild;
}
else {
cout << "error" << endl;
return;
}
if (HT[m].lchild == 0 && HT[m].rchild == 0) {
ans += w[m - 1].cc;
m = 2 * n - 1;
}
else {
cm = '\0';
}
}
if (cm == '\0') {
cout << "error" << endl;
return;
}
else {
cout << ans << endl;
}
}*/
int Leafnum(Node*root) {//同为先序查找叶子
if (!root) {
return 0;
}
else if ((root->left == NULL && root->right == NULL)) {
return 1;
}
else {
return (Leafnum(root->left) + Leafnum(root->right));
}
}
int Leafsum(Node*root,int*p, int*q,int*wf) {//同为先序查找叶子
if (!root) {
return 0;
}
else if ((root->left == NULL && root->right == NULL)) {
wf[*q] = *p;
*p = 0;
cout<<root->data<<" ";
return 1;
}
else {
*p++;
return (Leafsum(root->left,p,q,wf) + Leafsum(root->right, p, q, wf));
}
}
int main() {
int t;
cin >> t;
while (t--) {
Node *root = createBinaryTree();
preOrderTraverse(root);
cout << endl;
int n;
cin >> n;
int*w = new int[n];
for (int i = 0; i < n; i++) {
cin >> w[i];
}
HTNode*HT = new HTNode[n + 1];
string*HC = new string[n];
HTNode*Ht=HuffmanCoding(HT, HC, w, n);
int lfm=Leafnum(root);
int*wf = new int[lfm];
memset(wf, 0, lfm);
int pl = 0,ql = 0;
int*p = &pl;
int*q = &ql;
Leafsum(root, p,q,wf);
int sum = 0;
for (int i = 0; i < lfm; i++) {
sum += wf[i] * w[i];
}
cout << sum << endl;
}
return 0;
}
递归传递指针计数,算了吧还是
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
typedef char TelemType;
typedef struct BinaryTreeBode {
TelemType data;
struct BinaryTreeBode *left;
struct BinaryTreeBode *right;
}Node;
Node* createBinaryTree() {
Node* p;
TelemType ch;
cin >> ch;
if (ch == '0') {
p = NULL;
}
else {
p = new Node();
p->data = ch;
p->left = createBinaryTree();
p->right = createBinaryTree();
}
return p;
}
void preOrderTraverse(Node* root) {
if (root) {
cout << root->data;
preOrderTraverse(root->left);
preOrderTraverse(root->right);
}
}
int Leafnum(Node*root,string* aa) {
if (!root) {
return 0;
}
else if ((root->left == NULL && root->right == NULL)) {
*aa += root->data;
return 1;
}
else {
return (Leafnum(root->left,aa) + Leafnum(root->right,aa));
}
}
void printLeavesDepth(Node*t, int*wf ,const string aa,size_t depth = 0 ){
if (t == NULL) return;
if (t->left == NULL && t->right == NULL)
{
for (int i = 0; i < aa.length(); i++) {
if (t->data == aa[i]) {
wf[i] = depth;
}
}
}
else
{
printLeavesDepth(t->left,wf,aa, depth + 1);
printLeavesDepth(t->right,wf,aa, depth + 1);
}
}
int main() {
int t;
cin >> t;
while (t--) {
Node *root = createBinaryTree();
preOrderTraverse(root);
cout << endl;
int n;
cin >> n;
int*w = new int[n];
for (int i = 0; i < n; i++) {
cin >> w[i];
}
string aa;
string*dd = &aa;
int lfm = Leafnum(root,dd);
int*wf = new int[lfm];
printLeavesDepth(root, wf,aa,0);
int sum = 0;
for (int i = 0; i < lfm; i++) {
sum += wf[i] * w[i];
}
cout << sum << endl;
}
return 0;
}
样例过了,但是答案错误,我感觉是理解题意的问题
第三题比较水,但是还是挺有意思的
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
typedef char TelemType;
typedef struct BinaryTreeBode {
TelemType data;
struct BinaryTreeBode *left;
struct BinaryTreeBode *right;
}Node;
Node* createBinaryTree() {
Node* p;
TelemType ch;
cin >> ch;
if (ch == '0') {
p = NULL;
}
else {
p = new Node();
p->data = ch;
p->left = createBinaryTree();
p->right = createBinaryTree();
}
return p;
}
int Leafnum(Node*root,string* aa) {
if (!root) {
return 0;
}
else if ((root->left == NULL && root->right == NULL)) {
*aa += root->data;
return 1;
}
else {
return (Leafnum(root->left,aa) + Leafnum(root->right,aa));
}
}
void LeavesDepth(Node*t, int*wf ,const string aa,size_t depth = 0 ){
if (t == NULL) return;
if (t->left == NULL && t->right == NULL)
{
for (int i = 0; i < aa.length(); i++) {
if (t->data == aa[i]) {
wf[i] = depth;
}
}
}
else
{
LeavesDepth(t->left,wf,aa, depth + 1);
LeavesDepth(t->right,wf,aa, depth + 1);
}
}
int main() {
int t;
cin >> t;
while (t--) {
Node *root = createBinaryTree();
int n;
cin >> n;
int*w = new int[n];
for (int i = 0; i < n; i++) {
cin >> w[i];
}
string aa;
string*dd = &aa;
int lfm = Leafnum(root,dd);
int*wf = new int[lfm];
LeavesDepth(root, wf,aa,0);
int sum = 0;
for (int i = 0; i < lfm; i++) {
sum += wf[i] * w[i];
}
cout << sum << endl;
}
return 0;
}
第四题按照第三题的思路很快就能做出来
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
typedef char TelemType;
typedef struct BinaryTreeBode {
TelemType data;
struct BinaryTreeBode *left;
struct BinaryTreeBode *right;
}Node;
Node* createBinaryTree() {
Node* p;
TelemType ch;
cin >> ch;
if (ch == '0') {
p = NULL;
}
else {
p = new Node();
p->data = ch;
p->left = createBinaryTree();
p->right = createBinaryTree();
}
return p;
}
int Leafnum(Node*root,string* aa) {
if (!root) {
return 0;
}
else if ((root->left == NULL && root->right == NULL)) {
*aa += root->data;
return 1;
}
else {
return (Leafnum(root->left,aa) + Leafnum(root->right,aa));
}
}
void LeavesDepth(Node*t, int*wf ,const string aa,size_t depth = 0 ){
if (t == NULL) return;
if (t->left == NULL && t->right == NULL)
{
for (int i = 0; i < aa.length(); i++) {
if (t->data == aa[i]) {
wf[i] = depth;
}
}
}
else
{
LeavesDepth(t->left,wf,aa, depth + 1);
LeavesDepth(t->right,wf,aa, depth + 1);
}
}
int main() {
int t;
cin >> t;
while (t--) {
Node *root = createBinaryTree();
string aa;
string*dd = &aa;
int lfm = Leafnum(root,dd);
int*wf = new int[lfm];
LeavesDepth(root, wf,aa,0);
int max = 0;
for (int i = 0; i < lfm; i++) {
if (max < wf[i]) {
max = wf[i];
}
}
cout << max+1 << endl;
}
return 0;
}
第五题也还行,对递归和引用的掌握又深了一点
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
typedef char TelemType;
typedef struct BinaryTreeBode {
TelemType data;
struct BinaryTreeBode *left;
struct BinaryTreeBode *right;
int num;
}Node;
Node* createBinaryTree() {
Node* p;
TelemType ch;
cin >> ch;
if (ch == '0') {
p = NULL;
}
else {
p = new Node();
p->data = ch;
p->left = createBinaryTree();
p->right = createBinaryTree();
}
return p;
}
int Nodenum(Node*root,int*h,int &q) {//求结点数
if (!root) {
return 0;
}
else if ((root->left == NULL && root->right == NULL)) {
return 1;
}
else {
return (Nodenum(root->left,h,q) + Nodenum(root->right,h,q)+root->num);
}
}
bool sumload(Node *t, int num,int&sum)
{
if (t == NULL)
return false;
if (t->num == num || sumload(t->left, num,sum) || sumload(t->right, num,sum))
{
sum+= t->num; //路径上的结点标识打印出来
return true;
}
return false;
}
void preOrderTraverse(Node* root,int*w,int&p) {//为什么用指针会地址混乱?
if (root) {
root->num = w[p];
p++;
preOrderTraverse(root->left,w,p);
preOrderTraverse(root->right,w,p);
}
}
int main() {
int t;
cin >> t;
while (t--) {
Node *root = createBinaryTree();
//int lfm = Nodenum(root);
//cout << lfm;
int n;
cin >> n;
int*w = new int[n];//结构体不等于数组、指针,传入函数要返回。string怎么样?
for (int i = 0; i < n; i++) {
cin >> w[i];
}
int p = 0;
preOrderTraverse(root, w, p);
int*nsum = new int[n];
for (int i = 0; i < n; i++) {
int s = 0;
sumload(root, w[i], s);
nsum[i] = s;
}
int max = 0;
for (int i = 0; i < n; i++) {
if (max < nsum[i]) {
max = nsum[i];
}
}
cout << max << endl;
}
return 0;
}