422Exam2-v3

advertisement
1
2
3
4
5
6
7
8
9
10
11
12
13
Exam 2
EE 422C - University of Texas at Austin - Spring 2015
Name ___________________________ EID __________________ Lab day/time_____________
Test taking instructions. No calculators, laptops or other assisting devices are allowed. Write your
answers on these sheets. Wherever code is required, write JAVA statements in the blank areas
provided, or by modifying the given code in place. You are not required to follow the coding style
guidelines when writing code on the exam, but be as neat as possible. If you are unsure of the meaning
of a specific test question, then write down your assumptions and proceed to answer the question on
that basis. If you see a typo or syntax error, fix it, circle it, and if you are right you will get a bonus
point for each one fixed. In your programming solutions on this test you may use any of the
classes/methods that you know from the library, or JCF.
Questions about the exam questions will not be answered during the test.
For the binary tree related questions on this exam you may assume that this generic class has been
defined for your use and is included. When the term Btnode is used on the exam it is referring to
this generic class (e.g. in Question 5).
class Btnode<T>
{
/* this generic class models a node of a binary tree */
/* here are the private data members of the binary tree node */
private T element;
private Btnode<T> left;
private Btnode<T> right;
private Btnode<T> parent;
/* the constructor given an element,left child,right child & parent */
Btnode(T theElement, Btnode<T> lt, Btnode<T> rt, Btnode<T> pt )
{
element = theElement;
left
= lt;
right
= rt;
parent = pt;
}
/* here are the public get/set methods for the data members */
public T getElement( ){ return element;}
public Btnode<T> getLeft( ) {return left;}
public Btnode<T> getRight( ){return right;}
public Btnode<T> getParent( ){return parent;}
public void setElement( T x ){element = x;}
public void setLeft( Btnode<T> t ) {left = t;}
public void setRight( Btnode<T> t ){right = t;}
public void setParent( Btnode<T> t ){parent = t;}
}
Page 2
Question 1. Terminology. [22 pts.] Answer each of the following questions by
choosing the best answer from the list below.
A.
B.
C.
D.
E.
F.
G.
H.
I.
J.
K.
L.
M.
N.
O.
P.
Q.
R.
S.
T.
U.
V.
a.
b.
c.
d.
e.
f.
g.
h.
i.
j.
k.
l.
m.
n.
____A search algorithm that is O(log log n) for an ordered array
____A data structure used to convert an infix expression to postfix notation
____A sorting technique that requires NO comparisons
____A systematic approach to trial and error solution searching
____A data structure that has the same data structure as a component
____A collection of nodes with each one linked to its parent, and a single root on top
____A binary tree that represents variable length character codes for compression
____The average case search time in a Binary Search Tree (BST)
____The BST traversal that produces an ordered sequence
____A BST is best when inserts are at the leaves and there are not too many internal of these operations.
____On average log n + .25 comparisons are needed to insert into this type of BST.
____A data structure developed to store indexes into databases on disk
____A best sorting method in terms of worst case number of comparisons and minimum space utilization
____A control abstraction that splits a process into multiple parallel processes
____The synchronization problem that occurs when 2 threads want to use 2 resources, but each thread has 1
resource locked and needs the other resource
____A synchronized data structure that implements a queue that will wait if needed to perform enqueue and
dequeue operations
____A directed graph that contains no cycles
____The inventor of the shortest path algorithm for a weighted digraph
____The inventor of the minimum spanning tree algorithm for a weighted graph
____ Where the classes for drawing basic graphical shapes are found
____ For GUI’s the screen and various windows are made up of these individual elements
____ The language used to render web pages within a browser window
ArrayList
ArrayBlockingQueue
AVL
AWT
backtracking
B-Tree
DAG
deadlock
deletes
deque
Dijkstra
duplicates
expression tree
fork
o.
p.
q.
r.
s.
t.
u.
v.
w.
x.
y.
z.
aa.
bb.
cc.
hashing function
heapsort
HTML
Huffman
infinite loop
inorder
interpolation
LinkedList
LISP
List
load factor
makeHeap
mergesort
Newton
O(log n)
dd.
ee.
ff.
gg.
hh.
ii.
jj.
kk.
ll.
mm.
nn.
oo.
O(n)
pixels
Priority Queue
Prim
put
race condition
radix
recursive
Stack
topological sort
tree
TreeSet
Page 3
Question 2 - Multiple Choice. [2 pts. each - 8 pts total] For each of the following subparts, circle
the best or all correct answers as indicated.
A. Which of the following sorting algorithms have a average-case performance that is better than O(n2)?
i) Selection sort
ii) Bubble sort
iii) Shell sort
iv) Merge sort
v) Heap sort
vi) Quick sort
vii) Radix (bin) sort
B. Given three arrays with L, M and N elements respectively, estimate the running time for the following
algorithm in terms of the number of times that the do something step is executed:
repeat the following for items i from 1 to N for array1
repeat the following for items j from 1 to M for array2
repeat the following for items k from 1 to L for array3
do something
end repeat
end repeat
end repeat
i.
ii.
iii.
iv.
O (L+M+N)
O(N3)
O(L*M*N)
None of the above
C. Which of the following statements are true of heaps?
I.
II.
III.
IV.
V.
VI.
It is a binary tree
In terms of its shape, it must be complete
It can be either a maximal or a minimal heap
The value of the root has no relationship to the value of any other nodes
Is useful for implementing a stack
Is the most efficient representation for a priority queue
D. A method uses the selection sort algorithm to sort an array of doubles. The method takes 5 seconds to
complete given an array with 10,000 distinct elements in random order. What is the expected time in
seconds for the method to complete given an array with 30,000 distinct elements in random order?
I.
II.
III.
IV.
V.
10
25
100
20
None of the above
Page 4
Question 3. Recursion (6 points)
A. [4 pts] Write a recursive algorithm (not code) that determines whether a specified target character is
present within a string. It should return true if the target is found and false if not. The stopping conditions
should be:
 A string reference to null or a string of length 0, the result is false
 The first character in the string matches the target, the result is true
