数据结构实验之二叉树一:树的同构
在没有new的时候,不会自动调用析构函数

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 struct Node{
 4     char u;
 5     int left;
 6     int right;
 7     Node(){
 8         u = 'A';
 9         left = 11;
10         right = 11;
11     }
12 }node1[20 + 3], node2[20 + 3];
13 
14 bool cal(int i, int j){
15     if(node1[node1[i].left].u == node2[node2[j].left].u && node1[node1[i].right].u == node2[node2[j].right].u)return true;
16     if(node1[node1[i].left].u == node2[node2[j].right].u && node1[node1[i].right].u == node2[node2[j].left].u)return true;
17     return false;
18 }
19 bool judge(int n, int m){
20     int i, j;
21     for(i = 0; i < n; i++){
22         for(j = 0; j < m; j++){
23             if(node1[i].u == node2[j].u){
24                 if(cal(i, j)){
25                     break;
26                 }
27                 else {
28                     return false;
29                 }
30             }
31         }
32         if(j == m){
33             return false;
34         }
35     }
36     return true;
37 }
38 int main(){
39     int n, m;
40     while(~scanf("%d", &n)){
41         char str[10];
42         int l, r, i;
43 
44         for(i = 0; i < n; i++){
45             scanf("%s", str);
46                 node1[i].u = str[0];
47             scanf("%s", str);
48             if(str[0] != '-'){
49                 node1[i].left = str[0] - '0';
50             }
51             else node1[i].left = 11;
52             scanf("%s", str);
53             if(str[0] != '-'){
54                 node1[i].right = str[0] - '0';
55             }
56             else 
57                 node1[i].right = 11;
58         }
59 
60         scanf("%d", &m);
61         for(i = 0; i < m; i++){
62             scanf("%s", str);
63                 node2[i].u = str[0];
64             scanf("%s", str);
65             if(str[0] != '-'){
66                 node2[i].left = str[0] - '0';
67             }
68             else node2[i].left = 11;
69             scanf("%s", str);
70             if(str[0] != '-'){
71                 node2[i].right = str[0] - '0';
72             }
73             else node2[i].right = 11;
74         }
75         if(judge(n, m))
76             printf("Yes\n");
77         else
78             printf("No\n");
79     }
80     return 0;
81 }

数据结构实验之二叉树二:遍历二叉树

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 struct Node
 4 {
 5     char u;
 6     Node* left;
 7     Node* right;
 8     Node()
 9     {
10         left = NULL;
11         right = NULL;
12     }
13 };
14 char str[100];
15 int i;
16 Node *create(int len)
17 {
18     if(i >= len)
19         return NULL;
20     if(str[i] == ',')
21     {
22         i++;
23         return NULL;
24     }
25     Node* tmp = new Node();
26     tmp->u = str[i++];
27     tmp->left = create(len);
28     tmp->right = create(len);
29     return tmp;
30 }
31 void ceng(Node* root)
32 {
33     if(root)
34     {
35         ceng(root->left);
36         printf("%c", root->u);
37         ceng(root->right);
38     }
39 }
40 void hou(Node* root)
41 {
42     if(root)
43     {
44         hou(root->left);
45         hou(root->right);
46         printf("%c", root->u);
47     }
48 }
49 int main()
50 {
51 
52     while(scanf("%s", str) != EOF)
53     {
54         i = 0;
55         Node* root = new Node();
56         int len = strlen(str);
57         root = create(len);
58         ceng(root);
59         printf("\n");
60         hou(root);
61         printf("\n");
62     }
63     return 0;
64 }
65 
66 
67 /***************************************************
68 User name: ACM2017002信科1701黄庆祥
69 Result: Accepted
70 Take time: 0ms
71 Take Memory: 200KB
72 Submit time: 2019-09-27 21:27:21
73 ****************************************************/

 前序和中序推层序遍历,中序遍历(层序遍历通过队列实现)(非递归版)

 

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 struct Node{
 4     char u;
 5     Node* left;
 6     Node* right;
 7     Node(){
 8     left = NULL;
 9     right = NULL;
10     }
11 };
12 char str1[100], str2[100];
13 int cnt;
14 Node* build(Node* root, int L, int R)
15 {
16     int i;
17     char t = str1[cnt++];
18     for(i = L; i < R; i++){
19         if(str2[i] == t){
20             break;
21         }
22     }
23     root = new Node();
24     root->u = t;
25     if(i > L && i < R){
26         root->left = build(root->left, L, i);
27     }
28     if(i + 1 > L && i + 1 < R){
29         root->right = build(root->right, i + 1, R);
30     }
31     return root;
32 }
33 Node* hou(Node* root){
34     if(root){
35         hou(root->left);
36         hou(root->right);
37         printf("%c",root->u);
38     }
39 }
40 void ceng(Node* root){
41     queue<Node*>q;
42     if(root)
43     q.push(root);
44     while(!q.empty()){
45         Node* tmp = new Node();
46         tmp = q.front();
47         q.pop();
48         printf("%c",tmp->u);
49         if(tmp->left)
50         q.push(tmp->left);
51         if(tmp->right)
52         q.push(tmp->right);
53     }
54     return ;
55 }
56 int main(){
57     int T;
58     scanf("%d", &T);
59     while(T--){
60     cnt = 0;
61     Node* root = new Node();
62     scanf("%s", str1);
63     scanf("%s", str2);
64     int len = strlen(str2);
65     root = build(root, 0, len);
66     hou(root);
67     printf("\n");
68     ceng(root);
69     printf("\n");
70     }
71     return 0;
72 }

