当前位置:网站首页>C语言版:二叉树叶子结点和非叶子结点求法
C语言版:二叉树叶子结点和非叶子结点求法
2022-04-21 06:24:00 【历飞雨_smile】
数据结构:二叉树中的叶子结点和非叶子结点的求法。
前言
叶子结点: 没有左孩子和右孩子,指向NULL, 非叶子结点:根结点和有左孩子或者右孩子,可能全有的情况,这是咱们递归遍历的结束的条件。
提示:以下是本篇文章正文内容,下面案例可供参考
一、叶子和非叶子结点区别
叶子结点数是在结点无左孩子和无右孩子的情况+1,不和递归同时进行,而非叶子结点则是+1,和递归同时进行。 原因:叶子结点是使递归结束的条件,和递归进行是相反的,故不能放在一起。
二、求法和打印
1.叶子结点数求法
代码如下(示例):
//求叶子结点函数
int leaf_node_function(RTreeNode *p)
{
if(!p)
{
return 0; //空二叉树的情况
}
else if(p->left_child == NULL && p->right_child == NULL)
{
return + 1;
}
else
{
return leaf_node_function(p->left_child) + leaf_node_function(p->right_child);
}
} //因为我们用的递归是根结点开始的,所以必须把递归的函数放在else(非叶子结点这里)
//打印叶子结点
void leaf_print(RTreeNode *p)
{
if(p != NULL)
{
if(p->left_child == NULL && p->right_child == NULL)
{
printf("%c ", p->data);
}
leaf_print(p->left_child);
leaf_print(p->right_child);
}
}
2.非叶子结点数求法
代码如下(示例):
//求非叶子结点函数
int Non_leaf_node_function(RTreeNode *p)
{
if(!p)
{
return 0; //空二叉树
}
else if(p->left_child == NULL && p->right_child == NULL)
{
return 0; //结点为叶子结点 = 左子树 + 右子树 = NULL
}
else
{
return Non_leaf_node_function(p->left_child) + Non_leaf_node_function(p->right_child) + 1;
}//就利用控制流程语句,就能控制递归
}
//打印非叶子结点
void Non_leaf_print(RTreeNode *p)
{
if(p != NULL)
{
if(!(p->left_child == NULL && p->right_child == NULL)) //为叶子结点的
{
printf("%c ", p->data);
}
Non_leaf_print(p->left_child);
Non_leaf_print(p->right_child);
}
}
三,代码实现
//
// Created by xoo on 2021/7/7.
//
//采用静态建立二叉树,求叶子结点和非叶子结点
#include<stdio.h>
#include<stdlib.h>
typedef struct TreeNode
{
struct TreeNode *left_child, *right_child;
char data;
}RTreeNode;
//生成一个结点
RTreeNode *init_node(char x, RTreeNode *left_child, RTreeNode *right_child)
{
RTreeNode *node;
node = (struct TreeNode*) malloc(sizeof(struct TreeNode));
node->left_child = left_child;
node->right_child = right_child;
node->data = x;
return node;
}
//生成一颗二叉树树
RTreeNode *create_tree()
{
RTreeNode *root, *b, *c, *d, *e, *f;
d = init_node('D', NULL, NULL);
e = init_node('E', NULL, NULL);
f = init_node('F', NULL, NULL);
b = init_node('B', d, NULL);
c = init_node('C', e, f);
root = init_node('A', b, c);
return root;
}
//求叶子结点函数
int leaf_node_function(RTreeNode *p)
{
if(!p)
{
return 0; //空二叉树的情况
}
else if(p->left_child == NULL && p->right_child == NULL)
{
return + 1;
}
else
{
return leaf_node_function(p->left_child) + leaf_node_function(p->right_child);
}
} //因为我们用的递归是根结点开始的,所以必须把递归的函数放在else(非叶子结点这里)
//求非叶子结点函数
int Non_leaf_node_function(RTreeNode *p)
{
if(!p)
{
return 0; //空二叉树
}
else if(p->left_child == NULL && p->right_child == NULL)
{
return 0; //结点为叶子结点 = 左子树 + 右子树 = NULL
}
else
{
return Non_leaf_node_function(p->left_child) + Non_leaf_node_function(p->right_child) + 1;
}
}
//打印非叶子结点
void Non_leaf_print(RTreeNode *p)
{
if(p != NULL)
{
if(!(p->left_child == NULL && p->right_child == NULL)) //为叶子结点的
{
printf("%c ", p->data);
}
Non_leaf_print(p->left_child);
Non_leaf_print(p->right_child);
}
}
//打印叶子结点
void leaf_print(RTreeNode *p)
{
if(p != NULL)
{
if(p->left_child == NULL && p->right_child == NULL)
{
printf("%c ", p->data);
}
leaf_print(p->left_child);
leaf_print(p->right_child);
}
}
//前序遍历
void preorder(RTreeNode *p)
{
if(p != NULL)
{
printf("->%c", p->data);
preorder(p->left_child);
preorder(p->right_child);
}
}
int main()
{
RTreeNode *root;
root = create_tree();
printf("前序遍历:");
preorder(root);
printf("\n");
printf("非叶子的个数:%d", Non_leaf_node_function(root));
printf("\n");
printf("非叶子结点:");
Non_leaf_print(root);
printf("\n");
printf("求叶子结点的个数:%d", leaf_node_function(root));
printf("\n叶子结点:");
leaf_print(root);
printf("\n");
system("pause");
return 0;
}
结果:
总结
其实和二叉树的动态建立有很大关联, 只需要理解如何用递归建立起二叉树,求叶子和非叶子结点就很简单,因为你理解了它的递归思路,很容易想到该如何控制递归结束条件。
版权声明
本文为[历飞雨_smile]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_53318344/article/details/118538497
边栏推荐
- dpdk 问题分析:dpdk-20.11 ice 100G 网卡 rss_hash 配置无效问题
- C#数组
- Chapter 5 support vector machine (SVM)
- Substring Inversion (Easy Version)
- yolov5的onnx模型去除transpose层
- setpci 命令与内核 pci_enable_device 与 pci_disable_device 函数
- C#类和方法的定义
- Add parentheses to Boolean expressions for short circuit operators
- WordPress修改上传文件大小限制
- 2020杭电多校赛第一场1006 Finding a MEX(hdu6756)
猜你喜欢
随机推荐
There is no prompt for importing JSTL tag library URI
开放平台及其技术架构
杂项1
Gojs anhydrous printing plate
LEFT JOIN关联表中ON,WHERE后面跟条件的区别
[STM32 & LwIP] record the solution of a strange Ping failure
P1018 乘积最大题解
2020杭电多校赛第一场1006 Finding a MEX(hdu6756)
[STM32] cubemx configuration diagram of 480mhz clock under 25MHz external crystal oscillator of h743
Implémenter un tableau en tant que fonction JS. Prototype. Foreach (),. Map (),. Filtre ()
SQL--查询分组和限制返回行数
Chapter 5 support vector machine (SVM)
pg 数据库不能使用 zh_CN.UTF-8:initdb: error: locale “zh_CN.UTF-8“ requires unsupported encoding “GBK“
完全清理mysql-win
C语言版:二叉树的前序,中序,后序非递归遍历实现
云计算中网络基础知识
使用zk实现分布式锁原理代码浅析
跨域问题-Allow-Origin header contains multiple values... but only one is allowed
微服务架构下的数据库拆分
数据的导出









