树的宽度与高度

  1. // 求二叉树的深度和宽度.cpp : 定义控制台应用程序的入口点。  
  2. #include "stdafx.h"  
  3. #include <iostream>  
  4. #include <queue>  
  5. using namespace std;  
  6.   
  7. struct BTNode  
  8. {  
  9.     char m_value;  
  10.     BTNode *m_left;  
  11.     BTNode *m_right;  
  12. };  
  13.   
  14. //先序创建二叉树  
  15. void CreatBTree(BTNode *&root)  
  16. {     
  17.     char nValue = 0;  
  18.     cin >> nValue;  
  19.     if ('#' == nValue)  
  20.     {  
  21.         return;  
  22.     }  
  23.     else  
  24.     {  
  25.         root = new BTNode();  
  26.         root->m_value = nValue;  
  27.         CreatBTree(root->m_left);  
  28.         CreatBTree(root->m_right);  
  29.     }     
  30. }  
  31.   
  32. //求二叉树的深度  
  33. int GetDepth(BTNode *pRoot)  
  34. {  
  35.     if (pRoot == NULL)  
  36.     {  
  37.         return 0;  
  38.     }  
  39.   
  40.     //  int nLeftLength = GetDepth(pRoot->m_left);  
  41.     //  int nRigthLength = GetDepth(pRoot->m_right);  
  42.     //  return nLeftLength > nRigthLength ? (nLeftLength + 1) : (nRigthLength + 1);  
  43.   
  44.     return GetDepth(pRoot->m_left) > GetDepth(pRoot->m_right) ?   
  45.         (GetDepth(pRoot->m_left) + 1) : (GetDepth(pRoot->m_right) + 1);  
  46. }  
  47.   
  48. //求二叉树的宽度  
  49. int GetWidth(BTNode *pRoot)  
  50. {  
  51.     if (pRoot == NULL)  
  52.     {  
  53.         return 0;  
  54.     }  
  55.   
  56.     int nLastLevelWidth = 0;//记录上一层的宽度  
  57.     int nTempLastLevelWidth = 0;  
  58.     int nCurLevelWidth = 0;//记录当前层的宽度  
  59.     int nWidth = 1;//二叉树的宽度  
  60.         queue<BTNode *> myQueue;  
  61.     myQueue.push(pRoot);//将根节点入队列  
  62.     nLastLevelWidth = 1;      
  63.     BTNode *pCur = NULL;  
  64.   
  65.     while (!myQueue.empty())//队列不空  
  66.     {  
  67.         nTempLastLevelWidth = nLastLevelWidth;  
  68.         while (nTempLastLevelWidth != 0)  
  69.         {  
  70.             pCur = myQueue.front();//取出队列头元素  
  71.             myQueue.pop();//将队列头元素出对  
  72.   
  73.             if (pCur->m_left != NULL)  
  74.             {  
  75.                 myQueue.push(pCur->m_left);  
  76.             }  
  77.   
  78.             if (pCur->m_right != NULL)  
  79.             {  
  80.                 myQueue.push(pCur->m_right);  
  81.             }  
  82.   
  83.             nTempLastLevelWidth--;  
  84.         }  
  85.   
  86.         nCurLevelWidth = myQueue.size();  
  87.         nWidth = nCurLevelWidth > nWidth ? nCurLevelWidth : nWidth;  
  88.         nLastLevelWidth = nCurLevelWidth;  
  89.     }  
  90.   
  91.     return nWidth;  
  92. }  
  93.   
  94. int _tmain(int argc, _TCHAR* argv[])  
  95. {  
  96.     BTNode *pRoot = NULL;     
  97.     CreatBTree(pRoot);  
  98.     cout << "二叉树的深度为:" << GetDepth(pRoot) << endl;  
  99.     cout << "二叉树的宽度为:" << GetWidth(pRoot) << endl;      
  100.     system("pause");  
  101.     return 0;  
  102. }  

运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值