1. 程式人生 > >樹和二叉樹

樹和二叉樹

reat 完成 child names space -1 include ges 中序

編程語言:c++

截圖展示:

技術分享

技術分享

技術分享

代碼如下:

main.cpp

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <stdlib.h>
  4 #include <queue>
  5 #include <stack>
  6 #include <windows.h>
  7 #include "show.cpp"
  8 #include "jiegou.cpp"
  9 #include "shuru.cpp"
 10 #include "zhuan.cpp
" 11 #include "dfs.cpp" 12 using namespace std; 13 14 int main() 15 { int n; 16 system("color 0D"); 17 tree* h; 18 btree* node; 19 while(1) 20 { 21 see(); 22 cin>>n; 23 getchar(); 24 if(n==1) 25 { 26 system("
cls"); 27 h=creats(); 28 gen(h); 29 cout<<endl<<endl; 30 cout<<"建樹完成了"<<endl<<endl; 31 cout<<"--按任意返回進行其他操作"<<endl; 32 getchar(); 33 system("cls"); 34 continue
; 35 } 36 else if(n==2) 37 { system("cls"); 38 node=zhuan(h); 39 cout<<endl<<endl; 40 cout<<"已經成功轉換為二叉樹"<<endl<<endl; 41 cout<<"--按任意返回進行其他操作"<<endl; 42 getchar(); 43 system("cls"); 44 continue; 45 } 46 else if(n==3) 47 { 48 cout<<"前序遍歷結果為:"<<endl; 49 dfs1(node); 50 cout<<endl<<endl; 51 cout<<"--按任意返回進行其他操作"<<endl; 52 getchar(); 53 system("cls"); 54 continue; 55 } 56 else if(n==4) 57 { 58 cout<<"中序遍歷結果為:"<<endl; 59 dfs2(node); 60 cout<<endl<<endl; 61 cout<<"--按任意返回進行其他操作"<<endl; 62 getchar(); 63 system("cls"); 64 continue; 65 } 66 else if(n==5) 67 { 68 cout<<"後序遍歷結果為:"<<endl; 69 dfs3(node); 70 cout<<endl<<endl; 71 cout<<"--按任意返回進行其他操作"<<endl; 72 getchar(); 73 system("cls"); 74 continue; 75 } 76 else if(n==6) 77 { 78 cout<<"非遞歸前序遍歷結果為:"<<endl; 79 dfs4(node); 80 cout<<endl<<endl; 81 cout<<"--按任意返回進行其他操作"<<endl; 82 getchar(); 83 system("cls"); 84 continue; 85 } 86 else if(n==7) 87 { 88 cout<<"非遞歸中序序遍歷結果為:"<<endl; 89 dfs5(node); 90 cout<<endl<<endl; 91 cout<<"--按任意返回進行其他操作"<<endl; 92 getchar(); 93 system("cls"); 94 continue; 95 } 96 else if(n==8) 97 { 98 cout<<"非遞歸後序遍歷結果為:"<<endl; 99 dfs6(node); 100 cout<<endl<<endl; 101 cout<<"--按任意返回進行其他操作"<<endl; 102 getchar(); 103 system("cls"); 104 continue; 105 } 106 else if(n==9) 107 { 108 cout<<"層次遍歷結果為:"<<endl; 109 dfs7(node); 110 cout<<endl<<endl; 111 cout<<"--按任意返回進行其他操作"<<endl; 112 getchar(); 113 system("cls"); 114 continue; 115 } 116 else if(n==10) 117 { 118 system("cls"); 119 cout<<"感謝使用,下次再見"<<endl; 120 Sleep(100); 121 break; 122 } 123 else 124 { cout<<endl<<endl; 125 cout<<"輸入選項錯誤"<<n<<endl<<endl; 126 cout<<"--按任意鍵返回重新輸入"<<endl; 127 getchar(); 128 system("cls"); 129 130 } 131 } 132 return 0; 133 }

