JAVA数据结构习题及解答(英) GOOD

合集下载

数据结构JAVA语言描述习题答案(刘小晶等主编).pdf总复习

数据结构JAVA语言描述习题答案(刘小晶等主编).pdf总复习
设定哈希函数 H(key) = key MOD 11 ( 表长=11 ) 若采用二次探测再散列处理冲突
0 1 2 3 4 5 6 7 8 9 10
55 01 23 14 36 82 68
19
11
3
1 1 2 1 2 1 4 1 ASL(成功)= (1*5+2*1+3+4)/9=14/9
3 V4 4 V5 5 V6
1 5 5 4
4
广度优先搜索法遍历序列: V1,V2,V3,V4,V5,V6
2
3
例题: 设有如下的两个网络, 分别用普里姆(Prim)算法 和克鲁斯卡尔(Kruskal)算法具体构造相应的最小生 成树。 写出过程。 a 5 6 1
b
5 6
c
6
5 4
d
3
2
[例1]假设按低下标优先存储整数数组 A9×3×5×8时, 第一个元素的字节地址是100,每个整数占 四个字节,问元素a3125的地址是什么? LOC(a3125)= ? 100+(3×3×5×8+1×5×8+2×8+5)×4 =1784
[ 例 2] 设有数组 A[1..8,1..10] ,数组的每个元素占 3 字节,数组从内存首地址 BA开始以列序为主序顺 序存放,求数组元素 a[5,8]的存储首地址.
8 9 10
55 01 23 14 68 11 82 36 19
1 1 2 1 3 6 2 5 1 查找次数 ASL(成功)=(4*1+2*2+3+5+6)/9=22/9
ASL(不成功)=(10+9+…+1+1)/11=56/11
例如: 关键字集合 { 19, 01, 23, 14, 55, 68, 11, 82, 36 }

数据结构Java版第二章习题

数据结构Java版第二章习题

数据结构Java版第二章习题在学习数据结构的过程中,第二章的习题往往是巩固和深化对相关概念理解的重要环节。

这一章可能涵盖了诸如数组、链表、栈、队列等基本数据结构的知识。

先来说说数组,这是一种最简单也最常见的数据结构。

在 Java 中,数组是一组相同类型元素的有序集合。

它的优点很明显,比如可以通过索引快速访问元素,查找操作的时间复杂度为 O(1)。

但缺点也不能忽视,比如数组的长度固定,插入和删除元素可能需要移动大量元素,导致时间复杂度较高。

来看一道关于数组的习题:给定一个整数数组,找出其中出现次数最多的元素。

这就需要我们遍历数组,使用一个辅助的数据结构(比如哈希表)来记录每个元素出现的次数,最后找出出现次数最多的那个元素。

再说说链表,链表是由一系列节点组成的,每个节点包含数据和指向下一个节点的引用。

相比于数组,链表在插入和删除操作上具有优势,时间复杂度为 O(1)。

但查找操作的时间复杂度为 O(n)。

有这样一道链表的习题:实现一个链表的反转。

这就需要我们改变链表节点的指针指向,依次将每个节点的 next 指针指向前一个节点,从而实现链表的反转。

接下来是栈,栈是一种特殊的线性表,遵循后进先出(LIFO)的原则。

在 Java 中,可以使用数组或者链表来实现栈。

比如有这样一道关于栈的习题:判断一个表达式中的括号是否匹配。

我们可以将左括号入栈,遇到右括号时弹出栈顶的左括号进行匹配,如果最后栈为空,则括号匹配成功。

然后是队列,队列遵循先进先出(FIFO)的原则。

在实际应用中,比如排队系统、任务调度等都可以用到队列。

例如,有这样一道队列的习题:实现一个循环队列。

这就需要我们处理好队列的头尾指针,以及队列满和空的判断条件。

在解决这些习题的过程中,我们不仅要熟练掌握各种数据结构的特点和操作方法,还要具备良好的逻辑思维和代码实现能力。

同时,通过不断地练习和思考,我们能够更加深入地理解数据结构的本质,为今后解决更复杂的问题打下坚实的基础。

数据结构习题和答案及解析

数据结构习题和答案及解析

第 1 章绪论课后习题讲解1. 填空⑴()是数据的基本单位,在计算机程序中通常作为一个整体进行考虑和处理。

【解答】数据元素⑵()是数据的最小单位,()是讨论数据结构时涉及的最小数据单位。

【解答】数据项,数据元素【分析】数据结构指的是数据元素以及数据元素之间的关系。

⑶从逻辑关系上讲,数据结构主要分为()、()、()和()。

【解答】集合,线性结构,树结构,图结构⑷数据的存储结构主要有()和()两种基本方法,不论哪种存储结构,都要存储两方面的内容:()和()。

【解答】顺序存储结构,链接存储结构,数据元素,数据元素之间的关系⑸算法具有五个特性,分别是()、()、()、()、()。

【解答】有零个或多个输入,有一个或多个输出,有穷性,确定性,可行性⑹算法的描述方法通常有()、()、()和()四种,其中,()被称为算法语言。

【解答】自然语言,程序设计语言,流程图,伪代码,伪代码⑺在一般情况下,一个算法的时间复杂度是()的函数。

