二叉树的深度和分支节点数

来源:互联网 发布:chrome ubuntu 16.04 编辑:程序博客网 时间:2024/05/24 06:47

点击(此处)折叠或打开

  1. /*
  2.  *二叉树采用二叉链表结构表示。设计并实现如下算法:
  3.  *求一棵二叉树的深度和双分支结点的个数。
  4.  */
  5. #include <stdio.h>
  6. #include <stdlib.h>

  7. #define ERROR 0
  8. #define OK 1
  9. #define OVERFLOW 0
  10. #define STACK_INIT_SIZE 100
  11. #define STACKINCREMENT 100

  12. typedef char TElemType;
  13. typedef struct BiTNode {
  14.     TElemType data;
  15.     struct BiTNode *lchild, *rchild;
  16. }BiTNode, *BiTree;

  17. typedef BiTree ElemType;
  18. typedef struct {
  19.     ElemType *base;
  20.     ElemType *top;
  21.     int stacksize;
  22. }SqStack;

  23. int InitStack (SqStack *S)
  24. {
  25.     S->base=(ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
  26.     if (!S->base)
  27.          exit (OVERFLOW);
  28.     S->top=S->base;
  29.     S->stacksize=STACK_INIT_SIZE;
  30.     return OK;
  31. }

  32. int StackEmpty (SqStack S)
  33. {
  34.     if (S.base==S.top)
  35.         return 1;
  36.     else
  37.         return 0;
  38. }

  39. int Push (SqStack *S, ElemType e)
  40. {
  41.     if (S->top - S->base >= S->stacksize)
  42.     {
  43.         S->base=(ElemType *)realloc(S->base,
  44.                               (S->stacksize + STACKINCREMENT)* sizeof(ElemType));
  45.         if (!S->base)
  46.             exit (OVERFLOW);
  47.         S->top=S->base+S->stacksize;
  48.         S->stacksize+=STACKINCREMENT;
  49.     }
  50.     *S->top++ = e;
  51.     return OK;
  52. }

  53. int Pop(SqStack *S,ElemType *e)
  54. {
  55.     if (S->top==S->base)
  56.         return ERROR;
  57.     *e=*(--S->top);
  58.     return OK;
  59. }

  60. void CreateBiTree (BiTree *T)
  61. {
  62.     TElemType temp;
  63.     scanf ("%c", &temp);
  64.     if (temp==' ')
  65.     {
  66.         *T=NULL;
  67.     }
  68.     else
  69.     {
  70.         (*T)=(BiTree)malloc(sizeof(BiTNode));
  71.         if (!(*T))
  72.             exit (1);
  73.         CreateBiTree (&((*T)->lchild));
  74.         CreateBiTree (&((*T)->rchild));
  75.         (*T)->data=temp;
  76.     }
  77. }

  78. void Traverse (BiTree T)
  79. {
  80.     BiTree P;
  81.     SqStack S;
  82.     InitStack (&S);
  83.     P=T;
  84.     while (P||!StackEmpty(S))
  85.     {
  86.         if (P)
  87.         {
  88.             printf ("%c", P->data);
  89.             Push(&S,P);
  90.             P=P->lchild;
  91.         }
  92.         else
  93.         {
  94.             Pop(&S,&P);
  95.             P=P->rchild;
  96.         }
  97.     }
  98. }

  99. int ComputeDeep (BiTree root)
  100. {
  101.     int i=0, j=0;
  102.     if (root==NULL)
  103.     {
  104.         return 0;
  105.     }
  106.     else
  107.     {
  108.         i=ComputeDeep (root->lchild);
  109.         j=ComputeDeep (root->rchild);
  110.         return (i>j?i:j)+1;
  111.     }
  112. }

  113. int ComputeNode (BiTree root)
  114. {
  115.     int i=0,j=0;
  116.     if (root==NULL)
  117.     {
  118.         return i+j;
  119.     }
  120.     else
  121.     {
  122.         i=ComputeNode (root->lchild);
  123.         j=ComputeNode (root->rchild);
  124.         if (root->lchild&&root->rchild)
  125.             return i+j+1;
  126.         else
  127.             return i+j;
  128.     }
  129. }

  130. int main ()
  131. {
  132.     BiTree T;
  133.     int deep;
  134.     int node;
  135.     CreateBiTree (&T);
  136.     Traverse (T);
  137.     printf ("\n");
  138.     deep=ComputeDeep (T);
  139.     printf ("deep = %d\n", deep);
  140.     node=ComputeNode (T);
  141.     printf ("node = %d\n", node);
  142.     printf ("end");
  143.     getch ();
  144.     return 0;
  145. }

阅读(247) | 评论(0) | 转发(0) |
0

上一篇:二叉树

下一篇:有向图的深度和广度搜索

相关热门文章
  • 移动P2P资源算法的一些理论基...
  • 软件工程 工具之二—— PowerD...
  • 杂记(1)
  • 位置引起的错误
  • 云服务器整体服务结构...
  • test123
  • 编写安全代码——小心有符号数...
  • 使用openssl api进行加密解密...
  • 一段自己打印自己的c程序...
  • sql relay的c++接口
  • 怎么样找出BIND中查询并发量多...
  • 可有人在实际的openstack生产...
  • 如下makefile如何编写
  • sqlldr 参数配置
  • 讨论一下各位所管理的mysql生...
给主人留下些什么吧!~~