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

    • 深度优先遍历

       二叉树的遍历有三种方式,如下:

    (1)前序遍历(DLR),首先访问根结点,然后遍历左子树,最后遍历右子树。简记根-左-右。

    (2)中序遍历(LDR),首先遍历左子树,然后访问根结点,最后遍历右子树。简记左-根-右。

    (3)后序遍历(LRD),首先遍历左子树,然后遍历右子树,最后访问根结点。简记左-右-根。 

    总结:左右顺序不变 前中后是指的根节点

      如前序为根左右、中序为左根右、后序为左右根。

    • 广度优先遍历

           层次

        • 代码例子

              http://renhl252.blog.163.com/blog/static/2122100720097255425518/

    二叉树的遍历(递归(先、中、后)、非递归(先、中、后、层次))  

      1 #include <iostream>
      2 #include <stack>
      3 #include <queue>
      4 
      5 using namespace std ;
      6 
      7 typedef int TElemType;
      8 
      9 typedef struct BiNode
     10 {
     11  TElemType data;//数据
     12  struct BiNode *lchild;
     13  struct BiNode *rchild;//左孩子,右孩子
     14  bool label;
     15 }BiNode,*BiTree;
     16 
     17 
     18 //创建二叉树
     19 int CreateBiTree(BiTree &T)
     20 {
     21  TElemType data;
     22  cin>>data;
     23  if(data!=-1)
     24  {
     25   T=new BiNode;
     26   T->data=data;
     27   CreateBiTree(T->lchild);
     28   CreateBiTree(T->rchild);
     29  }
     30  else
     31  {
     32   T=NULL;
     33  }
     34  return 1;
     35 }
     36 
     37 //先序递归遍历
     38 void PreOrderTraverse(BiTree &T)
     39 {
     40  if(!T)
     41   return;
     42  cout<<T->data<<" ";
     43  PreOrderTraverse(T->lchild);
     44  PreOrderTraverse(T->rchild);
     45 }
     46 //中序递归遍历
     47 void InOrderTraverse(BiTree &T)
     48 {
     49  
     50  if(!T)
     51   return ;
     52  InOrderTraverse(T->lchild);
     53  cout<<T->data<<" ";
     54  InOrderTraverse(T->rchild);
     55 }
     56 
     57 //后序递归遍历
     58 void PostOrderTraverse(BiTree &T)
     59 {
     60  if(!T)
     61   return ;
     62  PostOrderTraverse(T->lchild);
     63  PostOrderTraverse(T->rchild);
     64  cout<<T->data<<" ";
     65 }
     66 
     67 //先序非递归遍历
     68 void PreOrderTraverse1(BiTree &T)
     69 {
     70  if(!T) return;
     71  BiTree p=T;
     72  stack<BiTree> s;
     73  while(p||!s.empty())
     74  {
     75   if(!p)
     76   {
     77    p=s.top();
     78    s.pop();
     79    p=p->rchild;
     80   }
     81   else
     82   {
     83    cout<<p->data<<" ";
     84    s.push(p);
     85    p=p->lchild;
     86   }
     87  }
     88  cout<<endl;
     89 }
     90 
     91 //中序非递归遍历
     92 void InOrderTraverse1(BiTree &T)
     93 {
     94  if(!T) return;
     95  BiTree p=T;
     96  stack<BiTree> s;
     97  while(p||!s.empty())
     98  {
     99   if(!p)
    100   {
    101    p=s.top();
    102    s.pop();
    103    cout<<p->data<<" ";
    104    p=p->rchild;
    105   }
    106   else
    107   {
    108    s.push(p);
    109    p=p->lchild;
    110   }
    111  }
    112  cout<<endl;
    113 }
    114 
    115 //后序非递归遍历
    116 void PostOrderTraverse1(BiTree &T)
    117 {
    118  if(!T) return;
    119  BiTree p=T;
    120  stack<BiTree> s;
    121  while(p||!s.empty())
    122  {
    123   
    124   if(!p)
    125   {
    126    p=s.top();
    127    s.pop();
    128    if(p->label==true)
    129    {
    130     p->label=false;
    131     s.push(p);
    132     p=p->rchild;
    133    }
    134    else
    135    {
    136     cout<<p->data<<" ";
    137     p=NULL;       //这是一个小的技巧
    138    }
    139    
    140   }
    141   else
    142   {
    143    p->label=true;
    144    s.push(p);
    145    p=p->lchild;
    146   }
    147   
    148  }
    149  
    150  cout<<endl;
    151 }
    152 
    153 
    154 //层次非递归遍历
    155 void LevelTraverse(BiTree &T)
    156 {
    157  if(!T)
    158   return ;
    159  BiTree p=T;
    160  queue<BiTree> q;
    161  q.push(p);
    162  while (!q.empty())
    163     {
    164         p=q.front();
    165   cout<<p->data<<" ";
    166         q.pop();
    167   if(p->lchild)
    168    q.push(p->lchild);
    169   if(p->rchild)
    170    q.push(p->rchild);
    171     }
    172  cout<<endl;
    173  
    174  
    175 }
    176 
    177 
    178 //删除树采用递归后序遍历
    179 void DeleteTree(BiTree & p)
    180 {
    181  if(!p)
    182   return;
    183  DeleteTree(p->lchild);
    184  DeleteTree(p->rchild);
    185  delete p;
    186  cout<<"节点删除"<<endl;
    187 }
    188 
    189 void main()
    190 {
    191  
    192  BiTree root;
    193  root=NULL;
    194  
    195  cout<<"-------------------先序遍历创建树(递归)---------------------------"<<endl;
    196  cout<<endl;
    197  cout<<"----注意事项:输入的值为整数,其中-1表示该节点为空------------------"<<endl;
    198  cout<<endl;
    199  cout<<endl;
    200  
    201  cout<<"输入数据:"<<endl;
    202  CreateBiTree(root);
    203  cout<<endl;
    204  cout<<endl;
    205  
    206  cout<<"-------------------先中后遍历(递归)-------------------------------"<<endl;
    207  cout<<endl;
    208  cout<<"先序遍历(递归):"<<endl;
    209  PreOrderTraverse(root);
    210  cout<<endl;cout<<endl;
    211  cout<<"中序遍历(递归):"<<endl;
    212  InOrderTraverse(root);
    213  cout<<endl;cout<<endl;
    214  cout<<"后序遍历(递归):"<<endl;
    215  PostOrderTraverse(root);
    216  cout<<endl;
    217  cout<<endl;
    218  cout<<endl;
    219  
    220  
    221  cout<<"-------------------先中后遍历(非递归)-----------------------------"<<endl;
    222  cout<<endl;
    223  cout<<"先序遍历(非递归):"<<endl;
    224  PreOrderTraverse1(root);
    225  cout<<endl;
    226  cout<<"中序遍历(非递归):"<<endl;
    227  InOrderTraverse1(root);
    228  cout<<endl;
    229  
    230  cout<<"后序遍历(非递归):"<<endl;
    231  PostOrderTraverse1(root);
    232  
    233  cout<<endl;
    234  cout<<endl;
    235  cout<<"---------------------层次遍历(非递归)-----------------------------"<<endl;
    236  cout<<endl;
    237  cout<<"层次遍历(非递归):"<<endl;
    238  LevelTraverse(root);
    239  cout<<endl;
    240  cout<<endl;
    241  
    242  
    243  cout<<"-------------------------删除二叉树---------------------------------"<<endl;
    244  cout<<endl;
    245  DeleteTree(root);
    246  cout<<"---------------------二叉树遍历结束---------------------------------"<<endl;
    247  
    248  
    249 }
    250 
    251  
    252 
    253 运行结果:
    254 
    255  
    256 
    257 -------------------先序遍历创建树(递归)-----------------------------------
    258 
    259  
    260 
    261 ----注意事项:输入的值为整数,其中-1表示该节点为空-------------
    262 
    263 
    264 输入数据:
    265 100
    266 50
    267 30
    268 -1
    269 -1
    270 20
    271 -1
    272 -1
    273 10
    274 -1
    275 -1
    276 
    277 
    278 -------------------先中后遍历(递归)-------------------------------
    279 
    280 先序遍历(递归):
    281 100 50 30 20 10
    282 
    283 中序遍历(递归):
    284 30 50 20 100 10
    285 
    286 后序遍历(递归):
    287 30 20 50 10 100
    288 
    289 
    290 -------------------先中后遍历(非递归)-----------------------------
    291 
    292 先序遍历(非递归):
    293 100 50 30 20 10
    294 
    295 中序遍历(非递归):
    296 30 50 20 100 10
    297 
    298 后序遍历(非递归):
    299 30 20 50 10 100
    300 
    301 
    302 ---------------------层次遍历(非递归)-----------------------------
    303 
    304 层次遍历(非递归):
    305 100 50 10 30 20
    306 
    307 
    308 -------------------------删除二叉树---------------------------------
    309 
    310 节点删除
    311 节点删除
    312 节点删除
    313 节点删除
    314 节点删除
    315 ---------------------二叉树遍历结束---------------------------------
    316 Press any key to continue
    View Code
  • 相关阅读:
    .NET视频学习总结
    hdu5288(2015多校1)OO’s Sequence
    【Go web开发之revel+mgo】第1章 述与环境
    Android获得当前系统时间、星期几、周几
    夯实Java:从面向对象说起
    centos7 安装nginx
    centos7 安装nginx
    centos7 安装nginx
    element-ui中cascader同时获取label和value值
    element-ui中cascader同时获取label和value值
  • 原文地址:https://www.cnblogs.com/renhl/p/3181268.html
Copyright © 2011-2022 走看看