【例3-5】擴展二叉樹


http://ybt.ssoier.cn:8088/problem_show.php?pid=1340

寫法一:結構體指針

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 struct node {//樹結點
 4     char data;//存放值
 5     node* lchild;//左兒子地址 注意數據類型為node *
 6     node* rchild;//右兒子地址 注意數據類型為node *
 7 };
 8 void create(node* &root) {
 9     char c;
10     scanf("%c", &c);
11     if (c == '.') root=NULL;
12     else {
13         root = new node;//新建樹結點
14         root->data = c; //先存根數據
15         create(root->lchild);//左子樹
16         create(root->rchild);//右子樹
17     }
18 
19 }
20 void inorder(node* root) {//中序輸出樹
21     if (root == NULL) return;
22     inorder(root->lchild);
23     printf("%c", root->data);
24     inorder(root->rchild);
25 }
26 void porder(node* root) {//后序輸出樹
27     if (root == NULL) return;
28     porder(root->lchild);
29     porder(root->rchild);
30     printf("%c", root->data);
31 }
32 int main() {
33     node* btnode = new node;//新建樹根結點
34     create(btnode);//創建樹
35     inorder(btnode);//先序輸出
36     printf("\n");
37     porder(btnode);//后序輸出
38     return 0;
39 }
View Code

 

寫法二:結構體指針typedef

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 struct Node;
 4 typedef Node *tree;
 5 struct Node {
 6     char data;
 7     tree lchild, rchild;
 8 };
 9 tree bt;
10 int i = -1;
11 string s;
12 void build(tree &bt) //建樹
13 {
14     if (s[++i] != '.') {
15         bt = new Node;
16         bt->data = s[i];
17         build(bt->lchild);
18         build(bt->rchild);
19     } else
20         bt = NULL;
21 }
22 void inorder(tree bt) //輸出中序序列
23 {
24     if (bt) {
25         inorder(bt->lchild);
26         cout << bt->data;
27         inorder(bt->rchild);
28     }
29 }
30 void postorder(tree bt) //輸出后序序列
31 {
32     if (bt) {
33         postorder(bt->lchild);
34         postorder(bt->rchild);
35         cout << bt->data;
36     }
37 }
38 int main() {
39     cin >> s;
40     build(bt);
41     inorder(bt);
42     cout << endl;
43     postorder(bt);
44     cout << endl;
45     return 0;
46 }
View Code

 

 寫法三:結構體數組實現樹

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 struct  node{
 4     char data;
 5     int lchild, rchild;
 6 };
 7 node tree[1000];
 8 int  num=1;
 9 void build(int root)
10 {
11     char c;
12     cin>>c;
13     if(c!='.'){
14         tree[root].data=c;//給根賦值 
15         tree[root].lchild=++num;//左兒子地址賦值 
16         build(tree[root].lchild);//進入左兒子 
17         tree[root].rchild=++num;//右兒子地址賦值 
18         build(tree[root].rchild);//進入右兒子 
19     }
20     else{
21         tree[root].data='.';//便於后面輸出時判斷 
22     }
23 }
24 void inorder(int root){//中序遍歷 
25     if(tree[root].data!='.'){
26         inorder(tree[root].lchild);//1.先訪問左兒子 
27         cout<<tree[root].data;// 2.訪問根並輸出根的值 
28         inorder(tree[root].rchild);//3.訪問右兒子 
29     }
30 }
31 void postorder(int root){//后序遍歷 
32     if(tree[root].data!='.'){
33         postorder(tree[root].lchild);//1.先訪問左兒子
34         postorder(tree[root].rchild);//2.訪問右兒子 
35         cout<<tree[root].data;//3.訪問根並輸出根的值
36     }
37 }
38 int main()
39 {
40     build(1);//結構體數組以下標1的數據為根 
41     //測試代碼 
42 //    for(int i=1; i<=15; i++){
43 //        cout<<i<<" "<<tree[i].data<<" "<<tree[i].lchild<<" "<<tree[i].rchild<<endl;
44 //    }
45     inorder(1);
46     cout<<endl;
47     postorder(1);
48     return 0;
49 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM