码迷,mamicode.com
首页 > 其他好文 > 详细

二叉树的计数

时间:2015-07-28 21:09:29      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:二叉树   计数   

二叉树的遍历也常常用来对二叉树进行计数。

#include <stdio.h>  
#include <stdlib.h>  
#include <malloc.h>  
#define MAXSIZE 100  
typedef char ElemType;  
typedef struct Node  
{  
    ElemType data;  
    struct Node *lchild;  
    struct Node *rchild;  
}*BitTree,BitNode;  
  
void CreateBitTree2(BitTree *T,char str[]);//非递归创建二叉树  
void DestroyBitTree(BitTree *T);//销毁二叉树  
int LeafNum(BitTree T);//统计二叉树中的叶子结点的数目  
int NotLeafCount(BitTree T);//统计二叉树中的非叶子结点的数目  
int BitTreeDepth(BitTree T);//计算二叉树的深度  


#include "LinkBiTree.h"  
  
void CreateBitTree2(BitTree *T,char str[])//非递归创建二叉树  
{  
    char ch;  
    BitTree stack[MAXSIZE];  
    int top = -1;  
    int flag,k;  
    BitNode *p;  
    *T = NULL,k = 0;  
    ch = str[k];  
    while(ch != '\0')  
    {  
        switch(ch)  
        {  
        case '(':  
            stack[++top] = p;  
            flag = 1;  
            break;  
        case ')':  
            top--;  
            break;  
        case ',':  
            flag = 2;  
            break;  
        default:  
            p = (BitTree)malloc(sizeof(BitNode));  
            p->data = ch;  
            p->lchild = NULL;  
            p->rchild = NULL;  
            if(*T == NULL)  
            {  
                *T = p;  
            }  
            else  
            {  
                switch(flag)  
                {  
                case 1:  
                    stack[top]->lchild = p;  
                    break;  
                case 2:  
                    stack[top]->rchild = p;  
                    break;  
                }  
            }  
        }  
        ch = str[++k];  
    }  
}  
void DestroyBitTree(BitTree *T)//销毁二叉树  
{  
    if(*T)  
    {  
        if((*T)->lchild)  
        {  
            DestroyBitTree(&((*T)->lchild));  
        }  
        if((*T)->rchild)  
        {  
            DestroyBitTree(&((*T)->rchild));  
        }  
        free(*T);  
        *T = NULL;  
    }  
}  
int LeafNum(BitTree T)//统计二叉树中的叶子结点的数目  
{  
    if(!T)  
    {  
        return 0;  
    }  
    else if(!T->lchild && !T->rchild)  
    {  
            return 1;  
    }  
    else  
    {  
        return LeafNum(T->lchild)+LeafNum(T->rchild);  
    }  
}  
int NotLeafCount(BitTree T)//统计二叉树中的非叶子结点的数目  
{  
    if(!T)  
    {  
        return 0;  
    }  
    else if(!T->lchild && !T->rchild)  
    {  
        return 0;  
    }  
    else  
    {  
        return NotLeafCount(T->lchild)+NotLeafCount(T->rchild)+1;  
    }  
}  
int BitTreeDepth(BitTree T)//计算二叉树的深度  
{  
    if(T == NULL)  
    {  
        return 0;  
    }  
    else  
    {  
        return BitTreeDepth(T->lchild) > BitTreeDepth(T->rchild)   
            ? 1+BitTreeDepth(T->lchild) : 1+BitTreeDepth(T->rchild);  
    }  
}  


#include "LinkBiTree.h"  
  
int main(void)  
{  
    BitTree T,root;  
    int num,depth;  
    printf("根据括号嵌套建立二叉树:(a(b(c,d),e(f(,g),h(i))))\n");  
    CreateBitTree2(&T,"(a(b(c,d),e(f(,g),h(i))))");  
    num = LeafNum(T);  
    printf("叶子结点的个数 = %2d\n",num);  
    num = NotLeafCount(T);  
    printf("非叶子结点的个数 = %2d\n",num);  
    depth = BitTreeDepth(T);  
    printf("二叉树的深度 = %2d\n",depth);  
    printf("根据括号嵌套建立二叉树:(A(B(D(,H(J)),E(,I(K,L))),C(F,G)))\n");  
    CreateBitTree2(&root,"(A(B(D(,H(J)),E(,I(K,L))),C(F,G)))");  
    num = LeafNum(root);  
    printf("叶子结点的个数 = %2d\n",num);  
    num = NotLeafCount(root);  
    printf("非叶子结点的个数 = %2d\n",num);  
    depth = BitTreeDepth(root);  
    printf("二叉树的深度 = %2d\n",depth);  
    DestroyBitTree(&T);  
    DestroyBitTree(&root);  
    return 0;  
}  
运行结果如下:

技术分享

版权声明:本文为博主原创文章,未经博主允许不得转载。

二叉树的计数

标签:二叉树   计数   

原文地址:http://blog.csdn.net/weichanjuan3/article/details/47110449

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!