【解答】问题规模⑻设待处理问题的规模为n,若一个算法的时间复杂度为一个常数,则表示成数量级的形式为(),若为n*log25n,则表示成数量级的形式为()。

【解答】Ο(1),Ο(nlog2n)【分析】用大O记号表示算法的时间复杂度,需要将低次幂去掉,将最高次幂的系数去掉。

2. 选择题⑴顺序存储结构中数据元素之间的逻辑关系是由()表示的,链接存储结构中的数据元素之间的逻辑关系是由()表示的。

A 线性结构B 非线性结构C 存储位置D 指针【解答】C,D【分析】顺序存储结构就是用一维数组存储数据结构中的数据元素,其逻辑关系由存储位置(即元素在数组中的下标)表示;链接存储结构中一个数据元素对应链表中的一个结点,元素之间的逻辑关系由结点中的指针表示。

⑵假设有如下遗产继承规则:丈夫和妻子可以相互继承遗产;子女可以继承父亲或母亲的遗产;子女间不能相互继承。

则表示该遗产继承关系的最合适的数据结构应该是()。

数据结构总复习题(JAVA)

数据结构总复习题(JAVA)

一、填空题1. 栈和队列的共同特点是(只允许在端点处插入和删除元素)。

2. 在深度为5的满二叉树中,叶子结点的个数为(31)3. 算法分析的目的是(分析算法的效率以求改进)。

4. 由两个栈共享一个存储空间的好处是(节省存储空间,降低上溢发生的机率)。

5.串的长度是(串中所含字符的个数)。

6.设有两个串p和q,求q在p中首次出现位置的运算称做(模式匹配)7. N个顶点的连通图中边的条数至少为(N-1)。

8.N个顶点的强连通图的边数至少有(N)。

9.对长度为n的线性表进行顺序查找,在最坏情况下所需要的比较次数为(N)。

P25910.假设线性表的长度为n,则在最坏情况下,冒泡排序需要的比较次数为(n(n-1)/2)。

P29211. 在n个结点的单链表中要删除已知结点*p,需找到它的前驱结点的地址,其时间复杂度为O(n)。

12. 在具有n个单元的循环队列中,队满时共有 n-1 个元素。

13. 有向图G用邻接表矩阵存储,其第i行的所有元素之和等于顶点i的出度。

14. 用Dijkstra算法求某一顶点到其余各顶点间的最短路径是按路径长度递增的次序来得到最短路径的。

15. 在图形结构中,每个结点的前驱结点数和后续结点数可以任意多个。

16.在一个循环队列中,队首指针指向队首元素的前一个位置。

17.在顺序表中插入或删除一个元素,需要平均移动表中一半元素,具体移动的元素个数与表长和该元素在表中的位置有关。

18. 线性表中结点的集合是有限的,结点间的关系是一对一的。

19.数据结构被形式地定义为(D, R),其中D是数据元素的有限集合,R是D上的关系有限集合。

20. 线性结构中元素之间存在一对一关系,树形结构中元素之间存在一对多关系,图形结构中元素之间存在多对多关系。

21. 一个算法的效率可分为时间效率和空间效率。

22. 在顺序表中访问任意一结点的时间复杂度均为 O(1) ,因此,顺序表也称为随机存取的数据结构。

23. 在n个结点的单链表中要删除已知结点*p,需找到它的前驱结点的地址,其时间复杂度为O(n)。

数据结构与算法分析java课后答案

数据结构与算法分析java课后答案

数据结构与算法分析java课后答案【篇一:java程序设计各章习题及其答案】>1、 java程序是由什么组成的?一个程序中必须有public类吗?java源文件的命名规则是怎样的?答:一个java源程序是由若干个类组成。

一个java程序不一定需要有public类:如果源文件中有多个类时,则只能有一个类是public类;如果源文件中只有一个类,则不将该类写成public也将默认它为主类。

源文件命名时要求源文件主名应与主类(即用public修饰的类)的类名相同,扩展名为.java。

如果没有定义public类,则可以任何一个类名为主文件名,当然这是不主张的,因为它将无法进行被继承使用。

另外,对applet小应用程序来说,其主类必须为public,否则虽然在一些编译编译平台下可以通过(在bluej下无法通过)但运行时无法显示结果。

2、怎样区分应用程序和小应用程序?应用程序的主类和小应用程序的主类必须用public修饰吗?答:java application是完整的程序,需要独立的解释器来解释运行;而java applet则是嵌在html编写的web页面中的非独立运行程序,由web浏览器内部包含的java解释器来解释运行。

在源程序代码中两者的主要区别是:任何一个java application应用程序必须有且只有一个main方法,它是整个程序的入口方法;任何一个applet小应用程序要求程序中有且必须有一个类是系统类applet的子类,即该类头部分以extends applet结尾。

应用程序的主类当源文件中只有一个类时不必用public修饰,但当有多于一个类时则主类必须用public修饰。

小应用程序的主类在任何时候都需要用public来修饰。

3、开发与运行java程序需要经过哪些主要步骤和过程?答:主要有三个步骤(1)、用文字编辑器notepad(或在jcreator,gel, bulej,eclipse, jbuilder等)编写源文件;(2)、使用java编译器(如javac.exe)将.java源文件编译成字节码文件.class;(3)、运行java程序:对应用程序应通过java解释器(如java.exe)来运行,而对小应用程序应通过支持java标准的浏览器(如microsoft explorer)来解释运行。

