求二叉树的高度,宽度、节点个数、叶子节点个数

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

/*实现求二叉树的高度,宽度、节点个数、叶子节点个数*/
typedef struct NODE
{
    int data;
    struct NODE *lchild,*rchild;
 }TNode,*PTNode;

 PTNode createtree()
 {
    PTNode ptnode;
    printf("输入节点值:\n");
    int data;
    scanf("%d",&data);
    if(data==0)
    {
        ptnode = NULL;
    }
    else
    {
        ptnode = (PTNode)malloc(sizeof(TNode));
        if(ptnode==NULL)
        {
            printf("create tree node error!\n");
            exit(-1);
        }
        ptnode->data = data;
        ptnode->lchild = createtree();
        ptnode->rchild = createtree();
    }
    return ptnode;
 }

void preOrdertree(PTNode root)
{
    if(root!=NULL)
    {
        printf("%d ",root->data);
        preOrdertree(root->lchild);
        preOrdertree(root->rchild);
    }
}

//求二叉树叶子节点个数----递归求解
int leaf(PTNode root)
{
    if(root==0)
    {
        return 0;
    }
    else if(root->lchild==NULL && root->rchild==NULL)
    {
        printf("叶子节点%d\n",root->data);
        return 1;
    }
    else
    {
        return leaf(root->lchild)+leaf(root->rchild);
    }
 } 


 //求二叉树节点个数
int nodenum(PTNode root)
{
    if(root==NULL)
    {
        return 0;
     }

    return nodenum(root->lchild)+nodenum(root->rchild)+1;
} 

//二叉树深度
int deep(PTNode root)
{
    if(root==NULL)
        return 0;
    int left = deep(root->lchild);
    int right = deep(root->rchild);
    return left>right?left+1:right+1;
} 

//二叉树宽度
typedef struct  QNODE
{
    PTNode ptnode;
    struct QNODE *next;
}QNode,*PQNode; 
typedef struct QUEUE 
{
    PQNode front;
    PQNode rear;
}Queue,*PQueue;

PQueue initQueue()
{
    PQueue pqueue = (PQueue)malloc(sizeof(Queue));
    PQNode pqnode = (PQNode)malloc(sizeof(QNode));
    if(pqnode==NULL)
    {
        printf("initqueue error\n");
        exit(-1); 
    }
    pqueue->front = pqueue->rear = pqnode;
    pqueue->rear->next = NULL;
    return pqueue;
}
bool empty(PQueue pqueue)
{
    if(pqueue->front == pqueue->rear)
        return 1;
    return 0;
}
void enQueue(PQueue pqueue,PTNode ptnode)
{
    if(pqueue==NULL)
    {
        printf("queue is null\n");
        exit(-1);
    }
    PQNode pqnode = (PQNode)malloc(sizeof(QNode));
    if(pqnode==NULL)
    {
        printf("push queue node error\n");
        exit(-1);
    }
    pqnode->ptnode = ptnode;
    pqnode->next = NULL;
    pqueue->rear->next = pqnode;
    pqueue->rear = pqnode;
}
PTNode getQueue(PQueue pqueue)
{
    if(empty(pqueue))
    {
        printf("queue is NULL top\n");
        exit(-1);
    }
    return pqueue->front->next->ptnode;
}

void deQueue(PQueue pqueue)
{
    if(empty(pqueue))
    {
        printf("queue is NULL pop\n");
        exit(-1);
    }
    PQNode pqnode=pqueue->front->next;
    pqueue->front->next = pqnode->next;
    printf("%d出队\n",pqnode->ptnode->data);
    if(pqnode==pqueue->rear)
    {
        pqueue->rear = pqueue->front;
    }
    free(pqnode);
}
int size(PQueue pqueue)
{
    if(empty(pqueue))
    {
        return 0;
    }
    int size=0;
    PQNode p = pqueue->front->next; 
    while(p!=NULL)
    {
        size++;
        p = p->next;
    }
    return size;
}

int width(PTNode root)
{
    if(root==NULL)
    {
        return 0;
    }
    int lastwidth = 0; //上一层宽度
    int templastwidth = 0;
    int curwidth = 0;//当前层宽度
    int width = 1; //二叉树宽度
    PQueue pqueue = initQueue();
    enQueue(pqueue,root);
    lastwidth = 1;
    PTNode p = NULL;
    while(!empty(pqueue))
    {
        templastwidth = lastwidth;
        while(templastwidth!=0)
        {
            p = getQueue(pqueue);
            deQueue(pqueue);
            if(p->lchild!=NULL)
            {
                enQueue(pqueue,p->lchild);
            }
            if(p->rchild!=NULL)
            {
                enQueue(pqueue,p->rchild);
            }
            templastwidth--;
        }

        curwidth = size(pqueue);
        width = curwidth>width?curwidth:width;
        lastwidth = curwidth;       
    }
    return width;
}



void main()
{
    PTNode root;
    root = createtree();
    preOrdertree(root);
    printf("\n");
    printf("二叉树叶子节点个数%d\n",leaf(root));
    printf("\n");
    printf("二叉树节点个数%d\n",nodenum(root));
    printf("\n");
    printf("二叉树深度%d\n",deep(root));
    printf("\n");
    printf("二叉树宽度%d\n",width(root));

}

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值