数据结构实验报告7.doc

上传人:laozhun 文档编号:2396640 上传时间:2023-02-17 格式:DOC 页数:8 大小:71.50KB
返回 下载 相关 举报
数据结构实验报告7.doc_第1页
第1页 / 共8页
数据结构实验报告7.doc_第2页
第2页 / 共8页
数据结构实验报告7.doc_第3页
第3页 / 共8页
数据结构实验报告7.doc_第4页
第4页 / 共8页
数据结构实验报告7.doc_第5页
第5页 / 共8页
点击查看更多>>
资源描述

《数据结构实验报告7.doc》由会员分享,可在线阅读,更多相关《数据结构实验报告7.doc(8页珍藏版)》请在三一办公上搜索。

1、武汉纺织大学数据结构实验报告班级: 信管 专业 班 姓名: 学号: 实验时间: 2016 年 5 月 6 日 指导教师: 宋泽源 实验七:线性查找操作与应用一、实验目的: 1、掌握顺序查找、折半查找的基本方法和操作过程 2、掌握二叉排序树的基本方法和操作过程3、掌握查找效率的分析方法二、实验内容:1、编写程序,实现顺序查找操作,可参考书本P260/P25示例程序。 实验步骤: 、在Java语言编辑环境中新建程序,建立一个顺序表(表长10),依次输入10个数据元素(对元素存放的先后顺序没有要求),并按照存储顺序输出所有元素; 、输入待查找关键字,在顺序表中进行顺序查找; 、输出查找结果。2、编写

2、程序,实现有序表折半查找操作,可参考书本P263/P218示例程序。 实验步骤: 、在Java语言编辑环境中新建程序,建立一个顺序表(表长10),依次输入10个数据元素(要求所有元素按照递增顺序排列),并按照存储顺序输出所有元素; 、输入待查找关键字,在有序表中进行折半查找; 、输出查找结果。3、编写程序,实现二叉排序树查找操作,可参考书本P277/P235示例程序。 实验步骤: 在Java语言编辑环境中新建程序,依次输入10个数据元素,建立一个二叉排序树,并按照中序遍历输出所有元素;、输入待查找关键字,在二叉排序树中进行查找; 、输出查找结果。三、操作步骤:实验1:package searc

3、h;import java.util.Scanner;public class Sequence public static void main(String args) throws java.io.IOExceptionSeqList list = new SeqList(10);int value=Sequence.readInt();for(int i=0;ivalue.length;i+)list.append(valuei);System.out.println(list.toString();System.out.println(输入要查找的数:);Scanner scan =

4、new Scanner(System.in);while(true)int key = scan.nextInt();System.out.println(list.search(key)+在数组中下标为+list.indexOf(key)+的位置);public static int readInt() throws java.io.IOExceptionSystem.out.println(输入10个数:);byte buffer=new byte512;int count =System.in.read(buffer);if(count2)return null;String s=new

5、 String(buffer,0,count-2);String str=s.split( );int value=new intstr.length;int i=0,j=0;while(istr.length)tryvaluej=Integer.parseInt(stri);j+;catch(NumberFormatException e)System.out.println(stri+不能转换为数组);finallyi+; if(i=j) return value; int keys=new intj; System.arraycopy(value, 0, keys, 0, j); ret

6、urn keys; 实验二package search;import java.util.Scanner;public class BinarySearch public static void main(String args)throws java.io.IOExceptionSeqList list = new SeqList(10);int value=BinarySearch.readInt();for(int i=0;ivalue.length;i+)list.append(valuei);System.out.println(list.toString();System.out.

7、println(使用折半查找方法,输入要查找的数:);Scanner scan = new Scanner(System.in);while(true)int key = scan.nextInt();System.out.println(key+在数组中的下标为+list.binarySearch(value, key);public static int readInt() throws java.io.IOExceptionSystem.out.println(输入10个升序数:);byte buffer=new byte512;int count =System.in.read(buf

8、fer);if(count2)return null;String s=new String(buffer,0,count-2);String str=s.split( );int value=new intstr.length;int i=0,j=0;while(imax)return -1;elseint mid=(max+min)/2;if(valuemid=key)return mid;else if(valuemidkey)return binarySearch(value,key,mid+1,max);elsereturn binarySearch(value,key,min,mi

9、d-1);实验三package search;import java.util.Scanner;public class BinarySortTree_ex public static void main(String args) throws java.io.IOExceptionBinarySortTree bstree=new BinarySortTree();int values=BinarySortTree_ex.readInt();for(int i=0;ivalues.length;i+)bstree.insert(new Integer(valuesi); /插入排序二叉树bs

10、tree.inOrder(); /中根次序遍历二叉树/System.out.println(中序遍历输出二叉排序树+bstree.toString();System.out.println(输入查找的数字:);Scanner scan = new Scanner(System.in);while(true)int key = scan.nextInt();System.out.println(查找+key+,+(bstree.search(key)!=null?:不)+成功);public static int readInt() throws java.io.IOExceptionSyste

11、m.out.println(输入10个数:);byte buffer=new byte512;int count =System.in.read(buffer);if(count2)return null;String s=new String(buffer,0,count-2);String str=s.split( );int value=new intstr.length;int i=0,j=0;while(istr.length)tryvaluej=Integer.parseInt(stri);j+;catch(NumberFormatException e)/System.out.println(stri+不能转换为数组);finallyi+; if(i=j) return value; int keys=new intj; System.arraycopy(value, 0, keys, 0, j); return keys; 四、实验收获和建议:条条大路通罗马。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 建筑/施工/环境 > 项目建议


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号