实验五、二叉树操作及应用

一、 实验目的
掌握二叉树的定义、结构特征,以及各种存储结构的特点及使用范围,各种遍历算法。掌握用指针类型描述、访问和处理二叉树的运算。掌握前序或中序的非递归遍历算法。

二、 实验要求
有如下二叉树:

程序代码给出了该二叉树的链式存储结构的建立、前序、中序、后序遍历的算法,同时也给出了查询“E”是否在二叉树里的代码。代码有三处错误,有标识,属于逻辑错误,对照书中的代码仔细分析后,请修改了在电脑里运行。

#include <stdlib.h>
#include <stdio.h>

typedef char DataType;

typedef struct Node
{
DataType data;/数据域/
struct Node *leftChild;/左子树指针/
struct Node *rightChild;/右子树指针/
}BiTreeNode;/结点的结构体定义/

/初始化创建二叉树的头结点/
void Initiate(BiTreeNode **root)
{
*root = (BiTreeNode *)malloc(sizeof(BiTreeNode));
(*root)->leftChild = NULL;
(*root)->rightChild = NULL;
}

void Destroy(BiTreeNode **root)
{
if((*root) != NULL && (*root)->leftChild != NULL)
Destroy(&(*root)->leftChild);

if((*root) != NULL && (*root)->rightChild != NULL)
Destroy(&(*root)->rightChild);

free(*root);
}

/若当前结点curr非空,在curr的左子树插入元素值为x的新结点/
/原curr所指结点的左子树成为新插入结点的左子树/
/若插入成功返回新插入结点的指针,否则返回空指针/
BiTreeNode *InsertLeftNode(BiTreeNode *curr, DataType x)
{
BiTreeNode *s, *t;
if(curr == NULL) return NULL;

t = curr->leftChild;/保存原curr所指结点的左子树指针/
s = (BiTreeNode *)malloc(sizeof(BiTreeNode));
s->data = x;
s->leftChild = t;/新插入结点的左子树为原curr的左子树/
s->rightChild = NULL;

curr->leftChild = s;/新结点成为curr的左子树/
return curr->leftChild;/返回新插入结点的指针/
}

/若当前结点curr非空,在curr的右子树插入元素值为x的新结点/
/原curr所指结点的右子树成为新插入结点的右子树/
/若插入成功返回新插入结点的指针,否则返回空指针/
BiTreeNode *InsertRightNode(BiTreeNode *curr, DataType x)
{
BiTreeNode *s, *t;

if(curr == NULL) return NULL;

t = curr->rightChild;/保存原curr所指结点的右子树指针/
s = (BiTreeNode *)malloc(sizeof(BiTreeNode));
s->data = x;
s->rightChild = t;/新插入结点的右子树为原curr的右子树/
s->leftChild = NULL;

curr->rightChild = s;/新结点成为curr的右子树/
return curr->rightChild;/返回新插入结点的指针/
}

void PreOrder(BiTreeNode *t, void visit(DataType item))
//使用visit(item)函数前序遍历二叉树t
{
if(t != NULL)
{//此小段有一处错误
visit(t->data);
PreOrder(t-> rightChild, visit);
PreOrder(t-> leftChild, visit);
}
}

void InOrder(BiTreeNode *t, void visit(DataType item))
//使用visit(item)函数中序遍历二叉树t
{
if(t != NULL)
{//此小段有一处错误
InOrder(t->leftChild, visit);
InOrder(t->rightChild, visit);
visit(t->data);
}
}

void PostOrder(BiTreeNode *t, void visit(DataType item))
//使用visit(item)函数后序遍历二叉树t
{
if(t != NULL)
{//此小段有一处错误
visit(t->data);
PostOrder(t->leftChild, visit);
PostOrder(t->rightChild, visit);
}
}

void Visit(DataType item)
{
printf("%c ", item);
}

BiTreeNode *Search(BiTreeNode *root, DataType x)//需找元素x是否在二叉树中
{
BiTreeNode *find=NULL;
if(root!=NULL)
{
if(root->data == x)
find=root;
else
{
find=Search(root->leftChild,x);
if(find==NULL)
find=Search(root->rightChild,x);
}
}
return find;
}