非递归版:

 

 1 Node* build(Node* root, int L, int R)
 2 {
 3    // cout << str1 << " " << str2 << endl;
 4     stack<Node*>q;
 5     root = new Node();
 6     root->u = str1[0];
 7     q.push(root);
 8     int i, j;
 9     for(i = 1, j = 0; i < R; i++){
10         Node* node = new Node();
11         node->u = str1[i];
12         Node *tmp = NULL;
13         while(!q.empty() && q.top()->u == str2[j]){
14             tmp = q.top();
15             q.pop();
16             j++;
17         }
18         if(tmp)
19             tmp->right = node;
20         else
21             q.top()->left = node;
22         q.push(node);
23     }
24     return root;
25 }

 

知道中序和后序求树

通过后序最后一个来判断

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 struct Node{
 4     char u;
 5     Node* left;
 6     Node* right;
 7     Node(){
 8     left = NULL;
 9     right = NULL;
10     }
11 };
12 char str1[100], str2[100];
13 int cnt;
14 int flag = 0;
15 Node* build(Node* root, int l, int r){
16     if(flag)
17         return NULL;
18     root = new Node();
19     root->u = str2[--cnt];
20     int i, k = 0;
21     for(i = l; i < r; i++){
22         if(str1[i] == root->u){
23             k = 1;
24             break;
25         }
26     }
27     if(!k)
28         return NULL;
29     if(i + 1 < r)
30         root->right = build(root->right, i + 1, r);
31     if(i > l)
32         root->left = build(root->left, l, i);
33     return root;
34 }
35 void ceng(Node* root){
36     queue<Node*>q;
37     if(root)
38     q.push(root);
39     while(!q.empty()){
40         Node* tmp = new Node();
41         tmp = q.front();
42         q.pop();
43         printf("%c",tmp->u);
44         if(tmp->left)
45         q.push(tmp->left);
46         if(tmp->right)
47         q.push(tmp->right);
48     }
49     return ;
50 }
51 
52 int main(){
53     scanf("%s", str1);
54     scanf("%s", str2);
55     int len = strlen(str1);
56     cnt = len;
57     Node* root = new Node();
58     root = build(root, 0, len);
59     ceng(root);
60     return 0;
61 }

 非递归版

 

Node* build(int L, int R)
 {
    // cout << str1 << " " << str2 << endl;
    stack<Node*>q;
    Node* root = new Node();
    Node* tmp = new Node();
    root->u = str2[R - 1];
    q.push(root);
    for(int i = R - 2, j = R - 1; i >= 0; i--){
        Node* node = new Node();
        tmp = NULL;
        node->u = str2[i];
        while(!q.empty() && str1[j] == q.top()->u){
            tmp = q.top();
            q.pop();
            j--;
        }
        if(tmp == NULL)
            q.top()->right = node;
        else
            tmp->left = node;
        q.push(node);
    }
    return root;
}