《数据结构(Java版)叶核亚(第4版)》样卷及答案

《数据结构(Java版)叶核亚(第4版)》样卷及答案

《数据结构(Java 版)》课程样卷教材:《数据结构(Java 版)(第4版)》,叶核亚编著,电子工业出版社,2015年7月出版。

试题范围:第1~9章,掌握基础原理,熟悉经典算法,问答题形式考核。

编程题重点是:1.单/双链表; 2.二叉树/树,递归算法。

这是必须掌握的,即使部分学生掌握不了递归算法,也必须考。

不考内容:6.3 线索二叉树求父母、插入、删除算法(没写),7.5.2 Floyd ,8.5.3 平衡二叉树,第10章。

可作为课程设计题。

试卷范围和难度不超过样卷。

4-0 模拟样卷一、 填空题(16分=2分×8题)1. 声明抽象数据类型的目的是________________________________________。

2. 以下数据存储结构声明为_________________________________________。

table Node<T>……3. 已知ng.String 类声明以下成员方法:public String replaceAll(String pattern, String str) //将所有与pattern 匹配的子串替换为str下列语句的执行结果是________________________________________。

String target="aababbabac", pattern="ab", str="aba";System.out.println("\""+target+"\".replaceAll(\""+pattern+"\", \""+str+"\")=\""+ target.replaceAll(pattern,str)+"\"");4. A+B*(C-D*(E+F)/G+H)-(I+J)*K 的后缀表达式为______________________。

java课后习题及答案

java课后习题及答案Java课后习题及答案Java作为一门广泛应用于软件开发领域的编程语言,其学习和掌握对于计算机专业的学生来说是非常重要的。

在学习过程中,课后习题是巩固知识、提高编程能力的重要环节。

本文将为大家提供一些常见的Java课后习题及其答案,希望能对大家的学习有所帮助。

一、基础习题1. 编写一个Java程序,输出"Hello, World!"。

```javapublic class HelloWorld {public static void main(String[] args) {System.out.println("Hello, World!");}}```2. 编写一个Java程序,计算并输出1到100之间的所有偶数的和。

```javapublic class SumOfEvenNumbers {public static void main(String[] args) {int sum = 0;for (int i = 2; i <= 100; i += 2) {sum += i;System.out.println("1到100之间的所有偶数的和为:" + sum); }}```3. 编写一个Java程序,判断一个整数是否是素数。

```javapublic class PrimeNumber {public static void main(String[] args) {int num = 17;boolean isPrime = true;for (int i = 2; i <= Math.sqrt(num); i++) {if (num % i == 0) {isPrime = false;break;}}if (isPrime) {System.out.println(num + "是素数");} else {System.out.println(num + "不是素数");}}```二、进阶习题1. 编写一个Java程序,实现一个简单的计算器,能够进行加、减、乘、除四则运算。

数据结构Java版第二章习题

(依据自己的状况选作部分习题,不要剽窃)第二章习题次序储存线性表一判断题1.线性表的逻辑次序与储存次序老是一致的。

×2.次序储存的线性表能够按次号随机存取。

√3.次序表的插入和删除操作不需要付出很大的时间代价,由于每次操作均匀只有近一半的元素需要挪动。

×4.线性表中的元素能够是各种各种的,但同一线性表中的数据元素拥有同样的特征,所以是属于同一数据对象。

√5.在线性表的次序储存构造中,逻辑上相邻的两个元素在物理地点上其实不必定紧邻。

×6.在线性表的次序储存构造中,插入和删除时,挪动元素的个数与该元素的地点相关。

√二单项选择题 ( 请从以下 A,B,C,D 选项中选择一项 )1.线性表是 ( A )。

(A)一个有限序列,能够为空;(B)一个有限序列,不可认为空;(C)一个无穷序列,能够为空;(D)一个无序序列,不可认为空。

2.对次序储存的线性表,设其长度为n,在任何地点上插入或删除操作都是等概率的。

插入一个元素时均匀要挪动表中的( A )个元素。

(A) n/2(B) n+1/2(C) n -1/2(D) n三填空题1.在次序表中做插入操作时第一检查___表能否满了 ______________。

四算法设计题1.设线性表寄存在向量A[arrsize]的前elenum个重量中,且递加有序。

试写一算法,将x插入到线性表的适合地点上,以保持线性表的有序性。

而且剖析算法的时间复杂度。

2.已知一次序表A,其元素值非递减有序摆列,编写一个函数删除次序表中剩余的值同样的元素。

3.编写一个函数,从一给定的次序表A 中删除值在 x~y(x<=y) 之间的所有元素,要求以较高的效率来实现。

提示:能够先将序表中所有在 x~y 之的元素置成一个特别的,其实不立刻除它,而后从最后向前挨次描,拥有特别的元素后,移后来面的元素将其除去。

4.性表中有 n 个元素,每个元素是一个字符,存于向量R[n] 中,写一算法,使 R 中的字符按字母字符、数字字符和其余字符的序摆列。

数据结构与算法分析java课后答案

