Sample Final Exam

advertisement
Spring 2, 2003
07/01/16
Data Structures
Name: __________________
FINAL EXAM
Part 1- Theoretical Knowledge
1). [20 pts] Assume you have the following linked list structure:
struct node
{
char data[51];
node *link;
};
node * Head;
And the following linked list data, where Head points to Anne:
Head
data : Anne
data: Paul
data : Jim
data: David
The following is a modified linked list traversal function that goes through the linked list
using recursion, there is a slight catch, though…
void Traverse(node *current)
{
if(current != NULL)
{
cout << “Begin Traverse.” << endl;
cout << current->data[0] << endl;
Traverse(current->link);
Cout << current->data[2] << endl;
cout << “End Traverse.”;
}
}
// print 1st char
// print 3rd char
If Head points to Anne, what is the OUTPUT (if any) generated by this recursive call:
Traverse(Head->link);
Spring 2, 2003
07/01/16
3).
Data Structures
Name: __________________
[20 pts] Assume you have the following data streams. Load each into their own binary
search trees (non-balancing):
a).
H E M B F I P A C G K N Z J L
b).
H M G P C K Z L E B I A F N J
c).
What is the worst-case number of tries it would take to find an item in each of
these trees A and B? Which search tree is more efficient (if any) based on your
answer?
d).
Delete the node ‘H’ from tree (a) and delete the node ‘F’ from tree (b). Show the
resulting trees:
Spring 2, 2003
07/01/16
4).
Data Structures
Name: __________________
[20 pts] Traverse the following tree using the method indicated below:
.2.
.3.
.6.
.4.
.9.
.2.
.3.
.8.
.1.
.8.
.7.
.4.
.3.
.2.
.5.
.3.
.5.
.4.
.6.
.3.
a).
What is the post-order traversal?
b).
What is the pre-order traversal?
c).
What is the in-order traversal?
.4.
.7.
.9.
.8.
5). [10 pts.] Below each tree, indicate whether it has the Heap property, the Ordered property, or Both, or None
9
7
/ \
7
/ \
0
3
9
/ \
\
3
9
/ \
/
2
4
9
7
/ \
/
5
8
6
/ \
4
4
/ \
2
2
8
\
3
9
Spring 2, 2003
07/01/16
Data Structures
Name: __________________
Part II : Programming Knowledge:
6). [40 pts] Suppose you had a standard binary tree definition of a Node structure and a root.
struct Node
{
int data;
Node *left;
Node *right;
};
Node * root;
And the following tree data, where root points to the topmost 2:
.2.
.3.
.4.
.9.
.6.
.2.
.3.
.8.
.1.
.7.
.5.
.4.
.3.
Write a recursive linked list print function that performs an in-order traversal, but ONLY
prints nodes up to a specified DEPTH. That is, if you only printed all nodes up to a depth of 3
(levels 0-2) up in-order in the above tree, you would get:
shallowTraverse(root, 3)

