先序遍历创建二叉树,对二叉树统计叶子节点个数和统计深度(创建二叉树时#代表空树,序列不能有误)c语言
来源:互联网 发布:java中object类的方法 编辑:程序博客网 时间:2024/05/21 13:58
#include "stdio.h"
#include "string.h"
#include "malloc.h"
#define NULL 0
#define MAXSIZE 30
typedef struct BiTNode //定义二叉树数据结构
{
char data;
struct BiTNode *lchild,*rchild;
} BiTNode;
void preCreate(BiTNode *& T) //先序遍历建立二叉树,#代表空树
{
char ch;
ch=getchar();
if(ch=='#')
T=NULL;
else
{
if(!(T=(BiTNode *)malloc(sizeof(BiTNode))))
printf("Error!");
T->data=ch;
preCreate(T->lchild);
preCreate(T->rchild);
}
}
int getLeafNum(BiTNode *root)//统计二叉树叶子节点个数
{
int count=0;//叶子总数,左子树叶子数.右子数叶子数
int left_count=0;
int right_count=0;
/*判断根节点是否为null
若根节点不空,判断根节点是否是叶子,是的话叶子总数+1并返回,
若不是统计左子树叶子数目和右子数叶子数目并相加返回
若根节点为空,则叶子数为0并返回
*/
if(root)
{
if(root->lchild==NULL&&root->rchild==NULL)
count++;
else
{
left_count=getLeafNum(root->lchild);
right_count=getLeafNum(root->rchild);
count=left_count+right_count;
}
}
else
{
count=0;
}
return count;
}
int getTreeDepth(BiTNode *root)//统计二叉树深度
{
int depth=0;
int left_depth=0;
int right_depth=0;
/*
判断根节点是否为空,
若根节点为空,深度置为0,并返回
若根节点不为空,统计左子树深度,统计右子树深度,二者相加后再加上1(1位根节点)并返回
*/
if(root)
{
left_depth=getTreeDepth(root->lchild);
right_depth=getTreeDepth(root->rchild);
depth=1+(left_depth>right_depth?left_depth:right_depth);
}
else
{
depth=0;
}
return depth;
}
int main()
{
BiTNode * bitree=NULL;
preCreate(bitree);//先序遍历创建二叉树
printf("叶子个数:%d\n",getLeafNum(bitree));
printf("该二叉树深度:%d\n",getTreeDepth(bitree));
return 0;
}
#include "string.h"
#include "malloc.h"
#define NULL 0
#define MAXSIZE 30
typedef struct BiTNode //定义二叉树数据结构
{
char data;
struct BiTNode *lchild,*rchild;
} BiTNode;
void preCreate(BiTNode *& T) //先序遍历建立二叉树,#代表空树
{
char ch;
ch=getchar();
if(ch=='#')
T=NULL;
else
{
if(!(T=(BiTNode *)malloc(sizeof(BiTNode))))
printf("Error!");
T->data=ch;
preCreate(T->lchild);
preCreate(T->rchild);
}
}
int getLeafNum(BiTNode *root)//统计二叉树叶子节点个数
{
int count=0;//叶子总数,左子树叶子数.右子数叶子数
int left_count=0;
int right_count=0;
/*判断根节点是否为null
若根节点不空,判断根节点是否是叶子,是的话叶子总数+1并返回,
若不是统计左子树叶子数目和右子数叶子数目并相加返回
若根节点为空,则叶子数为0并返回
*/
if(root)
{
if(root->lchild==NULL&&root->rchild==NULL)
count++;
else
{
left_count=getLeafNum(root->lchild);
right_count=getLeafNum(root->rchild);
count=left_count+right_count;
}
}
else
{
count=0;
}
return count;
}
int getTreeDepth(BiTNode *root)//统计二叉树深度
{
int depth=0;
int left_depth=0;
int right_depth=0;
/*
判断根节点是否为空,
若根节点为空,深度置为0,并返回
若根节点不为空,统计左子树深度,统计右子树深度,二者相加后再加上1(1位根节点)并返回
*/
if(root)
{
left_depth=getTreeDepth(root->lchild);
right_depth=getTreeDepth(root->rchild);
depth=1+(left_depth>right_depth?left_depth:right_depth);
}
else
{
depth=0;
}
return depth;
}
int main()
{
BiTNode * bitree=NULL;
preCreate(bitree);//先序遍历创建二叉树
printf("叶子个数:%d\n",getLeafNum(bitree));
printf("该二叉树深度:%d\n",getTreeDepth(bitree));
return 0;
}
0 0
- 先序遍历创建二叉树,对二叉树统计叶子节点个数和统计深度(创建二叉树时#代表空树,序列不能有误)c语言
- C语言二叉树创建、遍历、深度、叶子节点
- 二叉树的创建、遍历、深度、叶子节点个数
- C语言二叉树创建、遍历以及统计结点、叶子结点
- 创建二叉树求叶子节点个数
- 二叉树的创建、遍历、深度、叶子节点数
- 【数据结构_树_Tree_0971】统计利用先序遍历创建的二叉树的深度
- 统计利用先序遍历创建的二叉树的深度
- 一个关于二叉树的创建、先序遍历、中序遍历、后序遍历、求叶子节点的完整函数的c语言完整程序。
- 二叉树(二)——遍历、深度统计、叶子结点统计、结点统计
- [c++ 二叉树创建与遍历] 数据结构实验之二叉树三:统计叶子数
- 二叉树的字符串创建和遍历,求深度,叶子节点数
- 二叉树之统计叶子节点
- 统计并保存叶子节点(二叉树)
- 2013-04-06 二叉树的创建,遍历,统计度为2的节点的个数。
- C语言实现二叉树的基本操作---创建、遍历、求深度、求叶子结点
- C语言实现二叉树的基本操作---创建、遍历、求深度、求叶子结点
- 二叉树 遍历|统计叶子节点|求深度|交换左右子树|查找是否存在某个特定叶子节点练习题
- Virtio概述和基本原理(KVM半虚拟化驱动)
- hdu 4932
- Shortest Prefixes
- 把这40句话看懂了 你会想通很多事情(想不通的时候可以慢慢看。。。)
- 基于highcharts封装的一个简单易用的报表统计
- 先序遍历创建二叉树,对二叉树统计叶子节点个数和统计深度(创建二叉树时#代表空树,序列不能有误)c语言
- 转Array.prototype.slice.call(arguments)
- Linux bridge-utils tunctl 使用
- ok6410 LED/GPIO 控制 C#程序
- Babelfish
- Phone List(trie树)
- 枚举 hdu1172 猜数字
- Unity3d中的Awake()、OnEnable()、Start()等默认函数的执行顺序和生命周期
- arm linux kernel 从入口到start_kernel 的代码分析