实验五: 树和二叉树实验
来源:互联网 发布:淘宝评论打不开 编辑:程序博客网 时间:2024/05/01 06:18
《数据结构》实验五: 树和二叉树实验
一..实验目的
巩固树和二叉树的相关知识,特别是二叉树的相关内容。学会运用灵活应用。
1.回树和二叉树的逻辑结构和存储方法,清楚掌握树和二叉树的遍历操作。
2.学习树的相关知识来解决实际问题。
3.进一步巩固程序调试方法。
4.进一步巩固模板程序设计。
二.实验时间
准备时间为第10周到第12前半周,具体集中实验时间为12周周四。2个学时。
三..实验内容
1.自己设计一个二叉树,深度最少为4,请递归算法分别用前序、中序、后序遍历输出树结点。
头文件
#ifndef BiTree_H#define BiTree_Hstruct BiNode{char data;BiNode *lchild, *rchild;};class BiTree{public:BiTree(){root=Creat(root);}~BiTree(){Release(root);}void PreOrder(){PreOrder(root);}void InOrder(){InOrder(root);}void PostOrder(){PostOrder(root);}private:BiNode *root;BiNode *Creat(BiNode *bt); void Release(BiNode *bt);void PreOrder(BiNode *bt);void InOrder(BiNode *bt);void PostOrder(BiNode *bt);};#endif
源文件
#include<iostream>using namespace std;#include "Bitree1.h"BiNode *BiTree::Creat (BiNode *bt){char ch;cout<<"请输入创建一棵二叉树的结点数据"<<endl;cin>>ch;if(ch=='#')return NULL;else{bt=new BiNode;bt->data =ch;bt->lchild =Creat(bt->lchild);bt->rchild =Creat(bt->rchild);}return bt;}void BiTree::Release (BiNode *bt){if(bt!=NULL){Release(bt->lchild);Release(bt->rchild);delete bt;}}void BiTree::PreOrder (BiNode *bt){if(bt==NULL)return;else{cout<<bt->data <<" ";PreOrder(bt->lchild);PreOrder(bt->rchild);}}void BiTree::InOrder (BiNode *bt){if(bt==NULL)return;else{ InOrder(bt->lchild); cout<<bt->data <<" "; InOrder(bt->rchild);}}void BiTree::PostOrder (BiNode *bt){if(bt==NULL)return;else{ PostOrder(bt->lchild); PostOrder(bt->rchild); cout<<bt->data <<" "; }}
主函数
#include<iostream>using namespace std;#include "Bitree1.h"int main(){BiTree T;cout<<"-----前序遍历-----"<<endl;T.PreOrder();cout<<endl;cout<<"-----中序遍历-----"<<endl;T.InOrder();cout<<endl;cout<<"-----后序遍历-----"<<endl;T.PostOrder();cout<<endl;return 0;}
效果截图
2.写程序判定出六枚硬币中的一枚假硬币。参照课本P136页8枚硬币的判定方法。
#include<iostream> using namespace std; //函数声明 void eightcoin(int arr[]); void compare(int a,int b,int real, int index1,int index2); void print(int h, int g, int i); int main() { int i = 0; int arr[6]; //这里输入a、b、c、d、e、f的重量 cout<<"请输入六枚硬币:"<<endl; for(i;i < 6; i++) { cin>>arr[i]; } eightcoin(arr); system("pause"); return 0; } /** * 六枚硬币问题描述: *,有一枚是假币,并且已知假币与真币的重量不同 *,但不知道假币与真币相比较轻还是较重。可以通过一架天平来任意比较两组硬币 *,设计一个高效的算法来检测出这枚假币 */ void eightcoin(int arr[]) { //1. 取数组中的前4个元素分为两组进行比较ab,cd //会有a+b > c+d | a+b == c+d| a+b < c+d 三种情况 int ab = arr[0] + arr[1] ; int cd = arr[2] + arr[3] ; int a = arr[0]; int b = arr[1]; int c = arr[2]; int d = arr[3]; int e = arr[4]; int f = arr[5]; if(ab > cd) //4枚硬币必有一枚假币 { if((a) > (d)) //去掉b,c,且a,d互换后,没有引起天平变化,说明假币必然是b,c中的一个 { if(a != c){ compare(a,c,e,0,2); }else{compare(b,d,e,1,3);} } else if ((a) == (d)) { compare(b,c,e,1,2); }else { compare(b,d,e,1,3); }} else if(ab == cd) //假币在e,f之中,最好状态 { if(e == a) { print(f,e,5); } else { print(e,f,4); } } else //ab < cd 这两组存在一枚假币,e,f为真币 { if((a) > (d)) { compare(b,d,e,1,3); } else if((a) == (d)) { compare(b,c,e,1,2); } else{if(a != c){compare(a,c,e,0,2);}else{compare(b,d,e,1,3); }} }} /** * 取出可能有一枚假币的两枚假币,作为参数a和参数b * real表示真币的重量,index1为第一枚硬币的下标,index2为第二枚硬币的下标 */ void compare(int a, int b,int real, int index1,int index2) { if(a == real) { print(b,real,index2); } else { print(a, real,index1); } } void print(int h, int g, int i) { if(h > g) { cout<<(i + 1)<<"是假币!"<<"且偏重!"<<endl; } else if(h < g){ cout<<(i + 1)<<"是假币!"<<"且偏轻!"<<endl; }else {cout<<"没有假币"<<endl;}}
效果截图
3. 自己确定2n(大小自己确定)个同学参加学校羽毛球淘汰比赛,请写程序,存放各队员比赛结果,并根据结果确定冠亚军。
第一题是必做题,第二题和第三题选做题。选做并做对者每题奖励5分。
四.参考资料实验教材P207到218
五.实验报告
1.在博客中先写上实习目的和内容,画出主要操作运算算法图,然后分别上传程序代码。插入调试关键结果截图。
2.写一个博文,比较总结树和二叉树的相关知识。
建议从知识点角度和应用角度两个方面各陈述。
0 0
- 实验五: 树和二叉树实验
- 实验五: 树和二叉树实验
- 实验五:树和二叉树实验
- 实验五: 树和二叉树实验
- 实验五: 树和二叉树实验
- 实验五:树和二叉树实验
- 实验五: 树和二叉树实验
- 实验五 二叉树
- 《数据结构》实验五: 树和二叉树实验
- 数据结构》实验五: 树和二叉树实验
- (实验五)《数据结构》第五章 树和二叉树实验
- 《数据结构》实验五: 树和二叉树实验
- 《数据结构》实验五: 树和二叉树实验
- 《数据结构》实验五: 树和二叉树实验总结
- 《数据结构》实验五: 树和二叉树实验
- 《数据结构》实验五: 树和二叉树实验报告
- 补 实验五:树和二叉树实验
- 《数据结构》实验五:树和二叉树实验
- SpringMVC返回JSON格式
- android:versionCode 不是点.
- 调查称过半新加坡年轻女性上传照片前会先修图
- 合并两列为一列
- 静电释放的危险
- 实验五: 树和二叉树实验
- linux shell编程 数组
- jquery积累
- Eclipse快捷键
- 在bat脚本中区分操作系统版本
- 为什么ListView的顶部和底部没有分隔线
- Failed to load JavaHL Library.
- spring中实现自己的定时器
- C# Dev PropertyGrid