The recursive step would involve searching the rest of the string for the target.
B. [2 pts] What is returned by the method call a(2) ? _________
public int a(int x)
{
if(x > 20) return x;
else return x + a(x * x + 1);
}
Question 4 [4 Pts] You have an array with 1,000,000 distinct elements in random order.
You have to search the array 100 times to determine if a given element is present or not.
What will result in less work? Sorting the array with quicksort and then doing the searches using binary
search OR just doing the searches with linear search. (without sorting)
Justify your answer with calculations. Tip: log2 (1,000,000) ~= 20
Page 5
Question 5. Tree questions (10 pts)
A. (2 pts) Draw the binary tree created by the following statements. Btnode is defined on page 1.
Btnode<Integer> root, a, b, c, d;
d = new Btnode<Integer> (6, NULL, NULL, NULL);
c = new Btnode<Integer> (9, NULL, d, NULL);
b = new Btnode<Integer> (4, NULL, c, NULL);
a = new Btnode<Integer> (12, NULL, NULL, NULL);
root = new Btnode<Integer> (10, a, b, NULL);
B. (3pts) Fill in the statements in the method below to perform an inorder traversal of a binary tree like the above. Print
out the value of the node’s element as it is visited inorder.
public static void inorder( Btnode<Integer>
{ if (tree == NULL) return;
else
{
tree )
}
}
C. (5 pts) Consider the following binary search tree. Use the original tree below when answering each subpart (a)
through (e).
50
25
20
15
35
(a)
(b)
(c)
(d)
(e)
70
90
23
30
5
80
18
45
65
100
75
68
95
150
If the value 54 is inserted into this tree, which node becomes its parent? __________________
If the value 27 is inserted into this tree, which node becomes its parent? ___________________
If we delete node 70, which node should be its replacement node? ___________________
If we delete node 25, which node should be its replacement node? __________________
If we delete the root node 50, which node should be selected as its replacement node so that the fewest number of
changes are made to the tree? ______________________
Page 6
Question 6. Heaps and Graphs (16 pts)
A. (3 pts) A heap can be represented by an ArrayList with the values stored in top down, level-by-level, left to right order.
Start with the following maximum heap and list the elements after each operation. Execute the operations
sequentially, using the result of the previous operation. The initial ArrayList values for this heap are
{50, 35, 15, 12, 3, 5}.
50
15
35
12
3
5
(a) Insert 17: The values in the ArrayList are now {____________________________________}.
(b) Delete (pop) an element from the heap: The values are now {_________________________________}.
B.(3 pts) Start with following tree and use the heapify algorithm to convert it into a maximum heap. Draw the resulting tree.
75
30
55
15
40
35
20
90
10
45
C. (2 pts) Show the minimum cost path (and the total cost) from node A to node E in the following digraph G.
A
1
B
2
5
2
C
3
D
7
1
E
1
D.(3 pts) Draw the adjacency list representation for the following digraph G.
A
1
B
2
5
2
C
3
D
7
1
E
1
Page 7
E. (5 pts) Draw the minimal spanning tree for the following graph G. What is the total cost?
0
6
5
8
1
5
2
3
5
6
3
2
4
4
65
5
Question 7 (4 pts) AVL Trees
a. [2 pts] Show that the below tree is indeed an AVL tree by writing in the balance indicators for each
node of the tree (hint: balance indicator = hr – hl )
jumps
quick
brown
The
fox
dog
over
the
lazy
b. [2 pts] Now insert the word “keen” into the above tree and rebalance the tree as necessary so that it
remains an AVL tree. Draw the new tree below.
Page 8
Question 8 (5 Pts) For the graph shown below determine the visit sequence for a breadth-first
search beginning at node 3. Show the changing queue contents for partial credit.
5
0
4
3
1
2
6
7
9
8
Visit sequence = _____________________________________________________________________________________
Question 9. [5 Pts] Given the Huffman code tree below, what does the given bit stream decode to
X
D
C
W
K
Given bit stream (spaces added for clarity): 1 1 0 1 0 0 0 0 1 0 0 1 1 0
Page 9
Question 10 [6 Pts].
For the following digraph, use Diykstra’s algorithm to show the minimum cost path from node A to all
other nodes. I have provided the initial table for A to all other vertices (v), and the initial d[v] and p[v]
pairs. Recall that d[v] is the distance/cost of getting from A to v, and p[v] is the immediate predecessor
node of v on the path from A to v. You will then derive the final table of d[v] and p[v] pairs which show
the minimum cost paths and the immediate predecessor nodes. Show your intermediate work for getting
from the initial table to the final table for partial credit.
C
5
5
30
10
A
10
D
20
B
20
40
5
E
Initial Table
v d[v] p[v]
B 10 A
C ∞
A
D ∞
A
E 40 A
Final Table
v d[v] p[v]
B
C
D
E
Page 10
Question 11 [5 pts]
Consider the following applet with an embedded thread. Describe the order
in which ALL the various methods are executed by the JVM.
import java.awt.*;
import java.applet.*;
/* <applet code="Banner" width=300 height=50> </applet> */
public class Banner extends Applet implements Runnable
{
String msg = " A Moving Banner.";
Thread t = null;
int state;
boolean stopFlag;
public void init()
{ setBackground(Color.cyan);
setForeground(Color.red);
}
public void start()
{ t = new Thread(this);
stopFlag = false;
t.start();
}
public void run()
{
// Display banner
while (true)
{ try
{ repaint();
Thread.sleep(250);
if(stopFlag)
break;
}
catch(InterruptedException e) {}
}
}
public void stop()
{ stopFlag = true;
t = null;
}
public void paint(Graphics g)
{ char ch;
ch = msg.charAt(0);
msg = msg.substring(1, msg.length());
msg = msg + ch;
g.drawString(msg, 50, 30);
}
}
Page 11
Question 12 [5 Pts] Consider the following mouse event handling code.
Add in the code necessary
to draw a small square at the position where the mouse was clicked in the applet window. Also describe
in words the purpose of the highlighted code statements.
import
import
import
import
import
java.applet.Applet;
java.awt.Graphics;
java.awt.Graphics2D;
java.awt.event.MouseListener;
java.awt.geom.*;
public class MouseSpyApplet extends Applet
{
public MouseSpyApplet()
{ MouseSpy listener = new MouseSpy();
addMouseListener(listener);
}
public void paint(Graphics g)
{ Graphics2D g2 = (Graphics2D)g;
// fill in the rest of this method
}
class MouseSpy implements MouseListener
{ public void mouseClicked(MouseEvent e)
{System.out.println
("Mouse clicked. x=" + e.getX() + " y=" + e.getY());
}
// assume empty bodies for other 4 mouse listener methods
}
}
Question 13. Multi-threading basics (4 pts)
For the design of your assignment 6 ticket office program, answer the following questions.
a. (2 pts) What constitutes a race condition? How can/did you avoid that in the current
assignment?
b. (2 pts) What constitutes a deadlock situation? How can/did you avoid that in the current
assignment?
Download