Toggle navigation
首页
热门资讯
资源下载
技术博客
会员
中心
会员中心
发布博文
发布资源
首页
文章中心
tree
二叉树的创建、遍历、深度、叶子节点个数
78 阅读
0 评论
52 点赞
我是
靠谱客
的博主
俊秀香水
,这篇文章主要介绍
二叉树的创建、遍历、深度、叶子节点个数
,现在分享给大家,希望可以做个参考。
#include
<
stdio.h
>
#include
<
malloc.h
>
typedef
struct
bnode
...
{
char
data;
struct
bnode
*
left,
*
right;
}
btree;
btree
*
creat()
...
{
btree
*
t;
char
p;
p
=
getchar();
if
(p
==
'
#
'
)
return
NULL;
else
...
{
t
=
(btree
*
)malloc(
sizeof
(btree));
t
->
data
=
p;
t
->
left
=
creat();
t
->
right
=
creat();
return
t;
}
}
void
preorder(btree
*
t)
...
{
if
(t
!=
NULL)
...
{
printf(
"
%3c
"
, t
->
data);
preorder(t
->
left);
preorder(t
->
right);
}
}
void
inorder(btree
*
t)
...
{
if
(t
!=
NULL)
...
{
inorder(t
->
left);
printf(
"
%3c
"
, t
->
data);
inorder(t
->
right);
}
}
void
postorder(btree
*
t)
...
{
if
(t
!=
NULL)
...
{
postorder(t
->
left);
postorder(t
->
right);
printf(
"
%3c
"
, t
->
data);
}
}
int
m
=
0
;
int
leaves(btree
*
t)
...
{
if
(t
!=
NULL)
...
{
if
(t
->
left
==
NULL
&&
t
->
right
==
NULL)
m
++
;
else
...
{
leaves(t
->
left);
leaves(t
->
right);
}
}
return
m;
}
int
depth(btree
*
t)
...
{
int
dep1, dep2;
if
(t
==
NULL)
return
0
;
else
...
{
dep1
=
depth(t
->
left);
dep2
=
depth(t
->
right);
return
(dep1
>
dep2
?
dep1
+
1
: dep2
+
1
);
}
}
int
main()
...
{
printf(
"
输入创建二叉树的字符
"
);
btree
*
tree_1
=
creat();
printf(
"
先序遍历
"
);
preorder(tree_1);
printf(
"
"
);
printf(
"
中序遍历
"
);
inorder(tree_1);
printf(
"
"
);
printf(
"
后序遍历
"
);
postorder(tree_1);
printf(
"
"
);
int
num
=
leaves(tree_1);
printf(
"
叶节点的个数:%d
"
, num);
printf(
"
二叉树的深度:%d
"
, depth(tree_1));
}
最后
以上就是
俊秀香水
最近收集整理的关于
二叉树的创建、遍历、深度、叶子节点个数
的全部内容,更多相关
二叉树
内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(
52
)
本文分类:
tree
浏览次数:
78
次浏览
发布日期:2024-05-15 18:50:01
本文链接:
https://www.kaopuke.com/article/k-p-k_13_u_7_o_26_f0_13_j_18_4.html
相关文章
树的逻辑结构、树的存储结构树的定义树的存储结构
c#二叉树 取叶子节点个数_图文并茂方式轻松掌握数据结构之二叉树和B树!1. 树结构示意图2. 二叉树(Binary Tree)2.1 满二叉树(Full Binary Tree)2.2 完全二叉树(Complete Binary Tree)2.3 平衡二叉树(Balanced Binary Tree)2.4 二叉搜索树(Binary Search Tree)2.5 红黑树(Red Black Tree)3. B 树3.1 B- 树3.2 B+ 树3.3 B* 树
二叉树的相关概念以及存储结构
打印二叉搜索树的叶子结点_数据结构 | 四种平衡二叉树介绍1 二叉搜索树2 六种平衡二叉搜索树3 平衡搜索树的性能对比
二叉树的创建、遍历、深度、叶子节点个数
【数据结构】已知一棵二叉链表表示的二叉树T,编写函数,判断T是否为完全二叉树1.算法思想2.定义结构体3.函数实现4.测试结果5.完整代码
二叉图(高级数据结构)一、定义二、STL实现三、二叉图的应用
二叉树相关(C语言描述)
评论列表
共有
0
条评论
发表评论
取消回复
登录
注册新账号
立即
投稿
返回
顶部
发表评论 取消回复