[实验五] 二叉树的基本操作
来源:互联网 发布:linux 执行sql脚本 编辑:程序博客网 时间:2024/05/16 13:38
四、实验内容
(1) 先序创建二叉树;
(2) 分别实现二叉树的先序、中序、后序递归遍历;
(3) 统计二叉树的结点数和叶结点数;
*(4) 计算二叉树的深度;
*(5) 按值查找二叉树的指定结点;
(6) 销毁二叉树。
#include<stdio.h>
#include<stdlib.h>
#define MAX_TREE_SIZE 100
typedef struct BiTNode{
char data;
struct BiTNode *lchild,*rchild;
struct BiTNode *next;
}BiTNode,*BiTree;
//以先序序列构建二叉树
void Createinitial(BiTree *L){
char n;
scanf("%c",&n);
if(n==' ')
*L=NULL;
else
{
*L=(BiTree)malloc(sizeof(BiTree));
(*L)->data=n;
Createinitial(&(*L)->lchild);
Createinitial(&(*L)->rchild);
}
}
void visit(char n,int level){
printf("%c位于第%d层\n",n,level);
}
int Create1Bitree(BiTree L,int level){
if(L)
{
visit(L->data,level);
Create1Bitree(L->lchild,level+1);
Create1Bitree(L->rchild,level+1);
}
}
void Create2Bitree(BiTree L,int level){
if(L)
{
Create2Bitree(L->lchild,level+1);
visit(L->data,level);
Create2Bitree(L->rchild,level+1);
}
}
void Create3Bitree(BiTree L,int level){
if(L)
{
Create3Bitree(L->lchild,level+1);
Create3Bitree(L->rchild,level+1);
visit(L->data,level);
}
}
int Destrony(BiTree L){
if(L)
{
if(L->rchild)
{
free(L);
Destrony(L->lchild);
}
else
{
free(L);
Destrony(L->rchild);
}
}
return 0;
}
int main()
{
BiTree L=NULL;
int level=1;
printf("给二叉树的各结点赋值");
Createinitial(&L);
printf("按先序序列访问时打印出各结点的层数为:\n");
Create1Bitree(L,level);
printf("按中序序列访问时打印出各结点的层数为:\n");
Create2Bitree(L,level);
printf("按后序序列访问时打印出各结点的层数为:\n");
Create3Bitree(L,level);
Destrony(L);
if(!Destrony(L));
printf("\n改二叉树销毁成功");
return 0;
}
- 实验五:二叉树的基本操作
- [实验五] 二叉树的基本操作
- 实验五 二叉树的操作
- 实验 二叉树的基本操作
- 南邮数据结构实验6.1二叉树的基本操作
- 数据结构实验2——二叉树的基本操作
- 实验四 二叉树基本操作的实现
- 实验五 二叉树
- 实验五 二叉树的实现
- 数据结构 实验五 二叉树操作
- 实验五 树和二叉树的实验1
- 实验五 树和二叉树的实验1
- 实验五 树和二叉树的实验1
- 二叉树的基本运算实验
- 二叉树的基本运算实验
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- phpstrom地址栏解决问题
- Java运用中,主要是集合处理的一点小结
- 更换插件图标的细节
- 将C#文档注释生成.chm帮助文档
- 机器学习笔记02-代价函数与梯度下降算法(一)
- [实验五] 二叉树的基本操作
- Spring MVC 的validation校验
- 秒懂-mysql数据库的左连接,右连接,内链接
- 卡尔曼滤波及其java实现
- Python读写配置文件
- Reactor模式,或者叫反应器模式
- IOS开发经常用到齐全控件
- 线程基础学习第一篇(多个线程竞争一把锁)
- centos 安装git