数据结构与算法期中考试

  1. 填空(2分)
    由小到大写出以下时间复杂度的序列:

Ascending write the sequence of followings according to their time complexit:

(1)n²+100n

(2)3n²+100n²

(3)10+3Log­10n

(4)10n+20nLog­10n

(5)2^n

(6)1000n

答案直接写标号,如:(1)(2)(3)(4)(5) (提示:系统基于字符匹配来判定答案,所以您的答案中不要出现空格)

Write down the answer labels such as (1)(2)(3)(4)(5). (Hint:This problem is judged by string matching, Please make sure your answer don’t contain any blanks.

正确答案:(3)(6)(4)(1)(2)(5)

  1. 填空(2分)

计算运行下列程序段后s的值:

After running the following program segment, the value of s is:

n=10; s=0;
for( k = 1; k < n - 1; k++)
for( j = n; j >= k; j–)
s = s + 1;

正确答案:52
解析: (n+3)*(n-2)/2

  1. 填空(2分)
    双端队列可以在队列的两端进行插入和删除操作,既可在队尾进行插入/删除,又可在队头进行插入/删除。现有11个不同的元素顺序输入到双端队列,那么可以得到多少种不同的排列?

Deque can do insert and delete operations on both ends of the queue, can insert / delete in the team head and also in the tail. Existing 11 different elements enter the double-ended queue orderly, then how many different permutations can you get?

正确答案:1024
解析: 第一个元素从左或右入队没有区别,以后每个元素都有从左和从右两种入队方式,即有2^{x-1}种方法。
The first element through left or right into the team makes no difference. Every elements after it can get in left and right, that is 2^(x-1) methods.

  1. 多选(3分)
    顺序栈是用一段连续的空间存储内容,本质是顺序表。链式栈则是采用单链表的方式存储。下列关于这两种存储方式的说法正确的是:

Sequential stack stores elements in a contiguous space, which is essentially a sequential list. Linked stack is implemented by a single linked list instead. Which of the following about the two storage methods are correct? (multiple choice)

A.顺序栈的压栈和出栈操作只需常数时间。 The push and pop operation of sequence stack only needs constant time.
B.链式栈的压栈和出栈操作只需常数时间。 The push and pop operation of linked stack only needs constant time.
C.顺序栈需要指定一个具体的长度 Sequential stack needs to be assigned a specific length.
D.链式栈需要一个结构性开销 Linked stack needs a structural cost.

正确答案:A、B、C、D

  1. 填空(2分)
    按照课程中介绍的机械的递归转换,将下列递归过程改写为非递归过程后,程序中需要设置____个语句标号。

According to the description in class about recursive mechanical conversion, rewritten the following recursive procedure into non recursive process, the program needs set ____ statement labels.

void test(int &sum){
    int x;
    scanf(x);
    if(x == 0){
        test(sum);
        sum = 0;
    }else{
        test(sum);
        sum += x;
    }
    printf(sum);
}

正确答案:4
解析: 根据课程介绍的方法,需要设置t+2个语句标号,其中t为递归调用本函数的语句个数。本题中,test函数调用自身2次(t=2),故需要4个语句标号。
According to the course’s description, it is necessary to set t+2 statement labels, where t is the number of recursive calls to the statement of the function. In this problem, test the function calls itself two time (t = 2), so it requires three statement labels.

  1. 填空(2分)
    若字符串s =“DataMining”,则其子串的数目为 (字串数目应该重复计算)

If the string s = “DataMining”, then the number of its substrings is:________. (If we encounter the same substring , the total should be added one)

正确答案:56
解析: 空串和自身各1个,长为1的10个,长为2的9个,…,长为9的2个。 2+10+9+8+7+6+5+4+3+2=56
the empty string and itself are both 1 and the number with length 1 is 10 and 2 is 9, ……, length 9 is 2. 2+10+9+8+7+6+5+4+3+2=56

  1. 多选(3分)
    在字符{A, C, G, T}组成的DNA序列中,A —— T和C —— G是互补对。

判断一个DNA序列中是否存在互补回文串(例如,ATCATGAT的补串是TAGTACTA,与原串形成互补回文串;即要求整个原串的补串是原串的逆序);

下面DNA序列中存在互补回文串的是:(多选)

In DNA sequences consisting of characters {A, C, G, T}, A - T and C - G are complementary pairs respectively.

Determine whether a DNA sequence has a complementary palindromic string (For example, ATCATGAT’s complementary string is TAGTACTA, with is the palindromic sequence to the original string; in such case the complementary string is also the reverse of the original string);

Which of the following DNA sequences have complementary palindromic string? (multiple choice)

得分/总分

A.CTGATCAG
B.AATTAATT
C.GTACGTAC
D.AGCTAGCT

正确答案:A、B、C、D

  1. 填空(2分)
    使用KMP算法求出模式p=”aabcaabbaa”的优化后的next数组。注意:只列出数字,数字之间用一个空格分隔。比如:0 0 0 0 0 0 0 0 0 0

Use KMP algorithm to derive the optimized “next” array for the pattern p = “aabcaabbaa”.

Note: Write down the numbers in the next array separated by single space.

For example: 0 0 0 0 0 0 0 0 0 0

正确答案:-1 -1 1 0 -1 -1 1 3 -1 -1

  1. 填空(2分)
    利用上题(KMP算法题)p=”aabcaabbaa”优化后的Next数组,对t=”aaabaabcabaabcaabbaab”进行匹配。有多少次字符比较?(注意:每一次p中的字符与t中的字符的一次比较计做一次)

Use the optimized “next” array above for pattern p = "aabcaabbaa"to match the target string t = “aaabaabcabaabcaabbaab”. How many character comparisons are needed?

(Note: Each comparison of the characters between p and t counts once)

正确答案:22

  1. 填空(2分)
    一个有4层结点的完全二叉树。按前序遍历周游给结点从1开始编号,则第21号结点的父结点是多少号?(注释:根的层数为0)

For a complete binary tree with four levels, label the nodes starting from 1 according to the preorder traversal. what is the label of the parent node for node 21? (The root is on level 0)

正确答案:19
解析: 画出该完全二叉树即可得到答案。
Draw out the complete binary tree and then get the answer.

  1. 填空(2分)

假设一棵二叉树中,度为2的结点有20个,度为1的结点有10个,度为0的结点有多少个?

In a binary tree, there are 20 nodes with a degree of 2 and 10 nodes with a degree of 1. How many nodes are with a degree of 0?

正确答案:21
解析: 解释:边数=总度数=结点数-1,即答案是度为2的结点个数+1
the amount of edges = the sum of degrees = the amount of nodes minus 1, so the answer is the amount of nodes with a degree of 2 plus 1

  1. 填空(2分)
    某二叉树中序序列为A,B,C,D,E,F,G, 前序序列为E,A,C,B,D,G,F, 则后序序列是?

(注意:答案不要含空格和逗号,比如可以是ABCDEFG)

The infix order sequence of a binary tree is ABCDEFG, and its preorder sequence is EACBDGF. Please write down its postorder sequence. (No blank space or comma in your answer. For example ABCDEFG is a valid format.)

正确答案:BDCAFGE
解析: 解释:根据中序序列和前序序列构建出这棵二叉树。
We can get the binary tree according to its sequences of preorder and infix order.

  1. 填空(2分)

对于键值序列{38,64,52,26,73,40,48,55,15,12},用筛选法建最小值堆,共交换元素多少次?

For the key value sequence {38,64,52,26,73,40,48,55,15,12}, use the bottom-up heapification method to construct a minimum heap. How many times should we exchange the elements in the array

正确答案:7
解析: 解释:73和12进行交换,26和15进行交换,52和40进行交换,64和12进行交换,38和12进行交换,38和15进行交换,38和26进行交换,一共7次。
Exchange 73 and 12, exchange 26 and 15, exchange 52 and 40, exchange 64 and 12, exchange 38 and 12, exchange 38 and 15, exchange 38 and 26, totally 7 times.

  1. 填空(2分)

以数据集{4,5,6,7,10,12,18}为结点权值所构造的哈夫曼树,其带权路径长度为?

Construct a Huffman tree with the weights {4,5,6,7,10,12,18}. What is the weighted external path length?

正确答案:165
解析: 解释:根据哈夫曼树的构建算法构建该哈夫曼树。
Construct the Huffman tree according to the construction algorithm.

  1. 单选(2分)

一个深度为h的满k叉树,最多有多少个结点?(独根树深度为0)

For a full k-ary tree with depth h, how many nodes could it have at most? (the depth of a tree with only one node is 0)

A.(k^(h+1) - 1)/(k - 1)
B.k^(h-1)
C.k^h
D.(k^(h - 1)/(k - 1)

正确答案:A
解析: A、k0 + k1 + k2 + … + kh = (k^(h+1) - 1)/(k - 1)

  1. 填空(2分)

从空二叉树开始,严格按照二叉搜索树的插入算法(不进行旋转平衡),逐个插入关键码{15, 82, 10, 4, 55, 89, 29, 45, 54, 35, 25}构造出一颗二叉搜索树,对该二叉搜索树按照后序遍历得到的序列为(每两个元素之间用一个空格隔开)

Given a null binary tree, insert key values {15, 82, 10, 4, 55, 89, 29, 45, 54, 35, 25} successively according to the insertion algorithm of a binary search tree strictly (no rotation and balance) to construct a binary search tree. Please write down the sequence of post order of this binary search tree. (There is one blank space between two elements)

正确答案:4 10 25 35 54 45 29 55 89 82 15
解析: 解释:依次插入所有元素之后,得到这棵二叉搜索树,对其进行前序遍历即可得到答案。
We get the binary search tree after inserting all the elements, and then we get the answer through preorder traversal.

  1. 单选(2分)

对二叉排序树(即BST,也称“二叉搜索树”)进行什么 遍历,可以得到该二叉树所有结点构成的排序序列?

From which traversal can we get the ordered sequence of the nodes of a binary search tree?

A.前序 preorder
B.后序 postorder
C.按层次 levelorder
D.中序 inorder

正确答案:D
解析: D、中序遍历先访问左子节点,再访问自身,最后访问右子节点
inorder traversal first visits the left child (including its entire subtree), then visits the node, and finally visits the right child (including its entire subtree)
前序遍历先访问自身,再访问左子节点,最后访问右子节点
preorder traversal first visits the node, then visits the left child (including its entire subtree), and finally visits the right child (including its entire subtree)
后序遍历先访问左子节点,再访问右子节点,最后访问自身
postorder traversal first visits the left child (including its entire subtree), then visits the right child (including its entire subtree), and finally visits the node
层次遍历先访问根节点,然后按序访问所有第一层节点(深度为1),再按序访问所有第二层节点(深度为2),以此类推
Level order first prints the root, then all nodes of level 1, then all nodes of level 2, and so on

  1. 多选(3分)

2-3树是一种特殊的树,它满足两个条件:

(1)每个内部结点有两个或三个子结点;(2)所有的叶结点到根的路径长度相同;

如果一棵2-3树有10个叶结点,那么它可能有_________个非叶结点。 (多选)

2-3 tree is a special kind of tree, which satisfies:

(1) Every internal node has 2 or 3 children nodes. (2) All the leaf nodes have the same path length from the root node.

If a 2-3 tree has 10 leaf nodes, then it may have __________ non-leaf nodes. (multiple choice)

A.8
B.7
C.5
D.6

正确答案:A、B
解析: A、深度一定是3.
倒数第二层若是5个结点,倒数第三层(第二层)有2个结点,加上根结点,一共5+2+1=8个非叶子结点。
倒数第二层若是4个结点,倒数第三层(第二层)有2个结点,一共4+2+1=7个非叶子结点。
The depth must be 3.
If the second level from the bottom has 5 nodes, the third level from the bottom will has 2 nodes, and the tree will has 5+2+1=8 non-leaf nodes, including the root node.
If the second level from the bottom has 4 nodes, the third level from the bottom will has 2 nodes, and the tree will has 4+2+1=7 non-leaf nodes.

  1. 填空(2分)

对于以下等价类,采用“加权合并规则”(也 称“重量权衡合并规则”),进行并查运算,给出最后父结点索引序列。

1-2 5-1 1-6 0-3 7-4 6-9 5-3 0-8 4–8

注意:当合并大小相同的两棵树的时候,将第二棵树的根指向第一棵树的根;根结点的索引是它本身;数字之间用一个空格隔开

Given the following equivalence pairs, please use the “union-by-weight rule” and the UNION/FIND algorithm to obtain the final parent node index sequence.

1-2 5-1 1-6 0-3 7-4 6-9 5-3 0-8 4-8

Notice: When we merge two trees with the same size, we let the root of the second tree point to the root of the first tree. The parent index of the root node is itself. Separate the numbers with only one space.

正确答案:1 1 1 0 7 1 1 1 1 1
解析: Every time after union, the parent node index changed like the following sequence.
每次节点合并后的父节点索引如下:
[0, 1, 1, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 1, 3, 4, 1, 6, 7, 8, 9]
[0, 1, 1, 3, 4, 1, 1, 7, 8, 9]
[0, 1, 1, 0, 4, 1, 1, 7, 8, 9]
[0, 1, 1, 0, 7, 1, 1, 7, 8, 9]
[0, 1, 1, 0, 7, 1, 1, 7, 8, 1]
[1, 1, 1, 0, 7, 1, 1, 7, 8, 1]
[1, 1, 1, 0, 7, 1, 1, 7, 1, 1]
[1, 1, 1, 0, 7, 1, 1, 1, 1, 1]
[explanation]

  1. 填空(2分)

根据伪满二叉树的前序序列,求ltag-rlink的二叉树前序遍历

比如:给出伪满二叉树的前序序列如下:

A’ B’ D G’ / H C’ E’ F I /

则可以求出ltag-rlink的二叉树前序遍历为

0A5 0B3 1D-1 1G4 1H-1 0C-1 0E8 1F-1 1I-1

(注:各个结点按照“ltag结点名rlink”的方式给出,结点之间用一个空格分隔)

现给出伪满二叉树的前序序列如下:

A’ B’ C’ / I H D’ E’ G / F

则所求出ltag-rlink的二叉树前序遍历为

According to the pre-order traversal sequence of a “pseudo full binary tree”, please write down the pre-order traversal sequence of this binary tree in an “ltag-rlink” form.

For example: Given the pre-order traversal sequence of a “pseudo full binary tree” like this: A’ B’ D G’ / H C’ E’ F I /

Then we can get the pre-order traversal sequence of this binary tree in the “ltag-rlink” form: 0A5 0B3 1D-1 1G4 1H-1 0C-1 0E8 1F-1 1I-1

(P.S. The form of each node should be “LtagNodeRlink”, and all the nodes are separated by a single space.)

Now, given the pre-order traversal sequence of a “pseudo full binary tree” like “A’ B’ C’ / I H D’ E’ G / F”, please write down the pre-order traversal sequence of this binary tree in the “ltag-rlink” form.

正确答案:

 0A5 0B4 1C3 1I-1 1H-1 0D8 0E-1 1G-1 1F-1
  1. 单选(2分)

若无向图G的顶点度数最小值大于等于x时,G至少有一条回路。求x的最小值。

If the minimum degree of vertices of undirected graph G is bigger than or equal to x, and G have at least one circuit. What is the minimum value of x?

A.1
B.2
C.3
D.4

正确答案:A

  1. 单选(2分)

用相邻矩阵A表示图,判定任意两个顶点Vi和Vj之间是否有长度为m的路径相连,则只要检查_________的第 i 行第 j 列的元素是否为零即可。

Using an adjacency matrix A to represent a graph, we just need to check whether the element in the row i, column j of ________ is zero to tell whether there is a path with length m connecting vertex Vi and Vj.

A. A^m
B. mA
C. A
D. A^m-1

正确答案:A

  1. 填空(2分)

请使用Kruskal算法求出下图的最小生成树,依次写出每次被选择的合法的合并代价最小的边的编号,用一个空格分隔(如果同时存在多条边满足要求,选择编号最小的)。顶点a到顶点b(a<b)之间的边编号为ab,例如图中权值为1的边编号为45。

Please use Kruskal algorithm for the following graph to find the minimum spanning tree. Write down the edge labels which are selected in the minimum spanning tree one by one (if there are multiple valid edges, select the vertex with the minimum label). The label of an edge connecting vertex a and vertex b is ab (a<b). For example, the edge with a weight of 1 in the graph is labeled 45. (Separate different labels with a single blank space.)

正确答案:45 35 14 25 02

  1. 填空(2分)

求图的中心点。设V是有向图G的一个顶点, V的偏心度定义为:

Find the central point of the graph. Let V be a vertex of the graph G, the definition of the eccentricity of V is:

max{dist(w,v),∀w∈V(G)}

如果v是有向图G中具有最小偏心度的顶点,则称顶点v是G的中心点。

If the eccentricity of v is minimal in the graph G, then we call V a central point of G.

请从以下代码语句中选择正确的5条,填入空白处。按空白的标号顺序依次列出代码语句的标号,用一个空格分隔。如A F D H C

Please choose 5 statements from the following, and put them into the blanks.

List the number of the statement you choose according to the order of the blanks, and separate them with a single blank space. For instance, A F D H C.

C++代码:

void FLOYD_PXD(AdjMatrix g){ // 对以带权邻接矩阵表示的有向图g,求其中心点。
    AdjMatrix w = g;
    for(k = 1; k <= n; k++)
        for(i = 1; i< = n; i++)
            for(j = 1; j <= n; j++)
                if( (1) )
                    (2)  ;
    v = 1;
    dist = MAXINT;
    for(j = 1; j <= n; j++){
        s = 0;
        for(i = 1; i <= n; i++)
            if( (3) )
                s = w[i][j];
        if( (4) ){
            dist = s;
            (5)  ;
         }
    } //for
    printf("有向图g的中心点是顶点%d,偏心度%d\n", v, dist);
}

Python代码:

def FLOYD_PXD(AdjMatrix g):
    AdjMatrix w = g
    for k in range(1, n+1):
        for i in range(1, n+1):
            for j in range (1, n+1):
                if ( (1) )
                    (2)
    v = 1
    dist = MAXINT
    for j in range(1, n+1):
        s = 0
        for i in range(1, n+1):
            if ( (3) ):
                s = w[i][j]
        if ( (4) )
            dist = s
            (5)
    print("有向图g的中心点是" + str(v) + ",顶点偏心度" + str(dist))

选项:
在这里插入图片描述

正确答案:A D E M K

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值