树的遍历及应用
来源:互联网 发布:php画三角形星星 编辑:程序博客网 时间:2024/05/29 09:04
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
char data;
struct Node * LChild;
struct Node * RChild;
}BiTNode, *BiTree;
void CreateBiTree(BiTree *bt)
{
char ch;
ch=getchar();
if(ch=='.') *bt=NULL;
else
{
*bt=(BiTree)malloc(sizeof(BiTNode));
(*bt)->data=ch;
CreateBiTree(&((*bt)->LChild));
CreateBiTree(&((*bt)->RChild));
}
}
//先序遍历
void PreOrder(BiTree root)
{
if (root!=NULL)
{
printf("%c ",root ->data);
PreOrder(root ->LChild);
PreOrder(root ->RChild);
}
}
//中序遍历
void InOrder(BiTree root)
{
if(root != NULL)
{
InOrder(root->LChild);
printf("%c ",root ->data);
InOrder(root->RChild);
}
}
//后序遍历
void PostOrder(BiTree root)
{
if(root!=NULL)
{
PostOrder(root ->LChild);
PostOrder(root ->RChild);
printf("%c ",root ->data);
}
}
//统计叶子结点数目
int leaf(BiTree root)
{
int LeafCount;
if(root==NULL)
LeafCount =0;
else if ((root->LChild==NULL)&&(root->RChild==NULL))
LeafCount =1;
else
LeafCount =leaf(root->LChild)+leaf(root->RChild);
return LeafCount;
}
//求二叉树高度
int PostTreeDepth(BiTree bt)
{
int hl, hr, max;
if(bt!=NULL)
{
hl=PostTreeDepth(bt->LChild);
hr=PostTreeDepth(bt->RChild);
max=hl>hr?hl:hr;
return(max+1);
}
else return(0);
}
int main()
{
printf("请输入:");
int Case=10;
while(Case--)
{
BiTree tree;
CreateBiTree(&tree);
printf("\nPreOrder:\n");
PreOrder(tree);
printf("\nInOrder:\n");
InOrder(tree);
printf("\nPostOrder:\n");
PostOrder(tree);
printf("\n叶子结点数目:%d\n",leaf(tree));
printf("\n二叉树的高度:%d\n",PostTreeDepth(tree));
}
}
#include <stdlib.h>
typedef struct Node
{
char data;
struct Node * LChild;
struct Node * RChild;
}BiTNode, *BiTree;
void CreateBiTree(BiTree *bt)
{
char ch;
ch=getchar();
if(ch=='.') *bt=NULL;
else
{
*bt=(BiTree)malloc(sizeof(BiTNode));
(*bt)->data=ch;
CreateBiTree(&((*bt)->LChild));
CreateBiTree(&((*bt)->RChild));
}
}
//先序遍历
void PreOrder(BiTree root)
{
if (root!=NULL)
{
printf("%c ",root ->data);
PreOrder(root ->LChild);
PreOrder(root ->RChild);
}
}
//中序遍历
void InOrder(BiTree root)
{
if(root != NULL)
{
InOrder(root->LChild);
printf("%c ",root ->data);
InOrder(root->RChild);
}
}
//后序遍历
void PostOrder(BiTree root)
{
if(root!=NULL)
{
PostOrder(root ->LChild);
PostOrder(root ->RChild);
printf("%c ",root ->data);
}
}
//统计叶子结点数目
int leaf(BiTree root)
{
int LeafCount;
if(root==NULL)
LeafCount =0;
else if ((root->LChild==NULL)&&(root->RChild==NULL))
LeafCount =1;
else
LeafCount =leaf(root->LChild)+leaf(root->RChild);
return LeafCount;
}
//求二叉树高度
int PostTreeDepth(BiTree bt)
{
int hl, hr, max;
if(bt!=NULL)
{
hl=PostTreeDepth(bt->LChild);
hr=PostTreeDepth(bt->RChild);
max=hl>hr?hl:hr;
return(max+1);
}
else return(0);
}
int main()
{
printf("请输入:");
int Case=10;
while(Case--)
{
BiTree tree;
CreateBiTree(&tree);
printf("\nPreOrder:\n");
PreOrder(tree);
printf("\nInOrder:\n");
InOrder(tree);
printf("\nPostOrder:\n");
PostOrder(tree);
printf("\n叶子结点数目:%d\n",leaf(tree));
printf("\n二叉树的高度:%d\n",PostTreeDepth(tree));
}
}
阅读全文
0 0
- 树的遍历及应用
- 树的建立,遍历,及相关应用
- 树的实现遍历及应用
- 二叉树的遍历及应用
- Map 的遍历过程及应用实例
- 二叉树遍历及简单应用
- 二叉树的遍历应用
- 二叉树遍历的应用
- 遍历二叉树的应用
- 二叉树遍历的应用
- 树及树的遍历
- 树及树的遍历
- 树及树的遍历
- 树及树的遍历
- 树的存储及遍历
- 二叉树及其应用-二叉树的遍历及哈夫曼树-数据结构上机实验
- 数据结构与算法专题之树——二叉树的遍历及应用
- 二叉树的三种遍历方式及应用(java实现)
- 编程练习之栈篇:进制转换
- 生成对抗网络(GANs)的资料小结,另附:资源|17类对抗网络经典论文及开源代码(附源码)
- spring cloud之zuul路由网关(六)
- 导出Excel
- 知识点累计
- 树的遍历及应用
- Android View的工作流程
- [JZOJ2784][BZOJ2434]【NOI2011】阿狸的打字机
- 一.Zookeeper三节点集群环境搭建(centOS)
- Constraint Layout(二)--添加和调整元素
- JS获取当前时间戳的方法
- Linux Kettle 闪退问题解决方案
- weblogic怎么修改长长的项目名访问
- npm修改源