zoukankan      html  css  js  c++  java
  • 二叉树的遍历

     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 #include <algorithm>
     5 #include <cstdlib>
     6 // #define test cout<<"***"<<endl
     7 // #define test1 cout<<"&&&&"<<endl
     8 
     9 using namespace std;
    10 
    11 typedef struct Tree{
    12     int value;
    13     struct Tree *left;
    14     struct Tree *right;
    15 }Tree ,*Binary_Tree;
    16 
    17 //创建二叉树
    18 void CreatTree(Binary_Tree &T){
    19     int n;
    20     T=(Binary_Tree)malloc(sizeof(Tree));
    21     cin>>n;
    22     if(n==0)
    23     T=NULL;
    24     else{
    25         T->value=n;
    26         CreatTree(T->left);
    27         CreatTree(T->right);
    28     }
    29 }
    30 
    31 //先序遍历
    32 void preshowTree(Binary_Tree T){
    33     if(T!=NULL){
    34         cout<<T->value<<" ";
    35         preshowTree(T->left);
    36         preshowTree(T->right);
    37     }
    38 }
    39 
    40 //中序遍历
    41 void midshowTree(Binary_Tree T){
    42     if(T!=NULL){
    43         midshowTree(T->left);
    44         cout<<T->value<<" ";
    45         midshowTree(T->right);
    46     }
    47 }
    48 
    49 //后序遍历
    50 void finshowTree(Binary_Tree T){
    51     if(T!=NULL){
    52         finshowTree(T->left);
    53         finshowTree(T->right);
    54         cout<<T->value<<" ";
    55     }
    56 }
    57 int main(){
    58     Tree *T;
    59     cout<<"创建一棵二叉树"<<endl;
    60     CreatTree(T);
    61     // test1;
    62     cout<<"先序遍历:"<<" ";
    63     preshowTree(T);
    64     cout<<endl;
    65     cout<<"中序遍历:"<<" ";
    66     midshowTree(T);
    67     cout<<endl;
    68     cout<<"后序遍历:"<<" ";
    69     finshowTree(T);
    70     cout<<endl;
    71     return 0;
    二叉树的三种遍历
     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 #include <algorithm>
     5 #include <cstdlib>
     6 // #define test cout<<"***"<<endl
     7 // #define test1 cout<<"&&&&"<<endl
     8 
     9 using namespace std;
    10 
    11 typedef struct Tree{
    12     int value;
    13     struct Tree *left;
    14     struct Tree *right;
    15 }Tree ,*Binary_Tree;
    16 
    17 //创建二叉树
    18 void CreatTree(Binary_Tree &T){
    19     int n;
    20     T=(Binary_Tree)malloc(sizeof(Tree));
    21     cin>>n;
    22     if(n==0)
    23     T=NULL;
    24     else{
    25         T->value=n;
    26         CreatTree(T->left);
    27         CreatTree(T->right);
    28     }
    29 }
    30 
    31 //先序遍历
    32 void preshowTree(Binary_Tree T){
    33     if(T!=NULL){
    34         cout<<T->value<<" ";
    35         preshowTree(T->left);
    36         preshowTree(T->right);
    37     }
    38 }
    39 
    40 //中序遍历
    41 void midshowTree(Binary_Tree T){
    42     if(T!=NULL){
    43         midshowTree(T->left);
    44         cout<<T->value<<" ";
    45         midshowTree(T->right);
    46     }
    47 }
    48 
    49 //后序遍历
    50 void finshowTree(Binary_Tree T){
    51     if(T!=NULL){
    52         finshowTree(T->left);
    53         finshowTree(T->right);
    54         cout<<T->value<<" ";
    55     }
    56 }
    57 int main(){
    58     Tree *T;
    59     cout<<"创建一棵二叉树"<<endl;
    60     CreatTree(T);
    61     // test1;
    62     cout<<"先序遍历:"<<" ";
    63     preshowTree(T);
    64     cout<<endl;
    65     cout<<"中序遍历:"<<" ";
    66     midshowTree(T);
    67     cout<<endl;
    68     cout<<"后序遍历:"<<" ";
    69     finshowTree(T);
    70     cout<<endl;
    71     return 0;
    72 }
  • 相关阅读:
    mysql 注意事项 PreparedStatement 对比 statement
    Dbutils commons-dbutils-1.3
    C3P0 mysql 5.7
    servlet-应用mysql-1
    javabean 用integer 而不是int
    servlet-1
    servlet 路径 编码 问题
    mac tomcat 9.0
    case end 的用法
    自定义抛出异常
  • 原文地址:https://www.cnblogs.com/zllwxm123/p/7986080.html
Copyright © 2011-2022 走看看