Chinese University MOOC- Chen Yue, Ming Chin Ho - data structures -2019 Spring Final Exam (subject + partial answer)


First, determine the question

Topic ideas normal.

Example 1-4 trans: only the four vertices of communication can be done with the full three sides. Therefore, the number of sides may be equal to the number of vertices minus one. error.

 

Second, multiple-choice questions

 

 

2-8 need to do some careful ambidextrous operation,

 

 

2-12 C

Re-hash table that is double the expansion of the original length, so that the loading factor (α) in half of the process. And because the load factor is desired inverse relationship between (a graph data structure analysis third edition chapter reference hash performance) search length, α is reduced so that a hash lookup can increase the efficiency.

After this length of the table in question should be re-hash 20, but larger than the first to take a prime number 20 , the length of the table 23 to take more appropriate. So choose C, instead of A.

 

This controversial question 2-14 C, D had misread the election, meaning the reading should choose C? Gangster seek correction.

 

 

 

2-20 triangle inequality also applies in graph theory, both sides must be greater than the sum of the third side.

After 2-21 FIG degenerate into linear table, stacks and queues on the same topological sort.

 

 

Third, the program fill in the blank

++counter //TopNum starts counting from 1

--Indegree[W]

 

 

The application heap is very smart, is to find the smallest possible number of K, and then choose one of the largest in which the first number is the number of small K.

The establishment of a K elements of the big top of the heap using the transport properties inequality. If the number is greater than a top of the stack, the stack must be greater than that which is below the top element, that the first number is at least a small number of K + 1 (1 + as the number larger than K), so the number of cycle skip ; If the number is less than a top of the stack, the top of the stack to complete the replacement operation + adjustment of the maximum heap, the heap is always such that the maximum number is smaller pile.

 

 

Fourth, the program title

7-1 and in accordance with subsequent output preorder traversal order (8 point (s))

This problem requires preorder traversal results based on the given sequence and a binary output preorder traversal of the tree results.

Input formats :

第一行给出正整数N(≤30),是树中结点的个数。随后两行,每行给出N个整数,分别对应后序遍历和中序遍历结果,数字间以空格分隔。题目保证输入正确对应一棵二叉树。

输出格式:

在一行中输出Preorder:以及该树的先序遍历结果。数字间有1个空格,行末不得有多余空格。

输入样例:

7
2 3 1 5 7 6 4
1 2 3 4 5 6 7

输出样例:

Preorder: 4 1 3 2 6 5 7

陈越老师一道讲过的课后习题

 

思路:

申请三个全局数组。PostOrder、InOrder、PreOrder。

依据PostOrder找到根节点后记录,查找InOrder中的根节点,把InOrder原序列划分成左右两个子序列;递归解决问题。

 

参考代码:

 1 #include <cstdio>
 2 #define MAXN 50
 3 
 4 int pre[MAXN], in[MAXN], post[MAXN];
 5 
 6 
 7 
 8 void InputPostAndIn(int N);
 9 void solve(int preL, int inL, int postL, int n);
10 void OutputPost(int N);
11 
12 int main()
13 {
14     int N; scanf("%d", &N);
15     InputPostAndIn(N);
16     solve(0, 0, 0, N);
17     OutputPost(N);
18 }
19 
20 void InputPostAndIn(int N)
21 {
22    
23     for (int i=0; i<N; i++) {
24         scanf("%d", &post[i]);
25     }
26     for (int i=0; i<N; i++) {
27         scanf("%d", &in[i]);
28     }
29     
30 }
31 
32 void solve(int preL, int inL, int postL, int Num)
33 {
34     if (Num == 0) {
35         return;
36     }
37    
38     int root, i, LTreeNodeNum, RTreeNodeNum;
39     root = post[postL + Num - 1];
40     pre[preL] = root;
41     
42     for (i=inL; in[i]!=root; i++) {}
43     
44     LTreeNodeNum = i - inL; RTreeNodeNum = Num - LTreeNodeNum - 1;
45     
46     solve(preL+1, inL, postL, LTreeNodeNum);
47     solve(preL+1+LTreeNodeNum, inL+1+LTreeNodeNum, postL + LTreeNodeNum, RTreeNodeNum);
48 }
49 
50 void OutputPost(int N)
51 {
52     printf("Preorder: ");
53     printf("%d", pre[0]);
54     for (int i=1; i<N; i++) {
55         printf(" %d", pre[i] );
56     }
57     printf("\n");
58 }
View Code

 

Guess you like

Origin www.cnblogs.com/acoccus/p/10957065.html