void main(void)
{
BiTreeNode *root, *p, *pp,*find;
char x=‘E’;

Initiate(&root);
p = InsertLeftNode(root, ‘A’);
p = InsertLeftNode(p, ‘B’);
p = InsertLeftNode(p, ‘D’);
p = InsertRightNode(p, ‘G’);
p = InsertRightNode(root->leftChild, ‘C’);
pp = p;
InsertLeftNode(p, ‘E’);
InsertRightNode(pp, ‘F’);

printf(“前序遍历:”);
PreOrder(root->leftChild, Visit);
printf("\n中序遍历:");
InOrder(root->leftChild, Visit);
printf("\n后序遍历:");
PostOrder(root->leftChild, Visit);

find=Search(root,x);
if(find!=NULL)
printf("\n数据元素%c在二叉树中 \n",x);
else
printf("\n数据元素%c不在二叉树中 \n",x);

Destroy(&root);

}
三、 实验任务:
1.改正程序错误。
2.编写二叉树的前序(或中序)的非递归遍历算法并进行测试。


#include
using namespace std;
stack<BiTreeNode*> s;
BiTreeNode *p;
s.push§;
s.top();
s.pop();
s.empty()_
3.完成实验报告的撰写。

代码如下

#include <stdlib.h>
#include <stdio.h>typedef char DataType;typedef struct Node
{DataType data;/*数据域*/struct Node *leftChild;/*左子树指针*/struct Node *rightChild;/*右子树指针*/
} BiTreeNode; /*结点的结构体定义*//*初始化创建二叉树的头结点*/
void Initiate(BiTreeNode **root)
{*root = (BiTreeNode *)malloc(sizeof(BiTreeNode));(*root)->leftChild = NULL;(*root)->rightChild = NULL;
}void Destroy(BiTreeNode **root)
{if((*root) != NULL && (*root)->leftChild != NULL)Destroy(&(*root)->leftChild);if((*root) != NULL && (*root)->rightChild != NULL)Destroy(&(*root)->rightChild);free(*root);
}/*若当前结点curr非空,在curr的左子树插入元素值为x的新结点*/
/*原curr所指结点的左子树成为新插入结点的左子树*/
/*若插入成功返回新插入结点的指针,否则返回空指针*/
BiTreeNode *InsertLeftNode(BiTreeNode *curr, DataType x)
{BiTreeNode *s, *t;if(curr == NULL) return NULL;t = curr->leftChild;/*保存原curr所指结点的左子树指针*/s = (BiTreeNode *)malloc(sizeof(BiTreeNode));s->data = x;s->leftChild = t;/*新插入结点的左子树为原curr的左子树*/s->rightChild = NULL;curr->leftChild = s;/*新结点成为curr的左子树*/return curr->leftChild;/*返回新插入结点的指针*/
}/*若当前结点curr非空,在curr的右子树插入元素值为x的新结点*/
/*原curr所指结点的右子树成为新插入结点的右子树*/
/*若插入成功返回新插入结点的指针,否则返回空指针*/
BiTreeNode *InsertRightNode(BiTreeNode *curr, DataType x)
{BiTreeNode *s, *t;if(curr == NULL) return NULL;t = curr->rightChild;/*保存原curr所指结点的右子树指针*/s = (BiTreeNode *)malloc(sizeof(BiTreeNode));s->data = x;s->rightChild = t;/*新插入结点的右子树为原curr的右子树*/s->leftChild = NULL;curr->rightChild = s;/*新结点成为curr的右子树*/return curr->rightChild;/*返回新插入结点的指针*/
}void PreOrder(BiTreeNode *t, void visit(DataType item))
//使用visit(item)函数前序遍历二叉树t
{if(t != NULL){//此小段有一处错误  已改visit(t->data);PreOrder(t-> leftChild, visit);PreOrder(t-> rightChild, visit);}
}void InOrder(BiTreeNode *t, void visit(DataType item))
//使用visit(item)函数中序遍历二叉树t
{if(t != NULL){//此小段有一处错误 已改InOrder(t->leftChild, visit);visit(t->data);InOrder(t->rightChild, visit);}
}void PostOrder(BiTreeNode *t, void visit(DataType item))
//使用visit(item)函数后序遍历二叉树t
{if(t != NULL){//此小段有一处错误  已改PostOrder(t->leftChild, visit);PostOrder(t->rightChild, visit);visit(t->data);}
}void Visit(DataType item)
{printf("%c ", item);
}BiTreeNode *Search(BiTreeNode *root, DataType x)//需找元素x是否在二叉树中
{BiTreeNode *find=NULL;if(root!=NULL){if(root->data==x)find=root;else{find=Search(root->leftChild,x);if(find==NULL)find=Search(root->rightChild,x);}}return find;
}int main()
{BiTreeNode *root, *p, *pp,*find;char x='E';Initiate(&root);p = InsertLeftNode(root, 'A');p = InsertLeftNode(p, 'B');p = InsertLeftNode(p, 'D');p = InsertRightNode(p, 'G');p = InsertRightNode(root->leftChild, 'C');pp = p;InsertLeftNode(p, 'E');InsertRightNode(pp, 'F');printf("前序遍历:");PreOrder(root->leftChild, Visit);printf("\n中序遍历:");InOrder(root->leftChild, Visit);printf("\n后序遍历:");PostOrder(root->leftChild, Visit);find=Search(root,x);if(find!=NULL)printf("\n数据元素%c在二叉树中 \n",x);elseprintf("\n数据元素%c不在二叉树中 \n",x);Destroy(&root);}

第二题

#include <stdlib.h>
#include <stdio.h>
#define MAX 100typedef char DataType;typedef struct Node
{DataType data;/*数据域*/struct Node *leftChild;/*左子树指针*/struct Node *rightChild;/*右子树指针*/
} BiTreeNode; /*结点的结构体定义*//*初始化创建二叉树的头结点*/
void Initiate(BiTreeNode **root)
{*root = (BiTreeNode *)malloc(sizeof(BiTreeNode));(*root)->leftChild = NULL;(*root)->rightChild = NULL;
}void Destroy(BiTreeNode **root)
{if((*root) != NULL && (*root)->leftChild != NULL)Destroy(&(*root)->leftChild);if((*root) != NULL && (*root)->rightChild != NULL)Destroy(&(*root)->rightChild);free(*root);
}/*若当前结点curr非空,在curr的左子树插入元素值为x的新结点*/
/*原curr所指结点的左子树成为新插入结点的左子树*/
/*若插入成功返回新插入结点的指针,否则返回空指针*/
BiTreeNode *InsertLeftNode(BiTreeNode *curr, DataType x)
{BiTreeNode *s, *t;if(curr == NULL) return NULL;t = curr->leftChild;/*保存原curr所指结点的左子树指针*/s = (BiTreeNode *)malloc(sizeof(BiTreeNode));s->data = x;s->leftChild = t;/*新插入结点的左子树为原curr的左子树*/s->rightChild = NULL;curr->leftChild = s;/*新结点成为curr的左子树*/return curr->leftChild;/*返回新插入结点的指针*/
}/*若当前结点curr非空,在curr的右子树插入元素值为x的新结点*/
/*原curr所指结点的右子树成为新插入结点的右子树*/
/*若插入成功返回新插入结点的指针,否则返回空指针*/
BiTreeNode *InsertRightNode(BiTreeNode *curr, DataType x)
{BiTreeNode *s, *t;if(curr == NULL) return NULL;t = curr->rightChild;/*保存原curr所指结点的右子树指针*/s = (BiTreeNode *)malloc(sizeof(BiTreeNode));s->data = x;s->rightChild = t;/*新插入结点的右子树为原curr的右子树*/s->leftChild = NULL;curr->rightChild = s;/*新结点成为curr的右子树*/return curr->rightChild;/*返回新插入结点的指针*/
}//前序遍历非递归算法
void Prev(Node *root)
{Node *p,*node[MAX];int top=0;p=root;do{while(p!=NULL){printf("%c",p->data);node[top]=p;top++;p=p->leftChild;}if(top>0){top--;p=node[top];p=p->rightChild;}}while(top>0||p!=NULL);
}//中序遍历非递归算法
void min(Node *root)
{Node *p,*node[MAX];int top=0;p=root;do{while(p!=NULL){node[top]=p;top++;p=p->leftChild;}if(top>0){top--;p=node[top];printf("%c",p->data);p=p->rightChild;}}while(top>0||p!=NULL);
}BiTreeNode *Search(BiTreeNode *root, DataType x)//需找元素x是否在二叉树中
{BiTreeNode *find=NULL;if(root!=NULL){if(root->data==x)find=root;else{find=Search(root->leftChild,x);if(find==NULL)find=Search(root->rightChild,x);}}return find;
}int main(void)
{BiTreeNode *root, *p, *pp,*find;char x='E';Initiate(&root);p = InsertLeftNode(root, 'A');p = InsertLeftNode(p, 'B');p = InsertLeftNode(p, 'D');p = InsertRightNode(p, 'G');p = InsertRightNode(root->leftChild, 'C');pp = p;InsertLeftNode(p, 'E');InsertRightNode(pp, 'F');printf("前序遍历:");Prev(root->leftChild);printf("\n中序遍历:");min(root->leftChild);find=Search(root,x);if(find!=NULL)printf("\n数据元素%c在二叉树中 \n",x);elseprintf("\n数据元素%c不在二叉树中 \n",x);Destroy(&root);
}

求赞!!!

数据结构实验课:实验五、二叉树操作及应用相关推荐

  1. 吉林大学单片机实验课实验五——重量测量

    主要数模转换和点阵液晶屏显示屏,距离上次更新时间有点久,今天把所有的实验全部更新完,实话说啊,这种不加注释的代码,虽然每个命令都是我亲手写的,但是隔了这么久,第一眼看我也是有点懵的,所以从这篇教程开始 ...

  2. python实验课_#017 python实验课第五周

    总结写在最前面: 1.语法还是不会...(每周强制留的C语言一百题都没空写PS.团委诶....)都是现查现用(莫凡Python这个网站特别好用知识点一个视频就一分钟B站的播放器没广告,用啥学啥,还配有 ...

  3. 《数据挖掘导论》实验课——实验一、数据处理之Numpy

    实验一.数据处理之Numpy 一.实验目的 1. 了解numpy库的基本功能 2. 掌握Numpy库的对数组的操作与运算 二.实验工具: 1. Anaconda 2. Numpy 三.Numpy简介 ...

  4. 华中科技大学操作系统实验课 实验四

    一.实验目的 (1)理解设备是文件的概念. (2)掌握Linux模块.驱动的概念和编程流程 (3)Windows /Linux下掌握文件读写基本操作 二.实验内容 (1)编写一个Linux内核模块,并 ...

  5. 微机实验课-实验四扬声器程序设计

    微机实验四操作参考 2018年11月 William 〇.实验准备 本次实验是设计汇编程序,控制8253和8255的工作原理及其应用编程.8253为微机系统中使用的定时/计数器,8255为并行接口,即 ...

  6. 华中科技大学操作系统实验课 实验三

    一.实验目的 (1)理解页面淘汰算法原理,编写程序演示页面淘汰算法. (2)验证Linux虚拟地址转化为物理地址的机制 (3)理解和验证程序运行局部性的原理. (4)理解和验证缺页处理的流程. 二.实 ...

  7. 如何用matlab画nyqist,机械控制工程基础实验课实验报告

    试验一数学模型的Matlab描述 一.实验目的 ①掌握Malab中数学模型的三种表现形式 ②掌握三种模型之间的转换方法 ③掌握复杂传递函数的求取方法 ④了解复杂系统表现形式及建模方法 二.实验要求 ① ...

  8. 实验五 二叉树的递归及非递归的遍历及其应用

    实验目的 熟练掌握二叉树的二叉链表存储结构的C语言实现.掌握二叉树的基本操作-前序.中序.后序遍历二叉树的三种方法.了解非递归遍历过程中"栈"的作用和状态,而且能灵活运用遍历算法实 ...

  9. 数据结构实验二 树和二叉树的实现

    广州大学学生实验报告 开课实验室:计算机科学与工程实验(电子楼418A)     2019年5月13日 学院 计算机科学与教育软件学院 年级.专业.班 计算机科学与技术172班 姓名 学号 17061 ...

最新文章

  1. 英文版PDF不能显示中文PDF文件的解决方法
  2. Part 1 – Reverse engineering using Androguard
  3. Js中for循环的阻塞机制
  4. php mysql文件缓存_PHP文件缓存类实现代码
  5. python3 规则引擎_几个常见规则引擎的简单介绍和演示
  6. Myecplise Tomcat 启动很慢
  7. 编程语言对比 条件控制语句
  8. linux忘记管理员密码,如何登陆?
  9. 算法选择_快速筛出topK的快速选择算法
  10. java - rest-assured 接口测试
  11. pytorh——Fully-connected
  12. matlab求滤波器的相频特性,matlab仿真一阶低通滤波器幅频特性和相频特性.docx
  13. 【MATLAB统计分析与应用100例】案例015:matlab读取Excel数据,进行值聚类分析
  14. UniBeast:在任何支持基于英特尔处理器的PC上安装OS X优胜美地
  15. cesium多边形描边_cesium--绘制多边形polygon
  16. i3 10105f对比i5 10400f选哪个好
  17. 如何拟合幂率分布的幂率
  18. 网络安全理论与技术概述-带你了解网络安全
  19. mindspore| lenet模型 推理过程记录
  20. 索尼ps4 linux,索尼PS4

热门文章

  1. linux-nginx部署
  2. Matlab绘图常用设置及函数
  3. 输入一个字符串,对该字符串进行逆序,输出逆序后的字符串。
  4. 【Java】 杨辉三角 二维数组打印杨辉三角
  5. 干货!Flask 动态展示 Pyecharts 图表数据的几种方法!
  6. 【深度学习入门】- 神经网络
  7. python科学计数法的显示与转换
  8. JQuery中append()方法的使用
  9. 利用snpEff对基因型VCF文件进行变异注释的详细方法
  10. 【蓝桥杯】最长子序列