求二叉树结点的个数

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

typedef char ElemType;
typedef struct BTNode{
	ElemType data;
	struct BTNode *lchild;
	struct BTNode *rchild;
}BTNode,*BiTree;

void createBiTree(BTNode *&B)
{
	ElemType ch;
	scanf("%c",&ch);
	if(ch== ' ') B=NULL;
	else
	{
		B = (BTNode*)malloc(sizeof(BTNode));
		B->data = ch;
		createBiTree(B->lchild);
		createBiTree(B->rchild);
	}
}

int countNode(BiTree B)
{
	if(!B)
		return 0;
	else
		return 1 + countNode(B->lchild) + countNode(B->rchild);
}

void printPreOrder(BiTree B)
{
	if(B)
	{
		printf("%c ",B->data);
		printPreOrder(B->lchild);
		printPreOrder(B->rchild);
	}
}

void main()
{
	BiTree B;
	createBiTree(B);
	printf("先序遍历:");
	printPreOrder(B);
	printf("\n");
	printf("结点个数:%d\n",countNode(B));
}

按先序遍历来输入二叉树结点,若孩子结点为空,则输入空格。

 

下面为你提供两种使用C语言二叉树中叶结个数的方法及代码: ### 递归方法 ```c #include <stdio.h> #include <stdlib.h> typedef struct BiTNode{ char data; struct BiTNode *lchild, *rchild; }BiTNode, *BiTree; // 先序序列建立二叉树 int CreateBiTree(BiTree *T) { char ch; scanf("%c", &ch); if(ch == '#'){ *T = NULL; } else { *T = (BiTree)malloc(sizeof(BiTNode)); if( !(*T) ){ return 0; } (*T)->data = ch; CreateBiTree(&(*T)->lchild); CreateBiTree(&(*T)->rchild); } return 1; } // 叶子节点个数 int NodeCount (BiTree T) { if(T == NULL){ return 0; } else { if (T->lchild == NULL && T->rchild == NULL) { return 1; } else { return NodeCount(T->lchild) + NodeCount(T->rchild); } } } int main() { BiTree Tr; if( CreateBiTree(&Tr) ){ printf("%d", NodeCount(Tr)); } return 0; } ``` 上述代码中,`CreateBiTree` 函数用于根据先序序列建立二叉树,而 `NodeCount` 函数则通过递归的方式来计算二叉树的叶子结个数。当当前结为空时,返回0;若当前结没有左右子结,则返回1;否则,递归计算其左右子树的叶子结个数并相加 [^1]。 ### 非递归方法 ```c #include <stdio.h> #include <stdlib.h> #define MAX_QUEUE_SIZE 100 typedef struct BiTNode{ char data; struct BiTNode *lchild, *rchild; }BiTNode, *BiTree; typedef struct { BiTree data[MAX_QUEUE_SIZE]; int front, rear; } Queue; // 初始化队列 void InitQueue(Queue *Q) { Q->front = Q->rear = 0; } // 判断队列是否为空 int IsEmpty(Queue *Q) { return Q->front == Q->rear; } // 入队 void EnQueue(Queue *Q, BiTree T) { if ((Q->rear + 1) % MAX_QUEUE_SIZE == Q->front) { return; } Q->data[Q->rear] = T; Q->rear = (Q->rear + 1) % MAX_QUEUE_SIZE; } // 出队 void DeQueue(Queue *Q, BiTree *p) { if (IsEmpty(Q)) { return; } *p = Q->data[Q->front]; Q->front = (Q->front + 1) % MAX_QUEUE_SIZE; } // 先序序列建立二叉树 int CreateBiTree(BiTree *T) { char ch; scanf("%c", &ch); if(ch == '#'){ *T = NULL; } else { *T = (BiTree)malloc(sizeof(BiTNode)); if( !(*T) ){ return 0; } (*T)->data = ch; CreateBiTree(&(*T)->lchild); CreateBiTree(&(*T)->rchild); } return 1; } // 叶子节点个数 int Leaves2(BiTree T) { Queue Q; InitQueue(&Q); BiTree p; EnQueue(&Q, T); int count = 0; while(!IsEmpty(&Q)){ DeQueue(&Q, &p); if(p->lchild == NULL && p->rchild == NULL) { count++; } if(p->lchild) { EnQueue(&Q, p->lchild); } if(p->rchild) { EnQueue(&Q, p->rchild); } } return count; } int main() { BiTree Tr; if( CreateBiTree(&Tr) ){ printf("%d", Leaves2(Tr)); } return 0; } ``` 此代码使用队列实现非递归的方法来计算二叉树的叶子结个数。`InitQueue` 函数用于初始化队列,`EnQueue` 和 `DeQueue` 分别用于入队和出队操作,`Leaves2` 函数将根结入队,然后不断从队列中取出结进行判断,若该结没有左右子结,则叶子结数加1,同时将其左右子结入队 [^2]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值