数据结构与算法分析java课后答案数据结构和算法分析java课后答案【篇一:java程序设计各章习题及其答案】>1、 java程序是由什么组成的?一个程序中必须有public类吗?java源文件的命名规则是怎样的?答:一个java源程序是由若干个类组成。

一个java程序不一定需要有public类:如果源文件中有多个类时,则只能有一个类是public 类;如果源文件中只有一个类,则不将该类写成public也将默认它为主类。

源文件命名时要求源文件主名应和主类(即用public修饰的类)的类名相同,扩展名为.java。

如果没有定义public类,则可以任何一个类名为主文件名,当然这是不主张的,因为它将无法进行被继承使用。

另外,对applet小使用程序来说,其主类必须为public,否则虽然在一些编译编译平台下可以通过(在bluej下无法通过)但运行时无法显示结果。

2、怎样区分使用程序和小使用程序?使用程序的主类和小使用程序的主类必须用public修饰吗?答:java application是完整的程序,需要独立的解释器来解释运行;而java applet则是嵌在html编写的web页面中的非独立运行程序,由web浏览器内部包含的java解释器来解释运行。

在源程序代码中两者的主要区别是:任何一个java application使用程序必须有且只有一个main方法,它是整个程序的入口方法;任何一个applet小使用程序要求程序中有且必须有一个类是系统类applet 的子类,即该类头部分以extends applet结尾。

使用程序的主类当源文件中只有一个类时不必用public修饰,但当有多于一个类时则主类必须用public修饰。

小使用程序的主类在任何时候都需要用public来修饰。

3、开发和运行java程序需要经过哪些主要步骤和过程?答:主要有三个步骤(1)、用文字编辑器notepad(或在jcreator,gel, bulej,eclipse,jbuilder等)编写源文件;(2)、使用java编译器(如javac.exe)将.java源文件编译成字节码文件.class;(3)、运行java程序:对使用程序应通过java解释器(如java.exe)来运行,而对小使用程序应通过支持java标准的浏览器(如microsoft explorer)来解释运行。

Java数据结构习题

第一章 绪论数据结构研究内容:数据的逻辑结构、存储结构及其操作 逻辑结构的分类:两大类分为线性结构、非线性结构;三大类分为线性结构、树、图 物理结构:顺序存储和链式存储(特点、优缺点...) 1、 对存储的连续性要求:顺序存储要求连续;链式存储连续与否均可 2、 存储密度:顺序存储的存储密度大,可达100%;链式存储存储密度较小 3、 对元素的访问:顺序存储比较方便。顺序存储是随机访问;链式存储是顺序访问 4、 删除和插入操作:链式存储的删除和插入操作较简单 算法分析的两个方面:时间复杂度、空间复杂度 程序段的时间复杂度

第二章 线性表(重点) 线性表的基本概念 习题:线性表L=(a1,a2,……,an),下列说法正确的是( )。 A. 每个元素都有一个直接前驱和一个直接后继 B. 线性表中至少要有一个元素 C. 表中诸元素的排列顺序必须是由小到大或由大到小 D. 除第一个和最后一个元素外,其余每个元素都由一个且仅有一个直接前驱和直接后继

线性表的两种存储方式 习题: 1、链表不具有的特点是( )。 A. 可随机访问任一元素 B. 插入删除不需要移动元素 C. 不必事先估计存储空间 D. 所需空间与线性表长度成正比 2、线性表采用链式存储时,结点的存储地址( )。 A. 必须是连续的 B. 必须是不连续的 C. 连续与否均可 D. 和头结点的存储地址相连续 3、在以下的叙述中,正确的是( )。 A. 线性表的顺序存储结构优于链表存储结构 B. 线性表的顺序存储结构适用于频繁插入/删除数据元素的情况 C. 线性表的链表存储结构适用于频繁插入/删除数据元素的情况 D. 线性表的链表存储结构优于顺序存储结构 4、判断:在线性表的顺序存储结构中,逻辑上相邻的两个元素但是在物理位置上不一定是相邻的。( )