4
3
2
2
Here’s the prototype:
void shallowTraverse (node *current, int numLevels);
8
6
5
Spring 2, 2003
07/01/16
Data Structures
Name: __________________
Part III: Short Answer. Please answer in complete
sentences.
9). [5 pts] Why don’t we just allow the user to see the pointers to nodes in our linked list
instead of iterators?
10). [10 pts] In our Linked List in class and our Binary Search Tree, we chose to make Node
defined as private. Why is that? Name 2 reasons why it is preferred that Node should be
private.
11).
[20 pts EXTRA CREDIT] Choose ONE of the following for extra credit:
11 a). Given the follow post-order traversal and in-order traversal, rebuild the tree:
In-Order 
Post-Order 
WXBZYUTARQLSDCM
WXZUTYBQSLRMCDA
11 b). There is one sort that is relatively easy to write using a Linked List, and that’s an insertion sort,
because it requires no direct access. Utilizing what you know of insertion sort from Algorithms,
and assuming insertInOrder exists for your list, write a function that performs an insertion sort
(should take < 10 lines of code):
Spring 2, 2003
07/01/16
Data Structures
Name: __________________
Part IV: Multiple Guess. Choose BEST answer. [2 pts each]
12). If you wanted to use a data structure to model a list of messages that you need to process, but you may
suddenly get a backlog, so you must store them and handle them in-order, you would probably use a:
a). Stack
b). Queue
d). Linked List c). Tree
e). Array
13). Assume you wanted to store a list of data that allows inserts, and deletions, and searching is a high priority
that must be done in an efficient manner. Which data structure would be best:
a). Stack
b). Queue
c). Linked List d). Tree
e). Array
14). If you were creating a game that would model a hand of cards, such that each card was in its proper place
and you could easy move from one card to the next and inserts and deletes were common, which data structure
would you use?
a). Stack
b). Queue
c). Linked List d). Tree
e). Array
15). If you wanted to store a series of data where you need direct access, and inserts and deletes are uncommon,
which data structure would be better?
a). Stack
b). Queue
c). Linked List
d). Tree
e). Array
16). Which of the following is NOT true about linked lists:
a). When you delete you must move items up to fill the gap in memory.
b). You cannot directly access any individual item without first traversing from the head or tail.
c). It is fairly easy to keep a list in sorted order with a linked list despite adds/deletes.
d). Linked lists are theoretically unlimited in the amount of data they can store.
17). A Node in a tree with no children is said to be:
a). A leaf node
b). A branch node
c). A sappling node
d). Loner
18). A Node that is not a leaf node is called:
a). The root node
b). An exterior node
c). Carnivorous
d). None of these
19). A binary search tree only has the ordered property and cannot have the heap property by definition, is this true or
false?
a). True
b). False
20). Which of the following is NOT true about the structure of Trees
A).
Trees are a type of graph with no loops or cycles.
B).
The root node has no ancestors
C).
A leaf node has no descendants
D).
Siblings cannot be connected, but cousins can
Spring 2, 2003
07/01/16
Data Structures
Name: __________________
Spring 2, 2003
07/01/16
Data Structures
Name: __________________
Part II: Multiple Guess. Choose BEST answer. [2 pts each]
2. Given the following class definition:
class foo
{
int num;
public:
void x(foo f);
void y(const foo f);
void z(foo f) const;
};
...
How many member functions can alter the PRIVATE member variable num of the foo
object that calls the function?
A. Only x can alter the private member variable num
B. Only y can alter the private member variables num
C. Only z can alter the private member variables num
D. Two of the functions can alter the private member variable num
E. All of the functions can alter the private member variables num
3. When should you use a const reference parameter?
A. Whenever the data type might be many bytes.
B. Whenever the data type might be many bytes, the function changes the parameter
within its body, and you do NOT want these changes to alter the actual argument.
C. Whenever the data type might be many bytes, the function changes the parameter
within its body, and you DO want these changes to alter the actual argument.
D. Whenever the data type might be many bytes, and the function does not change the
parameter within its body.
4. What is the primary purpose of template functions?
A. To allow a single function to be used with varying types of arguments
B. To hide the name of the function from the linker (preventing duplicate symbols)
C. To implement container classes
D. For designing classes that cannot be overloaded.
5. Consider this prototype for a template function:
template <typename T>
void foo(T x);
Which is the right way to explicitly call the foo function with an integer argument i?
A. foo<int>( i );
B. foo<Item>( i );
C. foo(<int> i );
D. foo(<Item> i );
Spring 2, 2003
07/01/16
4).
Data Structures
Name: __________________
[10 pts] Given the adjacency matrix below, construct the graph.
B C D E
F
G H
1
0
0
0
1
0
0
1
0
0
0
0
0
0
0
0
A
A
B
C
D
E
F
G
H
0
1
0
0
1
1
0
1
1
0
0
1
0
0
0
0
0
0
0
0
1
0
0
0
0
1
0
0
0
1
0
1
1
0
1
0
0
0
0
0
1
0
0
1
0
1
0
0
Spring 2, 2003
07/01/16
5).
Data Structures
Name: __________________
Given the graph you created from #4 above, answer the following questions:
a).
[2 pts] Is the graph directed?
b).
[2 pts] How can you tell (on a. above)?
c).
[2 pts] What word is used to describe nodes like G in this graph?
d).
[4 pts] What is the mathematical definition of this graph?
e).
[2 pts] Does this graph have at least one cycle? If so, name one:
f).
[2 pts] What is the degree of vertex A?
g).
[2 pts] Is this graph strongly connected, weakly connected, or disjoint?
h).
[4 pts] Give a breadth-first traversal of this graph starting at B:
i).
[4 pts] Give a depth-first traversal of this graph starting at F:
j).
[2 pts] Does this graph have at least one loop? If so, name one:
Spring 2, 2003
07/01/16
Data Structures
Name: __________________
Spring 2, 2003
07/01/16
6).
Data Structures
Name: __________________
[10 pts] Given the mathematic graph below, construct the graph:
V= {A,B,C,D,E,F}
E= {AB, AC, CD, DE, CE, FB, FA}
7).
Given the graph you created from #4 above, answer the following questions:
a).
[2 pts] Is the graph directed?
b).
[2 pts] How can you tell (on a. above)?
c).
[2 pts] Does this graph have at least one cycle? If so, name one:
d).
[4 pts] What is the indegree and what is the outdegree of vertex F?
e).
[4 pts] Give a breadth-first traversal of this graph starting at node A:
f).
[4 pts] Give a depth-first traversal of this graph starting at node F:
Spring 2, 2003
07/01/16
8).
Data Structures
Name: __________________
Give the graph below, answer the following questions:
A
E
H
B
I
G
C
D
J
F
8a).
[10 pts] Construct an adjacency matrix for this graph
8b).
[4 pts] What is the depth-first traversal of this graph starting at node D?
8c).
[4 pts] What is the mathematical definition of this graph?
Spring 2, 2003
07/01/16
9).
Data Structures
Name: __________________
Give the graph below, answer the following questions:
6
A
E
1
1
4
B
3
I
G
2
3
7
8
6
3
D
C
1
J
F
H
9
9
9a).
[10 pts] Construct the minimum spanning tree for this graph:
9b).
[10 pts] Construct the least cost path from D to J. SHOW WORK!
Spring 2, 2003
07/01/16
Data Structures
Name: __________________
Part II: Multiple Guess. Choose BEST answer. [2 pts each]
10). Which sort would stop immediately after 1 pass if the data were already sorted?
a).
bubble sort
b).
selection sort
c).
insertion sort
d).
quick sort
11).
Of the following sorts, which one is the worst in terms of complexity theory?
a).
bubble sort
b).
quick sort
c).
shell sort
d).
None, they are all O(n2) sorts.
12).
Which sort does C++ have built into its language library?
a).
shell sort
b).
bubble sort
c).
quick sort
d).
heap sort
13). Which sort operates by dividing a list into two halves, with all elements smaller than the
median on one side, and all greater than the median on the other?
a).
bubble sort
b).
selection sort
c).
insertion sort
d).
quick sort
14). Which sort operates by comparing consecutive pairs of numbers and swapping those that
are not arranged properly.
a).
bubble sort
b).
selection sort
c).
insertion sort
d).
quick sort
15). Which sort operates like a person picking up one card at a time, and inserting it into the
proper place in her/his hand?
a).
bubble sort
b).
selection sort
c).
insertion sort
d).
quick sort
Spring 2, 2003
07/01/16
Data Structures
Name: __________________
16). Which sort divides a list into intervals, which are spread throughout the list. These
intervals contract after each pass.
a).
bubble sort
b).
selection sort
c).
insertion sort
d).
shell sort
17). Which one of the following sorts will guarantee that the smallest item is in position 0 of
the array after the first pass?
a).
bubble sort
b).
insertion sort
c).
selection sort
d).
quick sort
18). Which sort guarantees that after n passes, the first n items in the list are sorted, but only in
relation to themselves.
a).
bubble sort
b).
selection sort
c).
insertion sort
d).
quick sort
e).
shell sort
19). True or false: In a non-directed, strongly connect graph, a depth first traversal will
eventually reach every node:
a).
True
b).
False
20).
True or false: In any digraph, a breadth first traversal will eventually reach every node:
a).
True
b).
False
21). Given that you have a non-directed graph of 20 nodes, you perform a depth first traversal
starting at node A. It comes up with one and only one node, which was the starting node A.
What must be true about node A?
a).
A has a loop.
b).
A has connections coming in, but not going out.
c).
A has a cycle.
d).
This is impossible, unless A is the only node in a non-directed graph
e).
A is isolated.
Spring 2, 2003
07/01/16
Data Structures
Name: __________________
Download