华南理工大学06年数据结构A卷
06年A卷
选择题
题目没有变化,自己练一下?
- 一个算法必须做以下所有事情,除了:
- Correct
- Composed of concrete steps
- Ambiguous
- Terminate
- Correct
- 选择随着 \(n\)
增大而对应于最有效算法的增长率:
- $ 20n $
- $ 5 n $
- $ 2^n $
- $ 10n^2 $
- $ 20n $
- 如果一个数据元素需要4字节,一个指针需要4字节,则在非零元素的比例小于约多少时,链表表示法将比标准数组表示法更节省空间?
- 75%
- 25%
- 15%
- 50%
- 75%
- 以下哪一陈述是不正确的?
- The number of leaves in a non-empty full binary tree is one more
than the number of internal nodes.
- A cluster is the smallest unit of allocation for a file, so all
files are a multiple of the cluster size.
- The best case for my algorithm is $ n = 1 $ because that is the
fastest.
- The number of 4-node binary trees in different shape is 14.
- The number of leaves in a non-empty full binary tree is one more
than the number of internal nodes.
- 减少磁盘程序所需时间的最有效方法是:
- Improve the basic operations.
- Minimize the number of disk accesses.
- Eliminate the recursive calls.
- Reduce main memory use.
- Improve the basic operations.
- 我们使用父指针表示法来解决一般树的哪一个问题?
- Shortest paths
- General tree traversal
- Equivalence classes
- Exact-match query
- Shortest paths
- 以下哪一陈述是正确的?
- In a BST, the left child of any node is less than the right child,
and in a heap, the left child of any node is less than the right
child.
- In a BST, the left child of any node could be less or greater than
the right child, but in a heap, the left child of any node must be less
than the right child.
- In a BST, the left child of any node is less than the right child,
but in a heap, the left child of any node could be less than or greater
than the right child.
- In both a BST and a heap, the left child of any node could be either less than or greater than the right child.
- In a BST, the left child of any node is less than the right child,
and in a heap, the left child of any node is less than the right
child.
- 替代选择排序的功能是:
- Select the maximal element.
- Generate the initial merge file.
- Merge the sorted file.
- Replace some record.
- Select the maximal element.
- 树索引方法是为了克服哈希中的什么缺陷?
- Inability to handle range queries.
- Inability to handle updates.
- Inability to handle large data sets.
- None of all above.
- Inability to handle range queries.
- 假设我们有8条记录,键值从A到H,且它们最初按字母顺序排列。现在,考虑应用以下访问模式:F
D F G E G F A D F G
E。如果列表按“移至前面”启发式组织,则最终的列表将是:
- F G D E A B C H
- E G F D A B C H
- A B F D G E C H
- F D G E A B C H
- F G D E A B C H
答案:CBDCB CCBAB
程序填空题
(1) 使用 C++ 抽象类声明一个列表,编写一个函数交换列表右部分的前两个元素:
// 交换当前元素和下一个元素的顺序
1 | void switch(List<Elem> L1) { |
解释:
L1.isEmpty()
用于检查列表是否为空。L1.next()
用于获取列表中的下一个元素。L1.insert()
用于插入元素。- 如果列表为空或没有下一个元素,输出错误。
(2) 给定一个按值排序的整数数组,修改二分查找函数,返回 K 不在数组中时最大值小于 K 的元素的位置。若数组中最小值大于 K,则返回错误:
// 返回小于等于 K 的最大元素的位置
1 | int newbinary(int array[], int n, int K) { |
解释:
l
和r
分别是左右边界,初始时设置为超出数组范围。- 使用二分查找来查找最接近但小于 K 的元素。
- 当
K
不在数组中时,l
将指向比K
小的最大值。 - 如果数组中最小的值大于
K
,则返回-1
。
(3) 一个有 100 个内部节点的完全 2 叉树有多少个节点?
答案: 201
解释:
- 完全二叉树的节点数量公式是:总节点数 = 内部节点数 + 叶子节点数。
- 对于一棵二叉树,内部节点数与叶子节点数之间的关系是:叶子节点数 = 内部节点数 + 1。
- 所以,当内部节点数为 100 时,叶子节点数为 100 + 1 = 101。
- 因此,总节点数为:100 + 101 = 201。
复杂度分析
(1)
1 | sum = 0; |
解决方案: Θ(n)
解释:
- 外部循环运行 3 次,这是常数操作。
- 内部循环运行 n 次。
- 所以,总的时间复杂度是 3 * n = Θ(n),常数因子可以忽略,因此是 Θ(n)。
(2)
1 | sum = 0; |
解决方案: Θ(n²)
解释:
- 外部循环运行 n 次。
- 内部循环的迭代次数取决于
A[j] != i
条件,在最坏情况下,内部循环最多执行 i 次。- 对于 i = 0,内循环运行 0 次;对于 i = 1,内循环运行 1 次,依此类推。
- 所以,总的迭代次数为 0 + 1 + 2 + ... + (n-1) = n(n-1)/2,这个是一个二次增长的序列。
- 因此,时间复杂度是 Θ(n²)。
(3)
1 | sum = 0; |
解决方案: Θ(n)
解释:
- 如果
n
是偶数,for
循环执行 n 次,所以时间复杂度为 Θ(n)。 - 如果
n
不是偶数,else
分支是常数时间操作,时间复杂度为 Θ(1)。 - 因此,不管
n
是偶数还是奇数,总的时间复杂度都是 Θ(n)。
图论
最短路径
- C → A: 距离为 4,路径为 (C, A)。
- C → F: 距离为 5,路径为 (C, F)。
- C → D: 距离为 6,路径为 (C, A, D)。
- C → B: 距离为 12,路径为 (C, A, D, B)。
- C → G: 距离为 11,路径为 (C, F, G)。
- C → E: 距离为 13,路径为 (C, A, D, B, E)。
最小生成树:
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Totoroの旅!
评论