zoukankan      html  css  js  c++  java
  • 树和二叉树

    编程语言: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
    ",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
    ",3,3);
     6   printf("   %c                                                                    %c
    ",3,3);
     7   printf("   %c       ---------  树与二叉树的转换及其遍历  ---------               %c
    ",3,3);
     8   printf("   %c                                                                    %c
    ",3,3);
     9   printf("   %c                                          -计科二班 刘家树%c         %c
    ",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
    ",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
    ",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
    ",4,4);
    13   printf("%c             1、开始建树                     2、转换为二叉树              %c
    ",4,4);
    14   printf("%c                                                                          %c
    ",4,4);
    15   printf("%c             3、前序遍历                     4、中序遍历                  %c
    ",4,4);
    16   printf("%c                                                                          %c
    ",4,4);
    17   printf("%c             5、后序遍历                     6、非递归前序                %c
    ",4,4);
    18   printf("%c                                                                          %c
    ",4,4);
    19   printf("%c             7、非递归中序                   8、非递归后序                %c
    ",4,4);
    20   printf("%c                                                                          %c
    ",4,4);
    21   printf("%c             9、层次遍历                     10、运行结束                 %c
    ",4,4);
    22   printf("%c                                                                          %c
    ",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
    ",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 }
  • 相关阅读:
    鼠标单击事件( onclick )
    什么是事件《js》
    作业
    javaben的设计规则
    cookie的相关方法
    cookie 基本用法
    include 指令与动作区别
    简单的购物车应用案例 相关代码4
    简单的购物车应用案例 相关代码3
    iOS UI 布局
  • 原文地址:https://www.cnblogs.com/llsq/p/7406302.html
Copyright © 2011-2022 走看看