已阅读5页,还剩9页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
数据结构课后习题参考答案1.1.20(1)T(n) = n-1;渐近时间复杂度为O(n)。(2)T(n) = log2n;渐近时间复杂度为O(log2n)。(3)T(n) = n(n+1)(n+2)/6;渐近时间复杂度为O(n3)(4)T(n) =;渐近时间复杂度为O()2.2.3template bool SeqList:Insert2(int i, T x)if (i n-1)cout Out of bounds endl;return false;if (n = maxLength)T *elem = new T2 * maxLength;maxLength = maxLength * 2;for (int j = 0; j = i; j+)elemj = elementsj;elemi+1 = x;for (j = i+1; j i; j-)elementsj+1 = elementsj;elementsi+1 = x;n+;return true;算法的时间复杂度为O(n)2.4template bool SeqList:Del(T x)if (!n)cout UnderFlow endl;return false;for (int i=0; in; i+)if (elementsi = x)for (int j=i+1; jn; j+)elementsj-1 = elementsj;n-;return true;return false;算法的时间复杂度为O(n)2.6 template void SingleList:Invert() Node *p=first,*q; first=NULL; while (p) q = p-link;p-link = first; first = p; p = q; 算法的时间复杂度为O(n)2.7 template bool SingleList:Del(const T& x) if (!n)cout Under Flow! endl;return false;Node *p=first, *q;if (first-data) = x) first = first-link;delete p;n-;return true;while (p & p-data != x) q = p;p = p-link;if (p)q-link = p-link;delete(p);n-;return true;return false;算法的时间复杂度为O(n)3.3.1答: (2)和(3)不能。对(2)中的E,B,D而言,E最先出栈则表明,此时B和D均在栈中,由于,B先于D进栈,所以应有D先出栈。同理(3)也不能。 (1)能,操作序列:push(A),pop(),push(B),pop(),push(C),pop(),push(D),pop(),push(E),pop()(4)能,操作序列:push(A),push(B),push(C),push(D),push(E),pop() ,pop(),pop(),pop(),pop() 3.4(1) (a+b)/(c+d) ab+cd+/(2) b2-4*a*c b24a*c*-(3) a*c-b/c2 ac*-bc2/-(4) (a+b)*c+d/(e+f) ab+c*def+/+(5) (a+b)*(c*d+e)-a*c ab+cd*e+*ac*- 3.13t template void SeqStack:Invert(void)SeqQueue sq(maxTop + 1);T temp;while (top != -1)this-Top(temp);this-Pop();sq.EnQueue(temp);while (!sq.IsEmpty()sq.Front(temp);sq.DeQueue();this-Push(temp);4.4.2 设三维数组的维度分别是m, n, l;每个元素占k个存储单元。Loc(Aijk) = Loc(A000) + (i * n * l + j * l) * k4.64.7col01234num10212k011345.5.2(1)无序树:9棵(2)有序树:12棵(3)二叉树:30棵5.4(1)2k-1(2)(3)k(i-1) + m + 1(4) i +15.5(1) 空二叉树和所有结点均无左孩子的二叉树(2) 空二叉树和只有一个根(3) 空二叉树和所有结点均无右孩子5.65.7先:DEHFJGCKAB中:HEJFGKCDAB后:HJKCGFEBAD5.9(1)template void BinaryTree:Del(BTNode *p) /private if (p!=NULL)Del(p-lChild);Del(p-rChild);delete p;template void BTree:Del() /publicDel(root);root = NULL;(2) /*求二叉树中度为1的结点个数*/template int BinaryTree:CountDegree1() int total = 0; CountDegree1(root, total); return total;template void BinaryTree:CountDegree1(BTNode *t, int &num) if (t) if ( ( (t-lChild != NULL) & (t-rChild = NULL) ) | ( (t-lChild = NULL) & (t-rChild != NULL) ) +num; CountDegree1(t-lChild, num); CountDegree1(t-rChild, num); (3)template void BinaryTree:Exch(BTNode *p) /private if (p!=NULL)BTNode *temp;temp=p-lchild; p-lchild=p-rchild; p-rchild=temp;Exch(p-lchild);Exch(p-rchild);template void BTree:Exchange() /publicExch(root); 5.14 5.19(1)(2)WPL = 91(3)各字符的编码A:1010B:1011C:100D:00E:01F:116.6.5template bool SeqList:Search1(T x)constreturn SearchRec(x, 0);template bool SeqList:SearchRec(T x, int i)constif (i = n) return false;if (elementsi = x) return true;else return SearchRec(x, i+1);6.6template bool SingleList:Search1(T x) constNode *p = first;while (p & p-element link;if (p = NULL | p-element x) return false;return true;6.10ASL成功=1/12(1*1+2*2+3*4+4*5)=37/12ASL失败=1/13(3*3+4*10)=49/137.7.17.37.16(1)7.17(1) 删除a删除e删除f删除h8.8.3 8.4012345678910线性探查法5545352570806050二次探查法4535802570605055双散列法55803525706045509.9.1(1)顶点入度出度030122212312423511(2)(3)9.29.4template void LGraph:Degree(int *in)for (int i=0;in;i+)ini=0;ENode *p;for (i=0; iadjvex+;p=p-nextarc;for (i=0;in;i+)couti: ini ; endl;9.6 9.13 9.1710.10.1(1) 简单选择排序初始序列(61 87 12 03 08 70 97 75 53 26第1趟 (03)87 12 61 08 70 97 75 53 26第2趟 (03 08)12 61 87 70 97 75 53 26第3趟 (03 08 12)61 87 70 97 75 53 26第4趟 (03 08 12 26)87 70 97 75 53 61第5趟 (03 08 12 26 53)70 97 75 87 61第6趟 (03 08 12 26 53 61)97 75 87 70第7趟 (03 08 12 26 53 61 70)75 87 97第8趟 (03 08 12 26 53 61 70 75)87 97第9趟 (03 08 12 26 53 61 70 75 87)97(2) 直接插入排序初始序列(61)87 12 03 08 70 97 75 53 26第1趟 (61 87)12 03 08 70 97 75 53 26第2趟 (12 61 87)03 08 70 97 75 53 26第3趟 (03 12 61 87)08 70 97 75 53 26第4趟 (03 08 12 61 87)70 97 75 53 26第5趟 (03 08 12 61 70 87)97 75 53 26第6趟 (03 08 12 61 70 87 97)75 53 26第7趟 (03 08 12 61 70 75 87 97)53 26第8趟 (03 08 12 53 61 70 75 87 97)26第9趟 (03 08 12 26 53 61 70 75 87 97) (3) 冒泡排序(注意冒泡排序只排了7趟)初始序列(61 87 12 03 08 70 97 75 53 26)第1趟 61 12 03 08 70 87 75 53 26 97 第2趟 12 03 08 61 70 75 53 26 87 97第3趟 03 08 12 61 70 53 26 75 87 97第4趟 03 08 12 61 53 26 70 75 87 97第5趟 03 08 12 53 26 61 70 75 87 97第6趟 03 08 12 26 53 61 70 75 87 97第7趟 03 08 12 26 53 61 70 75 87 97(4) 快速排序初始序列61 87 12 03 08 70 97 75 53 26第1趟 (53 26 12 3 8) 61 (97 75 70 87) 第2趟 ( 8 26 12 3) 53 61 (97 75 70 87) 第3趟 3 8 (12 26) 53 61 (97 75 70 87) 第4趟 3 8 12 26 53 (61 97 75 70 87) 第5趟 3 8 12 26 53 61 (87 75 70 97) 第6趟 3 8 12 26 53 61 (70 75) 87 97 第7趟 3 8 12 26 53 61 70 75 87 97 (5) 两路合并排序初始序列(61)(87) (12)(03) (08)(70) (97)(75) (53)(26)第1趟 (61 87) (03 12) ( 08 70) (75 97) (26 53)第2趟 (03 12 61 87) ( 08 70 75 97) (26 53)第3趟 (03 08 12 61 70 75 87 97) (26 53)第4趟 (03 08 12 26 53 61 70 75 87 97) 10.2(1)简单选择排序: 三种情况下的时间复杂度都为O(
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 物流管理合同的法律责任
- 文化创意设计工作室策划与创意部负责人绩效评价表
- 初中生演讲稿:努力成就梦想4篇
- 企业内训体系构建及执行流程指南
- 企业办公环境管理评估工具
- 供应链管理流程与工具集成模板
- 房地产公司项目总工程师绩效评定表
- 语文课上的一次辩论赛议论文写作(15篇)
- 人才招聘选拔过程操作手册
- 中级消防设施操作员模拟试卷检测维保及答案
- 2025至2030全球及中国区块链安全解决方案行业项目调研及市场前景预测评估报告
- 统编版小学语文四年级下册背诵篇目(新修订)
- 变频柜维修专业知识培训课件
- 电气工程及其自动化面试问题及答案完整版
- 学堂在线 中国经济概论 期末考试答案
- 新版中华民族共同体概论课件第八讲共奉中国与中华民族内聚发展(辽宋夏金时期)-2025年版
- 《PLC电气控制技术》课件(共九章)
- 开展非遗成果汇报
- 2025年陕西省事业单位劳动合同书
- 2024年西安外事学院《形势与政策》期末考试试题预测试卷含答案详解(能力提升)
- 2025年民航招飞心理测试试题及答案
评论
0/150
提交评论