Download 105-1 Data Structures Midterm Exam 系級: 學號: 姓名: 1. Rank the

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Lattice model (finance) wikipedia , lookup

Red–black tree wikipedia , lookup

Array data structure wikipedia , lookup

Quadtree wikipedia , lookup

Interval tree wikipedia , lookup

B-tree wikipedia , lookup

Binary tree wikipedia , lookup

Binary search tree wikipedia , lookup

Linked list wikipedia , lookup

Transcript
105-1 Data Structures Midterm Exam
系級:
學號:
姓名:
1.
Rank the following functions f1…f5 by order of growth. (5%)
If 𝑓𝑖 =O(𝑓𝑗 ) (i!=j), then 𝑓𝑖 < 𝑓𝑗 .
𝑛
𝑓1 = log (n!), 𝑓2 = n log n, 𝑓3 =22 , 𝑓4 =(log n)!, 𝑓5 =(3/2)𝑛
Please compare them.
𝑓3 > 𝑓5 > 𝑓4 > 𝑓1 = 𝑓2
2.
A circular queue can be implemented using an array. Two integer variables, namely,
start and end, can be used to keep track of the first element and the last element of the
queue.
Without using any additional variable, how can a full queue be distinguished from an
empty queue? (7%)
full: (end+1) % n == start
empty: start == end
3.
Consider the following function F written in a C-like pseudo-code, which takes an
array A of n positive integers and an initially-empty stack S as input parameters:
int F (A: array, S: stack) {
int i, t=0;
for (i=0 ; n-1 ; i++) {
if (A[i]%3==0)
push(S, A[i] ) ;
else
while (S is not empty)
t = t + pop(S) ;
} // end of for-loop
while (S is not empty)
t = t + pop(S) ;
return t ;
}
What is the output (returned value) of the function F for the array A = {6, 15, 3, 7, 12,
18, 10, 15, 6, 27, 12, 8}? (10%)
6 + 15 + 3 = 24
12 + 18 = 30
15 + 6 + 27 +12 = 60
24 + 30 +60 = 114
4.
Let f(n) be a function where the argument of the function are the natural number, i.e.,
n>=1. The function is defined as below:
f(n)=1 if n=1
f(n)=f(n-1)+2𝑛−1 , for all n>=2
Let R=f(12)-f(8), what is the value of R? (8%)
0
f(n) = 2 + … + 2
𝑛−1
=
20 (2𝑛 −1)
2−1
= 2𝑛 -1
f(12) – f(8) = (4096-1) – (256-1) = 3840
5.
The given figure uses an array to simulate a linked-list (first column stores data and
second column stores the position of the next data). Based on the figure, please answer
the following questions:
node
data
next
[0]
66
-1
[1]
25
-1
[2]
9
0
[3]
33
8
[4]
11
1
[5]
10
9
[6]
7
2
[7]
18
4
[8]
21
6
[9]
48
7
(1) The first element of the First list is stored in node[3].data. Show the First list. (5%)
(2) There is a second list also stored in the array. Where is the first element stored in
the array. (5%)
6.
The time complexity to find an arbitrary element in a singly linked list is O(n), while
the time complexity to find an arbitrary element in an array is O(log n), where n is the
number of elements in the list and array.
True or False. Please write the reason. (5%)
False. array 不一定是 sorted array
7.
Convert the expression ((A + B) * C – (D – E) ^ (F + G)) to equivalent Prefix and
Postfix notations. (6%)
Prefix: – * + ABC ^ – DE + FG
Postfix: AB + C * DE – FG + ^ –
8.
Please use a stack to illustrate how to evaluate the postfix expression “ 62/3–42*+ ”
step by step. Please draw the stack status after each step. (6%)
9.
We need a data structure to represent a “boss” relation. Every node in this data structure
has two fields – a name and a pointer to another node. For example, if John works for
Mary, then the pointer of the node representing John will point to the node representing
Mary. In other words, Mary is John’s boss. For ease of representation if a person does
not work for anyone, we set his/her “boss” pointer to himself/herself, and call him/her
a “super-boss”. Since every node has a pointer, everyone has at exactly one boss. As a
result if we trace the boss of the boss and keep on tracing, eventually we will find a
super-boss. If two persons have the same super-boss, we call them “colleagues”.
(1) Now given the following “boss” relation, draw a picture of this data structure.
(10%)
John works for Mary
Tom works for Mary
Jimmy works for Eric
Joe works for Tom
Mary works for Kevin
Kevin works for Lucy
Joe works for Tom
George works for Tom
Barry works for Kevin
Adam works for Mary
Harry works for Tom
(2) Please indicate all colleagues of Mary. (5%)
10.
How many different binary trees are there with size n=5? (5%)
42
11.
The possible maximum height of a binary tree with 1000 nodes is “a”, but the possible
minimum height is only “b”. (Root level=1)
The possible maximum number of nodes in a complete binary tree with height 8 is “c”,
but the possible minimum number of nodes is only “d”. (Root level=1)
a=? b=? c=? d=? (8%)
a = 1000, b =「𝑙𝑜𝑔2 (1000+1)」= 10
c = 28 – 1 = 255, d = (27 – 1) + 1 = 128
12.
What is the running time of the following code fragment?
for (int i = 0 ; i < 10 ; i++)
for(int j = 0 ; j < N ; j++)
for(int k = N-2 ; k < N+2 ; k++)
cout << i << “ ” << j << endl;
Answer is like O(?). (5%)
10𝑁 2
O(𝑁 2 )
13.
Please fill out the blank. (5% for each)
Singly linked list insertion :
void insert(list_pointer *first, list_pointer x)
{
/* insert a new node with data = 50 into the list ptr after node */
list_pointer temp;
temp = (list_pointer) malloc(sizeof(list_node));
if (IS_FULL(temp)){
fprintf(stderr, “The memory is full\n”);
exit (1); }
temp->data = 50;
if (*ptr) { //noempty list
_____
temp->link =node ->link;
_____
node->link = temp;
}
else { //empty list
_____
temp->link = NULL;
*ptr =temp; }}
Singly linked list deletion :
void delete(list_pointer *ptr, list_pointer trail, list_pointer node)
{ /* delete node from the list, trail is the preceding node ptr is the head of the list */
if (trail)
_____
trail->link = node->link;
else
_____ //head *ptr =
free(node); }
(*ptr) ->link;