show.cpp

 1 using namespace std;
 2 void see()
 3 {
 4   printf("   %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c\n",3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3);
 5   printf("   %c                                                                    %c\n",3,3);
 6   printf("   %c                                                                    %c\n",3,3);
 7   printf("   %c       ---------  樹與二叉樹的轉換及其遍歷  ---------               %c\n",3,3);
 8   printf("   %c                                                                    %c\n",3,3);
 9   printf("   %c                                          -計科二班 劉家樹%c         %c\n",3,1,3);
10   printf("   %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c\n",3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3);
11   printf("%c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c\n",4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4);
12   printf("%c                                                                          %c\n",4,4);
13   printf("%c             1、開始建樹                     2、轉換為二叉樹              %c\n",4,4);
14   printf("%c                                                                          %c\n",4,4);
15   printf("%c             3、前序遍歷                     4、中序遍歷                  %c\n",4,4);
16   printf("%c                                                                          %c\n",4,4);
17   printf("%c             5、後序遍歷                     6、非遞歸前序                %c\n",4,4);
18   printf("%c                                                                          %c\n",4,4);
19   printf("%c             7、非遞歸中序                   8、非遞歸後序                %c\n",4,4);
20   printf("%c                                                                          %c\n",4,4);
21   printf("%c             9、層次遍歷                     10、運行結束                 %c\n",4,4);
22   printf("%c                                                                          %c\n",4,4);
23   printf("%c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c  %c\n",4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4);
24   cout<<endl;
25   cout<<"-----------恭候您的選擇,請直接用鍵盤輸入選項------------"<<endl;
26   cout<<"您的選擇是:";
27 }

shuru.cpp

 1 tree* creats()
 2 {
 3     tree *h;
 4     h=(tree*)malloc(sizeof(tree));
 5     cout<<"輸入結點值"<<endl;
 6     cin>>h->value;
 7     h->n=0;
 8     h->next=NULL;
 9     h->child=NULL;
10     if(h->value==#)
11     return h;
12     cout<<"輸入孩子數"<<endl;
13     cin>>h->n;
14     getchar();
15     h->child=(tree**)malloc(sizeof(tree*));
16     for(int i=0;i<h->n;i++)
17     h->child[i]=creats();
18     return h;
19 }

jiegou.cpp

 1 struct tree
 2 {
 3     char value;
 4     int n;
 5     tree **child;
 6     tree *next;
 7 };
 8 struct btree
 9 {
10     char value;
11     btree* left;
12     btree* right;
13 };

zhuan.cpp

 1 void gen(tree* h)
 2 {
 3     if(h==NULL||h->value==#)
 4     return ;
 5     //cout<<h->value<<endl;
 6     for(int i=0;i<h->n;i++)
 7     {   if(i+1<h->n)
 8         h->child[i]->next=h->child[i+1];
 9         gen(h->child[i]);
10     }
11 }
12 btree* creatb()
13 {
14     btree *h;
15     h=(btree *)malloc(sizeof(btree));
16     h->left=NULL;
17     h->right=NULL;
18     h->value=#;
19     return h;
20 }
21 btree* zhuan(tree* h)
22 {
23     if(h==NULL)
24     return NULL;
25     btree* node=creatb();
26     node->value=h->value;
27    // cout<<h->value<<endl;
28     if(h->n!=0)
29     node->left=zhuan(h->child[0]);
30     node->right=zhuan(h->next);
31     return node;
32 }

dfs.cpp

 1 void dfs1(btree *h)//前序
 2 {
 3     if(h!=NULL)
 4     {
 5         cout<<h->value<<" ";
 6         dfs1(h->left);
 7         dfs1(h->right);
 8     }
 9 }
10 void dfs2(btree *h)//中序
11 {
12     if(h!=NULL)
13     {
14         dfs2(h->left);
15         cout<<h->value<<" ";
16         dfs2(h->right);
17     }
18 }
19 void dfs3(btree *h)//後序
20 {
21     if(h!=NULL)
22     {
23         dfs3(h->left);
24         dfs3(h->right);
25         cout<<h->value<<" ";
26     }
27 }
28 void dfs4(btree *h)//非遞歸前序
29 {
30     stack<btree*> s;
31     s.push(h);
32     while(!s.empty())
33     {
34         h=s.top();
35         s.pop();
36         cout<<h->value<<" ";
37         if((h->right)!=NULL)
38         s.push(h->right);
39         if((h->left)!=NULL)
40         s.push(h->left);
41     }
42 }
43 void dfs5(btree *h)//非遞歸中序
44 {
45     stack<btree*> s;
46     s.push(h);
47     while(!s.empty())
48     {
49         if((h->left)!=NULL)
50         s.push(h->left);
51         h=s.top();
52         s.pop();
53         cout<<h->value<<" ";
54         if((h->right)!=NULL)
55         s.push(h->right);
56     }
57 }
58 void dfs6(btree *h)//非遞歸後序
59 {
60   if(h!=NULL)
61     {
62         dfs6(h->left);
63         dfs6(h->right);
64         cout<<h->value<<" ";
65     }
66 }
67 void dfs7(btree *h)//層次
68 {
69     queue<btree*> q;
70     q.push(h);
71     while(!q.empty())
72     {   h=q.front();
73         q.pop();
74         cout<<h->value<<" ";
75         if((h->left)!=NULL)
76         q.push(h->left);
77         if((h->right)!=NULL)
78         q.push(h->right);
79     }
80 }

樹和二叉樹