二叉树的深度优先遍历

原文链接:http://blog.csdn.net/zhonghua18517/article/details/28238261


二叉树的深度优先遍历和先序遍历结果一样的。 思想是采用栈, 首先将根结点压入栈,如果栈不为空,而后出栈并输出当前结点中值,而后先把右子树压入栈,再把左子树压入栈,再判断栈是否为空,循环.....

步骤如下:

(1) 树的根结点入栈

(2)判断栈是否为空,不为空,则出栈,并输出出栈树结点的值

(3)出栈树结点的右子树入栈

(4)出栈树结点的左子树入栈

(5)循环回到(2)

 


创建如上树。详见前面二叉树的创建。

代码如下:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. // Tree_depth_breadth.cpp : 定义控制台应用程序的入口点。  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include <iostream>  
  6.   
  7. using namespace std;  
  8. typedef int type;  
  9.   
  10. //定义二叉树结点  
  11. typedef struct node  
  12. {  
  13.     type data;  
  14.     struct node *lchild;  
  15.     struct node *rchild;  
  16. }node,*bTree;  
  17.   
  18. //建立元素为树节点的栈  
  19. typedef struct stack  
  20. {  
  21.    bTree tree;  
  22.    struct stack *next;  
  23. }stack,*pStack;  
  24.   
  25.   
  26. //初始化栈  
  27. void InitStack(pStack &s)  
  28. {  
  29.     s=(pStack)malloc(sizeof(stack));  
  30.     if(NULL==s)  
  31.     {  
  32.       cout<<"申请内存失败!"<<endl;  
  33.      exit(-1);  
  34.     }  
  35.     s->next=NULL;  
  36. }  
  37.   
  38. //进栈  
  39. void Push(pStack s,bTree data)  
  40. {  
  41.     pStack temp=(pStack)malloc(sizeof(stack));  
  42.     if(NULL==temp)  
  43.     {  
  44.       cout<<"申请内存失败!"<<endl;  
  45.       exit(-1);  
  46.     }  
  47.     temp->tree=data;  
  48.     temp->next=s->next;  
  49.     s->next=temp;  
  50. }  
  51.   
  52. //出栈  
  53. pStack Pop(pStack s)  
  54. {  
  55.     pStack temp=s->next;  
  56.     if(NULL==temp)  
  57.     {  
  58.        cout<<"栈为空!"<<endl;  
  59.        exit(-1);  
  60.     }  
  61.       
  62.     s->next=temp->next;  
  63.     return temp;  
  64. }  
  65.   
  66. //深度优先遍历   类似于先序遍历  
  67. void DepthFS(bTree T,pStack s)  
  68. {  
  69.     Push(s,T);  
  70.     while(NULL!=s->next)  
  71.     {  
  72.       pStack temp=Pop(s);  
  73.       cout<<temp->tree->data<<" ";  
  74.         
  75.       if(NULL!=temp->tree->rchild)  
  76.           Push(s,temp->tree->rchild);  
  77.   
  78.       if(NULL!=temp->tree->lchild)  
  79.           Push(s,temp->tree->lchild);  
  80.     }  
  81. }  
  82.   
  83. //先序遍历  
  84. void PreOrder(bTree T)  
  85. {  
  86.     if(NULL!=T)  
  87.     {  
  88.         cout<<T->data<<" ";  
  89.         PreOrder(T->lchild);  
  90.         PreOrder(T->rchild);  
  91.     }  
  92. }  
  93.   
  94.   
  95. //出栈  
  96.   
  97. //创建二叉树  
  98. void CreateTree(bTree &T)  
  99. {  
  100.       
  101.     type ch;  
  102.     cin>>ch;  
  103.     if(0==ch)  
  104.     {  
  105.       T=NULL;  
  106.     }  
  107.     else  
  108.     {  
  109.         T=(bTree)malloc(sizeof(node));  
  110.         if(NULL==T)  
  111.         {  
  112.           cout<<"申请内存失败!"<<endl;  
  113.           exit(-1);  
  114.         }  
  115.         T->data=ch;  
  116.         CreateTree(T->lchild); //创建左子树  
  117.         CreateTree(T->rchild); //创建右子树  
  118.     }  
  119.       
  120. }  
  121.   
  122.   
  123.   
  124. int _tmain(int argc, _TCHAR* argv[])  
  125. {  
  126.   
  127.     bTree T=NULL;  
  128.     pStack s=NULL;  
  129.     CreateTree(T);  
  130.     InitStack(s);  
  131.       
  132.     PreOrder(T);  
  133.     cout<<endl;  
  134.     DepthFS(T,s);  
  135.     return 0;  
  136. }  

结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值