单链表、循环链表、双向链表的特点与优缺点 习题: 1、从表中任一结点出发,都能扫描整个表的是( )。 A. 单链表 B. 顺序表 C. 循环链表 D. 静态链表 2、循环链表的主要优点是( )。 A. 不再需要头指针 B. 已知某结点位置后能容易找到其直接前驱 C. 在进行插入、删除运算时能保证链表不断开 D. 在表中任一结点出发都能扫描整个链表 3、

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Chapter1OverviewThese questions are intended as a self-test for readers.Answers to the questions may be found in Appendix C.1.In many data structures you can________a single record,_________it,and_______it.2.Rearranging the contents of a data structure into a certain order is called_________.3.In a database,a field isa.a specific data item.b.a specific object.c.part of a record.d.part of an algorithm.4.The field used when searching for a particular record is the______________.5.In object-oriented programming,an objecta.is a class.b.may contain data and methods.c.is a program.d.may contain classes.6.A classa.is a blueprint for many objects.b.represents a specific real-world object.c.will hold specific values in its fields.d.specifies the type of a method.7.In Java,a class specificationa.creates objects.b.requires the keyword new.c.creates references.d.none of the above.8.When an object wants to do something,it uses a________.9.In Java,accessing an object’s methods requires the_____operator.10.In Java,boolean and byte are_____________.(There are no experiments or programming projects for Chapter1.)Questions31Chapter1,OverviewAnswers to Questions1.insert,search for,delete2.sorting3.c4.search key5.b6.a7.d8.method9.dot10.data typesChapter2ArraysThese questions are intended as a self-test for readers.Answers may be found in Appendix C.1.Inserting an item into an unordered arraya.takes time proportional to the size of the array.b.requires multiple comparisons.c.requires shifting other items to make room.d.takes the same time no matter how many items there are.2.True or False:When you delete an item from an unordered array,in most cases you shift other items to fill in the gap.3.In an unordered array,allowing duplicatesa.increases times for all operations.b.increases search times in some situations.c.always increases insertion times.d.sometimes decreases insertion times.4.True or False:In an unordered array,it’s generally faster to find out an item is not in the array than to find out it is.5.Creating an array in Java requires using the keyword________.6.If class A is going to use class B for something,thena.class A’s methods should be easy to understand.b.it’s preferable if class B communicates with the program’s user.c.the more complex operations should be placed in class A.d.the more work that class B can do,the better.7.When class A is using class B for something,the methods and fields class A can access in class B are called class B’s__________.8.Ordered arrays,compared with unordered arrays,area.much quicker at deletion.b.quicker at insertion.c.quicker to create.d.quicker at searching.9.A logarithm is the inverse of_____________.10.The base10logarithm of1,000is_____.11.The maximum number of elements that must be examined to complete a binary search in an array of200elements isa.200.b.8.c.1.d.13.12.The base2logarithm of64is______.13.True or False:The base2logarithm of100is2.14.Big O notation tellsa.how the speed of an algorithm relates to the number of items.b.the running time of an algorithm for a given size data structure.c.the running time of an algorithm for a given number of items.d.how the size of a data structure relates to the number of items.15.O(1)means a process operates in_________time.16.Either variables of primitive types or_________can be placed in an array.Chapter2,ArraysAnswers to Questions1.d2.True3.b4.False5.new6.d740APPENDIX C Answers to Questions7.interface8.d9.raising to a power10.311.812.613.False14.a15.constant16.objectsChapter3Simple SortingThese questions are intended as a self-test for readers.Answers may be found in Appendix C.puter sorting algorithms are more limited than humans in thata.humans are better at inventing new algorithms.puters can handle only a fixed amount of data.c.humans know what to sort,whereas computers need to be told.puters can compare only two things at a time.2.The two basic operations in simple sorting are_________items and_________ them(or sometimes_________them).3.True or False:The bubble sort always ends up comparing every item with every other item.4.The bubble sort algorithm alternates betweenparing and swapping.b.moving and copying.c.moving and comparing.d.copying and comparing.5.True or False:If there are N items,the bubble sort makes exactly N*N comparisons.6.In the selection sort,a.the largest keys accumulate on the left(low indices).b.a minimum key is repeatedly discovered.c.a number of items must be shifted to insert each item in its correctlysorted position.d.the sorted items accumulate on the right.7.True or False:If,in a particular sorting situation,swaps take much longer than comparisons,the selection sort is about twice as fast as the bubble sort.8.A copy is________times as fast as a swap.9.What is the invariant in the selection sort?10.In the insertion sort,the“marked player”described in the text corresponds to which variable in the insertSort.java program?a.inb.outc.tempd.a[out]11.In the insertion sort,“partially sorted”means thata.some items are already sorted,but they may need to be moved.b.most items are in their final sorted positions,but a few still need to be sorted.c.only some of the items are sorted.d.group items are sorted among themselves,but items outside the groupmay need to be inserted in it.12.Shifting a group of items left or right requires repeated__________.13.In the insertion sort,after an item is inserted in the partially sorted group,it willa.never be moved again.b.never be shifted to the left.c.often be moved out of this group.d.find that its group is steadily shrinking.14.The invariant in the insertion sort is that________.15.Stability might refer toa.items with secondary keys being excluded from a sort.b.keeping cities sorted by increasing population within each state,in a sortby state.c.keeping the same first names matched with the same last names.d.items keeping the same order of primary keys without regard tosecondary keys.Chapter3,Simple SortingAnswers to Questions1.dparing and swapping(or copying)3.False4.a5.False6.b7.False8.three9.Items with indices less than or equal to outer are sorted.10.c11.d12.copies13.b14.Items with indices less than outer are partially sorted.15.bChapter4Stacks and QueuesThese questions are intended as a self-test for readers.Answers may be found in Appendix C.1.Suppose you push10,20,30,and40onto the stack.Then you pop three items. Which one is left on the stack?2.Which of the following is true?a.The pop operation on a stack is considerably simpler than the remove operation on a queue.b.The contents of a queue can wrap around,while those of a stack cannot.c.The top of a stack corresponds to the front of a queue.d.In both the stack and the queue,items removed in sequence are takenfrom increasingly high index cells in the array.3.What do LIFO and FIFO mean?4.True or False:A stack or a queue often serves as the underlying mechanism on which an ADT array is based.5.Assume an array is numbered with index0on the left.A queue representing a line of movie-goers,with the first to arrive numbered1,has the ticket window on the right.Thena.there is no numerical correspondence between the index numbers andthe movie-goer numbers.b.the array index numbers and the movie-goer numbers increase inopposite left-right directions.c.the array index numbers correspond numerically to the locations in theline of movie-goers.d.the movie-goers and the items in the array move in the same direction.6.As other items are inserted and removed,does a particular item in a queue move along the array from lower to higher indices,or higher to lower?7.Suppose you insert15,25,35,and45into a queue.Then you remove three items.Which one is left?8.True or False:Pushing and popping items on a stack and inserting and removing items in a queue all take O(N)time.9.A queue might be used to holda.the items to be sorted in an insertion sort.b.reports of a variety of imminent attacks on the star ship Enterprise.c.keystrokes made by a computer user writing a letter.d.symbols in an algebraic expression being evaluated.10.Inserting an item into a typical priority queue takes what big O time?11.The term priority in a priority queue means thata.the highest priority items are inserted first.b.the programmer must prioritize access to the underlying array.c.the underlying array is sorted by the priority of the items.d.the lowest priority items are deleted first.12.True or False:At least one of the methods in the priorityQ.java program (Listing4.6)uses a linear search.13.One difference between a priority queue and an ordered array is thata.the lowest-priority item cannot be extracted easily from the array as it can from the priority queue.b.the array must be ordered while the priority queue need not be.c.the highest priority item can be extracted easily from the priority queue but not from the array.d.All of the above.14.Suppose you based a priority queue class on the OrdArray class in the orderedArray.java program(Listing2.4)in Chapter2,“Arrays.”This will buy you binary search capability.If you wanted the best performance for your priority queue,would you need to modify the OrdArray class?15.A priority queue might be used to holda.passengers to be picked up by a taxi from different parts of the city.b.keystrokes made at a computer keyboard.c.squares on a chessboard in a game program.d.planets in a solar system simulation.Chapter4,Stacks and QueuesAnswers to Questions1.102.bst-In-First-Out;and First-In-First-Out4.False.It’s the other way around.5.b6.It doesn’t move at all.7.458.False.They take O(1)time.9.c10.O(N)11.c12.True13.b14.Yes,you would need a method to find the minimum value.15.aChapter5Linked Lists1.Which of the following is not true?A reference to a class objecta.can be used to access public methods in the object.b.has a size dependant on its class.c.has the data type of the class.d.does not hold the object itself.2.Access to the links in a linked list is usually through the_________link.3.When you create a reference to a link in a linked list,ita.must refer to the first link.b.must refer to the link pointed to by current.c.must refer to the link pointed to by next.d.can refer to any link you want.4.How many references must you change to insert a link in the middle of a singly linked list?5.How many references must you change to insert a link at the end of a singly linked list?6.In the insertFirst()method in the linkList.java program(Listing5.1),the statement newLink.next=first;means thata.the next new link to be inserted will refer to first.b.first will refer to the new link.c.the next field of the new link will refer to the old first link.d.newLink.next will refer to the new first link in the list.7.Assuming current points to the next-to-last link in a singly linked list,what statement will delete the last link from the list?8.When all references to a link are changed to refer to something else,what happens to the link?9.A double-ended lista.can be accessed from either end.b.is a different name for a doubly linked list.c.has pointers running both forward and backward between links.d.has its first link connected to its last link.10.A special case often occurs for insertion and deletion routines when a list is ________.11.Assuming a copy takes longer than a comparison,is it faster to delete an item with a certain key from a linked list or from an unsorted array?12.How many times would you need to traverse a singly linked list to delete the item with the largest key?13.Of the lists discussed in this chapter,which one would be best for implementing a queue?14.Which of the following is not true?Iterators would be useful if you wanted toa.do an insertion sort on a linked list.b.insert a new link at the beginning of a list.c.swap two links at arbitrary locations.d.delete all links with a certain key value.15.Which do you think would be a better choice to implement a stack:a singly linked list or an array?Chapter5,Linked ListsAnswers to Questions1.b2.first3.d4.25.16.c7.current.next=null;8.Java’s garbage collection process destroys it.Chapter5,Linked Lists7419.a10.empty11.a linked list12.once,if the links include a previous reference13.a double-ended list14.bually,the list.They both do push()and pop()in O(1)time,but the list uses memory more efficiently.Chapter6RecursionThese questions are intended as a self-test for readers.Answers may be found in Appendix C.1.If the user enters10in the triangle.java program(Listing6.1),what is the maximum number of“copies”of the triangle()method(actually just copies of its argument)that exist at any one time?2.Where are the copies of the argument,mentioned in question1,stored?a.in a variable in the triangle()methodb.in a field of the TriangleApp classc.in a variable of the getString()methodd.on a stack3.Assume the user enters10as in question1.What is the value of n when the triangle()method first returns a value other than1?4.Assume the same situation as in question1.What is the value of n when the triangle()method is about to return to main()?5.True or false:In the triangle()method,the return values are stored on the stack.6.In the anagram.java program(Listing6.2),at a certain depth of recursion,a version of the doAnagram()method is working with the string“led”.When this method calls a new version of itself,what letters will the new version be working with?7.We’ve seen that recursion can take the place of a loop,as in the loop-oriented orderedArray.java program(Listing2.4)and the recursive binarySearch.java program(Listing6.3).Which of the following is not true?a.Both programs divide the range repeatedly in half.b.If the key is not found,the loop version returns because the rangebounds cross,but the recursive version occurs because it reaches thebottom recursion level.c.If the key is found,the loop version returns from the entire method, whereas the recursive version returns from only one level of recursion.d.In the recursive version the range to be searched must be specified in the arguments,while in the loop version it need not be.8.In the recFind()method in the binarySearch.java program(Listing6.3),what takes the place of the loop in the non-recursive version?a.the recFind()methodb.arguments to recFind()c.recursive calls to recFind()d.the call from main()to recFind()9.The binarySearch.java program is an example of the_________approach to solving a problem.10.What gets smaller as you make repeated recursive calls in the redFind() method?11.What becomes smaller with repeated recursive calls in the towers.java program (Listing6.4)?12.The algorithm in the towers.java program involvesa.“trees”that are data storage devices.b.secretly putting small disks under large disks.c.changing which columns are the source and destination.d.moving one small disk and then a stack of larger disks.13.Which is not true about the merge()method in the merge.java program(Listing6.5)?a.Its algorithm can handle arrays of different sizes.b.It must search the target array to find where to put the next item.c.It is not recursive.d.It continuously takes the smallest item irrespective of what array it’s in.14.The disadvantage of mergesort is thata.it is not recursive.b.it uses more memory.c.although faster than the insertion sort,it is much slower than quicksort.d.it is complicated to implement.15.Besides a loop,a___________can often be used instead of recursion.Chapter6,RecursionAnswers to Questions1.102.d3.24.105.false6.“ed”7.b8.c9.divide-and-conquer10.the range of cells to search11.the number of disks to transfer12.c13.b14.b15.stackChapter7Advanced SortingThese questions are intended as a self-test for readers.Answers may be found in Appendix C.1.The Shellsort works bya.partitioning the array.b.swapping adjacent elements.c.dealing with widely separated elements.d.starting with the normal insertion sort.2.If an array has100elements,then Knuth’s algorithm would start with an interval of________.3.To transform the insertion sort into the Shellsort,which of the following do you not do?a.Substitute h for1.b.Insert an algorithm for creating gaps of decreasing width.c.Enclose the normal insertion sort in a loop.d.Change the direction of the indices in the inner loop.4.True or false:A good interval sequence for the Shellsort is created by repeatedly dividing the array size in half.5.Fill in the big O values:The speed of the Shellsort is more than_______but less than________.6.Partitioning isa.putting all elements larger than a certain value on one end of the array.b.dividing an array in half.c.partially sorting parts of an array.d.sorting each half of an array separately.7.When partitioning,each array element is compared to the_______.8.In partitioning,if an array element is equal to the answer to question7,a.it is passed over.b.it is passed over or not,depending on the other array element.c.it is placed in the pivot position.d.it is swapped.9.True or false:In quicksort,the pivot can be an arbitrary element of the array.10.Assuming larger keys on the right,the partition isa.the element between the left and right subarrays.b.the key value of the element between the left and right subarrays.c.the left element in the right subarray.d.the key value of the left element in the right subarray.11.Quicksort involves partitioning the original array and then_________.12.After a partition in a simple version of quicksort,the pivot may beed to find the median of the array.b.exchanged with an element of the right subarray.ed as the starting point of the next partition.d.discarded.13.Median-of-three partitioning is a way of choosing the_______.14.In quicksort,for an array of N elements,the partitionIt()method will examine each element approximately______times.15.True or false:You can speed up quicksort if you stop partitioning when the partition size is5and finish by using a different sort.Chapter7,Advanced SortingAnswers to Questions1.c2.403.d4.false5.O(N*logN),O(N2)6.a7.pivot8.d9.true10.c11.partitioning the resulting subarrays12.b13.pivot14.log2N15.trueChapter8Binary TreesThese questions are intended as a self-test for readers.Answers may be found in Appendix C.1.Insertion and deletion in a tree require what big O time?2.A binary tree is a search tree ifa.every non-leaf node has children whose key values are less than(or equal to)the parent.b.every left child has a key less than the parent and every right child has akey greater than(or equal to)the parent.c.in the path from the root to every leaf node,the key of each node is greater than(or equal to)the key of its parent.d.a node can have a maximum of two children.3.True or False:Not all trees are binary trees.4.In a complete binary tree with20nodes,and the root considered to be at level 0,how many nodes are there at level4?5.A subtree of a binary tree always hasa.a root that is a child of the main tree’s root.b.a root unconnected to the main tree’s root.c.fewer nodes than the main tree.d.a sibling with the same number of nodes.6.In the Java code for a tree,the______and the_______are generally separate classes.7.Finding a node in a binary search tree involves going from node to node, askinga.how big the node’s key is in relation to the search key.b.how big the node’s key is compared to its right or left children.c.what leaf node we want to reach.d.what level we are on.8.An unbalanced tree is onea.in which most of the keys have values greater than the average.b.whose behavior is unpredictable.c.in which the root or some other node has many more left children thanright children,or vice versa.d.that is shaped like an umbrella.9.Inserting a node starts with the same steps as_______a node.10.Suppose a node A has a successor node S.Then S must have a key that is larger than_____but smaller than or equal to_______.11.In a binary tree used to represent a mathematical expression,which of the following is not true?a.Both children of an operator node must be operands.b.Following a postorder traversal,no parentheses need to be added.c.Following an inorder traversal,parentheses must be added.d.In pre-order traversal a node is visited before either of its children.12.If a tree is represented by an array,the right child of a node at index n has an index of_______.13.True or False:Deleting a node with one child from a binary search tree involves finding that node’s successor.14.A Huffman tree is typically used to_______text.15.Which of the following is not true about a Huffman tree?a.The most frequently used characters always appear near the top of the tree.b.Normally,decoding a message involves repeatedly following a path fromthe root to a leaf.c.In coding a character you typically start at a leaf and work upward.d.The tree can be generated by removal and insertion operations on apriority queue.Chapter8,Binary TreesAnswers to Questions1.O(logN)2.b3.True4.55.c6.node,tree7.a8.cChapter8,Binary Trees7439.finding10.A,A’s left-child descendents11.d12.2*n+113.Falsepress15.cChapter102-3-4Trees and External StorageThese questions are intended as a self-test for readers.Answers may be found in Appendix C.1.A2-3-4tree is so named because a node can havea.three children and four data items.b.two,three,or four children.c.two parents,three children,and four items.d.two parents,three items,and four children.2.A2-3-4tree is superior to a binary search tree in that it is________.3.Imagine a parent node with data items25,50,and75.If one of its child nodes had items with values60and70,it would be the child numbered__________.4.True or False:Data items are located exclusively in leaf nodes.5.Which of the following is not true each time a node is split?a.Exactly one new node is created.b.Exactly one new data item is added to the tree.c.One data item moves from the split node to its parent.d.One data item moves from the split node to its new sibling.6.A2-3-4tree increases its number of levels when________.7.Searching a2-3-4tree does not involvea.splitting nodes on the way down if necessary.b.picking the appropriate child to go to,based on data items in a node.c.ending up at a leaf node if the search key is not found.d.examining at least one data item in any node visited.8.After a non-root node of a2-3-4tree is split,does its new right child contain the item previously numbered0,1,or2?9.A4-node split in a2-3-4tree is equivalent to a_______in a red-black tree.10.Which of the following statements about a node-splitting operation in a2-3 tree(not a2-3-4tree)is not true?a.The parent of a split node must also be split if it is full.b.The smallest item in the node being split always stays in that node.c.When the parent is split,child2must always be disconnected from itsold parent and connected to the new parent.d.The splitting process starts at a leaf and works upward.11.What is the big O efficiency of a2-3tree?12.In accessing data on a disk drive,a.inserting data is slow but finding the place to write data is fast.b.moving data to make room for more data is fast because so many itemscan be accessed at once.c.deleting data is unusually fast.d.finding the place to write data is comparatively slow but a lot of data canbe written quickly.13.In a B-tree each node contains_______data items.14.True or False:Node splits in a B-tree have similarities to node splits in a2-3 tree.15.In external storage,indexing means keeping a file ofa.keys and their corresponding blocks.b.records and their corresponding blocks.c.keys and their corresponding records.st names and their corresponding keys.Chapter9,Red-Black TreesAnswers to Questions1.in order(or inverse order)2.b3.False4.d5.b6.rotations,changing the colors of nodes7.red8.a9.left child,right child10.d11.a node,its two children12.b13.True14.a15.TrueQuestionsThese questions are intended as a self-test for readers.Answers may be found in Appendix C.Chapter11Hash Tablesing big O notation,say how long it takes(ideally)to find an item in a hash table.2.A__________transforms a range of key values into a range of index values.3.Open addressing refers toa.keeping many of the cells in the array unoccupied.b.keeping an open mind about which address to use.c.probing at cell x+1,x+2,and so on until an empty cell is found.d.looking for another location in the array when the one you want is occupied.ing the next available position after an unsuccessful probe is called_____________.5.What are the first five step sizes in quadratic probing?6.Secondary clustering occurs becausea.many keys hash to the same location.b.the sequence of step lengths is always the same.c.too many items with the same key are inserted.d.the hash function is not perfect.7.Separate chaining involves the use of a_____________at each location.8.A reasonable load factor in separate chaining is________.9.True or False:A possible hash function for strings involves multiplying each character by an ever-increasing power.10.The best technique when the amount of data is not well known isa.linear probing.b.quadratic probing.c.double hashing.d.separate chaining.11.If digit folding is used in a hash function,the number of digits in each group should reflect_____________.12.True or False:In linear probing an unsuccessful search takes longer than a successful search.Questions57513.In separate chaining the time to insert a new itema.increases linearly with the load factor.b.is proportional to the number of items in the table.c.is proportional to the number of lists.d.is proportional to the percentage of full cells in the array.14.True or False:In external hashing,it’s important that the records don’t become full.15.In external hashing,all records with keys that hash to the same value are located in___________.Chapter11,Hash TablesAnswers to Questions1.O(1)2.hash function3.d4.linear probing5.1,4,9,16,256.b7.linked listChapter11,Hash Tables7458.1.09.True10.d11.the array size。

相关文档
最新文档