计算机考研机试指南(七)——二叉树和二叉排序树

机试指南 cha 3 二叉树

已知前序和中序求后序遍历

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <algorithm>
 4 #include <queue>
 5 #include <stack>
 6 #include <math.h>
 7 #include <string>
 8 #include <string.h>
 9 #include <stdlib.h>
10 #include <stack>
11 using namespace std;
12 
13 char s1[100],s2[100];
14 // 前序中序求后序代码
15 typedef struct BiNode
16 {
17     struct BiNode *lchild,*rchild;
18     char data;
19 }BiNode,*BiTree;
20 
21 
22 void postOrder(BiTree t)
23 {
24     if (t)
25     {
26         postOrder(t->lchild);
27         postOrder(t->rchild);
28         cout << t->data;
29     }
30 }
31 
32 BiTree buildTree(int b1,int e1,int b2,int e2)
33 {
34     // 对 b1 - e1前序 和 b2 - e2中序进行操作
35     // 每次仅能判断一个根节点,然后递归判断
36     BiTree root = (BiTree)malloc(sizeof(BiNode));
37     root->lchild = NULL;
38     root->rchild = NULL;
39     root->data = s1[b1];
40     int rootIndex;
41     for (int i = b2;i<=e2;i++)
42     { // 找到中序遍历中根节点的位置
43         if (s2[i] == s1[b1])
44         {
45             rootIndex  = i;
46             break;
47         }
48     }
49     if (rootIndex != b2)
50     {
51         // 左子树不为空
52         root->lchild = buildTree(b1+1,b1+rootIndex-b2,b2,rootIndex-1);
53     }
54     if (rootIndex != e2)
55     {
56         // 右子树不为空
57         root->rchild = buildTree(b1+rootIndex-b2+1,e1,rootIndex+1,e2);
58     }
59     return root;
60 }
61 int main()
62 {
63 
64     while (cin >> s1 >> s2)
65     {
66     int len1 = strlen(s1); // 从开始地址到\0符号为止的长度
67     int len2 = strlen(s2);
68     BiTree t = buildTree(0,len1-1,0,len2-1);
69     postOrder(t);
70     cout << endl;
71     }
72 
73     return 0;
74 }

 

二叉树

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <algorithm>
 4 #include <queue>
 5 #include <stack>
 6 #include <math.h>
 7 #include <string>
 8 #include <string.h>
 9 #include <stdlib.h>
10 #include <stack>
11 using namespace std;
12 
13 // 二叉树 完全二叉树 用顺序存储好一些
14 
15 int count1(int m,int n,int &sum)
16 {
17     if ( m <= n)
18     {
19       sum ++;
20       count1(2*m,n,sum);
21     }
22 
23     if (m+1 <= n )
24     {
25        sum ++;
26        count1(2*m+1,n,sum);
27     }
28     return sum;
29 
30 }
31 int main()
32 {
33     int m,n;
34     while (scanf("%d %d",&m,&n)!=EOF)
35     {
36 
37         int sum = 1;
38         cout << count1(2*m,n,sum)<< endl;
39 
40     }
41 
42     return 0;
43 }

 

树查找

在顺序存储单元中查找某层元素,通过完全二叉树的两条性质,找到本层的第一个数和最后一个数打印输出即可。

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <algorithm>
 4 #include <queue>
 5 #include <stack>
 6 #include <math.h>
 7 #include <string>
 8 #include <string.h>
 9 #include <stdlib.h>
10 #include <stack>
11 using namespace std;
12 
13 const int N = 1000;
14 
15 int main()
16 {
17     int tree[N], n, d;
18     while (scanf("%d", &n) != EOF)
19     {
20         for (int i = 0; i < n; i++)
21         {
22             scanf("%d", &tree[i]);
23         }
24         scanf("%d", &d);
25         int start = (int)pow(2, d - 1); // 此层第一个数
26         if (start > n)
27         {
28             printf("EMPTY\n");
29         }
30         else
31         {
32             int end = pow(2, d) - 1 > n ? n : (int)pow(2, d) - 1;
33             for (int i = start - 1; i < end - 1; i++)
34             {
35                 printf("%d ", tree[i]);
36             }
37             printf("%d\n", tree[end - 1]);
38         }
39     }
40     return 0;
41 }

 

机试指南 cha 3 二叉排序树

二叉排序树

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <algorithm>
 4 #include <queue>
 5 #include <stack>
 6 #include <math.h>
 7 #include <string>
 8 #include <string.h>
 9 #include <stdlib.h>
