Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1405091
  • 博文数量: 241
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 2253
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-11 22:27
个人简介

--

文章分类

全部博文(241)

文章存档

2021年(3)

2019年(6)

2018年(1)

2017年(9)

2016年(21)

2015年(50)

2014年(125)

2013年(26)

我的朋友

分类: C/C++

2014-03-05 18:00:32


点击(此处)折叠或打开

  1. #include "OJ.h"
  2. #include <string.h>
  3. #include <stdio.h>

  4. /*****************计算树的深度*******************/
  5. int TreeDepth(BiNode* pTop)
  6. {
  7.     if (pTop == NULL)
  8.     {
  9.         return 0;
  10.     }
  11.     else
  12.     {
  13.         int left = TreeDepth(pTop->left);
  14.         int right = TreeDepth(pTop->right);
  15.         return (left>right? left+1:right+1);
  16.     }
  17. }

  18. void bt_level_num(BiNode* tree, int a[], int h) {
  19.     if (tree == NULL)
  20.     {
  21.         h = 0;
  22.     }
  23.     else
  24.     {
  25.         a[h] += 1;
  26.         bt_level_num(tree->left, a, h + 1);
  27.         bt_level_num(tree->right, a, h + 1);
  28.     }
  29. }
  30. /****************计算树的宽度**********************/
  31. int bt_width(BiNode* tree) {
  32.     int a[N]={0}, h = 0, max=0, i;

  33.     bt_level_num(tree, a, h);
  34.  
  35.     for (i = 0; i < N; i++)
  36.     {
  37.         if (a[i] > max)
  38.         {
  39.             max = a[i];
  40.         }
  41.     }
  42.     return max;
  43. }


  44. int GetBiNodeInfo(BiNode &head, unsigned int *pulWidth, unsigned int *pulHeight)
  45. {
  46.     /*在这里实现功能*/
  47.     BiNode* pTop = &head;
  48.     if (pTop == NULL)
  49.     {
  50.         *pulWidth = 0;
  51.         *pulHeight = 0;
  52.         return -1;
  53.     }

  54.     unsigned int depth = TreeDepth(pTop);
  55.     *pulHeight = depth;

  56.     unsigned int wd = bt_width(pTop);
  57.     *pulWidth = wd;
  58.     return 0;
  59. }

阅读(1623) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~