c++二叉树的各种遍历

  1. //节点的数据结构  
  2. class BTree  
  3. {  
  4. public:  
  5.  int       m_nValue;  
  6.  BTree*    m_nLeft;  
  7.  BTree*    m_nRight;  
  8. public:  
  9.  BTree(int value)  
  10.  {  
  11.   m_nValue = value;  
  12.  }  
  13. };  


一:求二叉树的节点个数:

  1. /* 
  2. 求二叉数中的节点个数 
  3. 递归解法: 
  4. 1:如果二叉树为空,节点的个数为0 
  5. 2:如果二叉树不为空,二叉树节点的个数 = 左子树节点个数+右子树节点的个数+1;
  6. 注:也可以用void GetNodeCount(BTree* pRoot,int &count)
  7. */  
  8. int GetNodeCount(BTree* pRoot)  
  9. {  
  10.     if (pRoot == NULL)  
  11.         return 0;  
  12.   
  13.     int LeftNum  =  GetNodeCount(pRoot->m_nLeft);  
  14.     int RightNum =  GetNodeCount(pRoot->m_nRight);  
  15.     int ret = LeftNum+RightNum+1;  
  16.     return ret;  
  17.       
  18. }  


二:求二叉树的深度:

  1. /* 
  2. 求二叉树的深度 
  3. 递归解法: 
  4. 1:如果二叉树为空,则二叉树的深度为0 
  5. 2:如果二叉树不为空,二叉树的深度 = MAX(左子数深度,右子树深度)+1; 
  6. */  
  7. int GetTreeDepth(BTree* pRoot)  
  8. {  
  9.     if (pRoot == NULL)  
  10.         return 0;  
  11.     int LeftDepth  = GetTreeDepth(pRoot->m_nLeft);  
  12.     int RightDepth = GetTreeDepth(pRoot->m_nRight);  
  13.     int ret = max(LeftDepth,RightDepth)+1;  
  14.     return ret;  
  15. }  

前面两种遍历异曲同工


三:四种遍历方式:

  1. /* 
  2. 前序遍历: 
  3. 1:如果二叉树为空,空操作 
  4. 2:如果二叉树不为空,访问根节点,前序遍历左子树,前序遍历右子树 
  5. */  
  6. void PreOrderTraverse(BTree* pRoot)  
  7. {  
  8.     if (pRoot == NULL)  
  9.         return;  
  10.     cout<<pRoot->m_nValue<<endl;  
  11.     PreOrderTraverse(pRoot->m_nLeft);  
  12.     PreOrderTraverse(pRoot->m_nRight);  
  13. }  
  14. /* 
  15. 中序遍历: 
  16. 1:如果二叉树为空,空操作 
  17. 2:如果二叉树不为空,第一步中序遍历左字树,第二步访问跟节点,第三步中序遍历右子树 
  18. */  
  19. void InOrderTraverse(BTree* pRoot)  
  20. {  
  21.     if (pRoot == NULL)  
  22.         return;  
  23.     InOrderTraverse(pRoot->m_nLeft);  
  24.     cout<<pRoot->m_nValue<<endl;  
  25.     InOrderTraverse(pRoot->m_nRight);  
  26. }  
  27. /* 
  28. 后序遍历: 
  29. 1:如果二叉树为空,空操作 
  30. 2:如果二叉树不为空,第一步后序遍历左子树,第二步后序遍历右子树,第三步访问跟节点; 
  31. */  
  32. void BackOrderTraverse(BTree* pRoot)  
  33. {  
  34.     if (pRoot == NULL)  
  35.         return;  
  36.     BackOrderTraverse(pRoot->m_nLeft);  
  37.     BackOrderTraverse(pRoot->m_nRight);  
  38.     cout<<pRoot->m_nValue<<endl;  
  39. }  
  40. /* 
  41. 分层遍历二叉树(按层次从上到下,从左往右) 
  42. 相当于广度优先搜素,使用队列实现。 
  43. 队列初始化,将跟节点压入队列。 
  44. 当队列不为空:弹出一个节点,访问,若左子树节点或者右子树节点不为空,将其压入队列! 
  45. */  
  46. void LevelTraverse(BTree* pRoot)  
  47. {  
  48.     if (pRoot == NULL)  
  49.         return;  
  50.     queue<BTree*> q;  
  51.     q.push(pRoot);  
  52.     while (!q.empty())  
  53.     {  
  54.         BTree* pNode = q.front();  
  55.         q.pop();  
  56.         cout<<pNode->m_nValue<<endl;//访问节点  
  57.         if(pNode->m_nLeft != NULL)  
  58.             q.push(pNode->m_nLeft);  
  59.         if (pNode->m_nRight != NULL)  
  60.             q.push(pNode->m_nRight);  
  61.     }  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值