10 #include <stack>
11 using namespace std;
12 // 二叉排序树 +前中后序遍历
13 
14 typedef struct BiNode
15 {
16     struct BiNode *lchild,*rchild;
17     int data;
18 }BiNode,*BiTree;
19 
20 void postOrder(BiTree t)
21 {
22     if (t)
23     {
24         postOrder(t->lchild);
25         postOrder(t->rchild);
26         cout << t->data <<' ';
27     }
28 }
29 
30 void InOrder(BiTree t)
31 {
32     if (t)
33     {
34         InOrder(t->lchild);
35         cout << t->data <<' ';
36         InOrder(t->rchild);
37     }
38 }
39 
40 void preOrder(BiTree t)
41 {
42     if (t)
43     {
44         cout << t->data <<' ';
45         preOrder(t->lchild);
46         preOrder(t->rchild);
47 
48     }
49 }
50 
51 void insertBiTree(BiTree &t,BiTree &p)
52 {
53     if (t == NULL)
54     {
55         t = p;
56     }else
57     {
58        if (p->data < t->data)
59         insertBiTree(t->lchild,p);
60        if (p->data > t->data)
61         insertBiTree(t->rchild,p);
62     }
63 }
64 
65 int main()
66 {
67     BiTree t = NULL;
68     int n;
69     int x;
70     BiTree p;
71     while (cin >> n )
72     {
73         t = NULL;
74     for (int i = 0;i<n;i++)
75     {
76         cin >> x;
77         p = (BiTree)malloc(sizeof(BiNode));
78         p->lchild = NULL;
79         p->rchild = NULL;
80         p->data = x;
81 
82         insertBiTree(t,p);
83     }
84     preOrder(t);cout << endl;
85     InOrder(t);cout << endl;
86     postOrder(t);cout <<endl;
87     }
88     return 0;
89 }

 

二叉搜索树(没调过)

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <algorithm>
 4 #include <queue>
 5 #include <stack>
 6 #include <math.h>
 7 #include <string>
 8 #include <string.h>
 9 #include <stdlib.h>
10 #include <stack>
11 using namespace std;
12 // 二叉搜索树 如何判断两个二叉搜索树相同?
13 // 就二叉排序树而言,相同元素的二叉排序树中序遍历一定相同,而不同元素二叉排序树
14 //使用前序遍历便可发现不同。
15 
16 typedef struct BiNode
17 {
18     struct BiNode *lchild,*rchild;
19     char data;
20 }BiNode,*BiTree;
21 
22 void preOrder(BiTree t,char pre[11],int &i)
23 {
24     if (t)
25     {
26         pre[i++] = t->data;
27         preOrder(t->lchild,pre,i);
28         preOrder(t->rchild,pre,i);
29 
30     }
31 }
32 void insertBiTree(BiTree &t,BiTree &p)
33 {
34     if (t == NULL)
35     {
36         t = p;
37     }else
38     {
39        if (p->data < t->data)
40         insertBiTree(t->lchild,p);
41        if (p->data > t->data)
42         insertBiTree(t->rchild,p);
43     }
44 }
45 void createBiTree(BiTree &t)
46 {
47     char x[11];
48     t = NULL;
49     cin >> x;
50     BiTree p;
51     for (int i = 0;i<strlen(x);i++)
52     {
53         p = (BiTree)malloc(sizeof(BiNode));
54         p->lchild = NULL;
55         p->rchild = NULL;
56         p->data = x[i];
57         insertBiTree(t,p);
58     }
59 }
60 int main()
61 {
62     BiTree t,t2;
63     int n;
64     char pre[21];
65     char pre2[21];
66     while (cin >> n )
67     {
68         if (n == 0)
69             break;
70         int pre_i = 0;
71         int pre2_i = 0;
72         createBiTree(t); // 原始的树
73         preOrder(t,pre,pre_i);
74         pre[pre_i] = '\0';
75         cout << pre << endl;
76         for (int i = 0;i<n;i++)
77         {
78             pre2_i = 0;
79             createBiTree(t2); // 新建一个树
80             preOrder(t,pre2,pre2_i);
81             pre2[pre2_i] = '\0';
82             cout << pre2 << endl;
83             if (strcmp(pre,pre2) == 0)
84                 cout << "YES" <<endl;
85             else
86                 cout << "NO" << endl;
87         }
88     }
89     return 0;
90 }

 

posted @ 2018-08-21 09:44  暴走的二萌  阅读(652)  评论(0编辑  收藏  举报