#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));
}
求二叉树的高度,宽度、节点个数、叶子节点个数
最新推荐文章于 2024-05-30 14:29:32 发布