zoukankan      html  css  js  c++  java
  • 二叉树递归建立和遍历

    二叉树创建遍历规则:

    1.先序:根-左-右

    2.中序:左-根-右

    3.后序:左-右-根


      1 二叉树定义和辅助函数如下:
      2 
      3 
      4 
      5 struct node {  
      6     int data;  
      7     struct node* left;  
      8     struct node* right;  
      9 };  
     10   
     11 void visit(int data)  
     12 {  
     13     printf("%d ", data);  
     14 }  
     15 
     16 int indata()
     17 {
     18     int data;
     19     scanf("%d",&data);
     20     return data;
     21 }
     22 
     23 
     24 
     25 
     26 
     27 
     28 
     29 
     30 
     31 先序创建二叉树:
     32 
     33 
     34 
     35 struct node* CreateBiTree()//先序建立一个二叉树  
     36 {   
     37     char x; //x为根节点  
     38     struct node* t;   
     39     x=indata();  
     40     if (x==' ') /* 判断当前子树是否创建完成*/   
     41         return NULL;   
     42     else   
     43     {   
     44         t=(struct node*)malloc(sizeof(struct node));   
     45         t->data=x;   
     46         t->left=CreateBiTree();   
     47         t->right=CreateBiTree();   
     48     }   
     49     return t;  
     50 }  
     51 先序遍历二叉树:
     52 
     53 
     54 
     55 
     56 
     57 void preOrder(struct node* root)  
     58 {  
     59     if (root == NULL)  
     60         return;  
     61     visit(root->data);  
     62     preOrder(root->left);  
     63     preOrder(root->right);  
     64 }  
     65 
     66 
     67 
     68 
     69 
     70 
     71 中序创建二叉树:
     72 
     73 
     74 
     75 struct node* CreateBiTree()//先序建立一个二叉树  
     76 {   
     77     char x; //x为根节点  
     78     struct node* t;   
     79     x=indata();  
     80     if (x==' ') /* 判断当前子树是否创建完成*/   
     81         return NULL;   
     82     else   
     83     {  
     84         t=(struct node*)malloc(sizeof(struct node)); 
     85         t->left=CreateBiTree();  
     86         t->data=x;     
     87         t->right=CreateBiTree();   
     88     }   
     89     return t;  
     90 }  
     91 中序遍历二叉树:
     92 
     93 
     94 
     95 
     96 
     97 void inOrder(struct node* root)  
     98 {  
     99     if (root == NULL)  
    100         return;  
    101     inOrder(root->left);  
    102     visit(root->data);  
    103     inOrder(root->right);  
    104 }  
    105 
    106 
    107 
    108 
    109 后序创建二叉树
    110 
    111 
    112 
    113 
    114 
    115 struct node* CreateBiTree()//先序建立一个二叉树  
    116 {   
    117     char x; //x为根节点  
    118     struct node* t;   
    119     x=indata();  
    120     if (x==' ') /* 判断当前子树是否创建完成*/   
    121         return NULL;   
    122     else   
    123     {  
    124         t=(struct node*)malloc(sizeof(struct node)); 
    125         t->left=CreateBiTree();    
    126         t->right=CreateBiTree();   
    127         t->data=x;   
    128     }   
    129     return t;  
    130 }  
    131 后序遍历二叉树
    132 
    133 
    134 
    135 
    136 
    137 void inOrder(struct node* root)  
    138 {  
    139     if (root == NULL)  
    140         return;  
    141     inOrder(root->left);   
    142     inOrder(root->right);  
    143     visit(root->data); 
    144 }  

     下面以一个例子说明:

    1         1
    2       /   
    3      2    3
    4     /    / 
    5    4  5  6   7

    先序遍历(根左右)为:1 2 4 5 3 6 7

    中序遍历(左根右)为:4 2 5 1 6 3 7

    后序遍历(左右根)为:4 5 2 6 7 3 1

    代码如下:

     1 #include <iostream>
     2 using namespace std;
     3 
     4 struct Node
     5 {
     6     int data;
     7     Node* left;
     8     Node* right;
     9 };
    10 
    11 Node* CreateBinaryTree()
    12 {
    13      int data;
    14      cin>>data;
    15      Node* root;
    16      if(data==0)
    17      {
    18          return NULL;
    19      }
    20       else
    21       {
    22           root=new Node();
    23           root->data=data;
    24           root->left=CreateBinaryTree();    
    25           root->right=CreateBinaryTree();    
    26       }
    27      return root;
    28 }
    29 
    30 void PreOrder(Node* root)
    31 {
    32     if(root==NULL)
    33         return;
    34 
    35     cout<<root->data<<",";
    36     PreOrder(root->left);
    37     PreOrder(root->right);
    38 }
    39 
    40 void InOrder(Node* root)
    41 {
    42     if(root==NULL)
    43         return;
    44 
    45     InOrder(root->left);
    46     cout<<root->data<<",";
    47     InOrder(root->right);
    48 }
    49 
    50 void AfterOrder(Node* root)
    51 {
    52     if(root==NULL)
    53         return;
    54 
    55     AfterOrder(root->left);
    56     AfterOrder(root->right);
    57     cout<<root->data<<",";
    58 }
    59 
    60 
    61 int main(int argc ,char* argv[])
    62 {
    63     Node* Root;
    64     cout<<"--------------vpoet二叉树-------------"<<endl;
    65     cout<<"先序递归创建二叉树:"<<endl;
    66     Root=CreateBinaryTree();
    67     cout<<"先序遍历二叉树"<<endl;
    68     PreOrder(Root);
    69     cout<<endl;
    70     cout<<"中序遍历二叉树"<<endl;
    71     InOrder(Root);
    72     cout<<endl;
    73     cout<<"后序遍历二叉树"<<endl;
    74     AfterOrder(Root);
    75     cout<<endl;
    76     system("pause");
    77     return 0;
    78 }

    运行截图:

  • 相关阅读:
    Paragon NTFS for Mac免费获取官方赠送正版.更新获取ntfs for mac 14方法
    Python修饰器的函数式编程
    Python装饰器与面向切面编程
    linux配置IP的方法
    Centos搭建SVN服务器三步曲
    [CentOS 6.5 X64]讓firefox java plugin 啟動
    逻辑分析题(三)
    逻辑分析题汇总(一)
    第十章—DOM(0)—NODE类型
    第十章—DOM(三)——Text类型
  • 原文地址:https://www.cnblogs.com/vpoet/p/4659725.html
Copyright © 2011-2022 走看看