Exam 3

advertisement
CS 240 Computer Science II Exam 3
Page 1
April 4, 2012
Name_____________________________
1. . For each data structure in the table, give the complexity, O(1), O(log n), or O(n) for each of the operations
[16 pts]
Find last
Search for an
Traverse the
Insert an
element
arbitrary
list
element
element
Linked List (unsorted)
Unsorted array
Sorted array
Binary Search Tree
(inorder)
2. Fill in the blanks to implement a recursive algorithm to find the longest of the strings in a linked list. The
public helper function that calls the recursive function is given:
[10 pts]
public _________ getLongest(){
return getLongest(head);
}
private ___________ getLongest(LLNode cur){
if( ________ == _______ ) {//base case: end of list or empty
return ””;
}
String long = ________________ (__________ .getNext());
if(long._______________(____________.getData() > 0){
return ______________________;
} else {
return ______________________;
}
}
3. Convert the following normal infix expression to prefix and postfix. Account for proper operator precedence.
Be sure the operands in the expressions (a,b,c,d,e) are in the same original order.
[12 pts]
Draw the expression tree for a + b / c * d - e :
Prefix (pre-order traversal):________________________________
Postfix (post-order traversal):________________________________
CS 240 Computer Science II Exam 3
Page 2
4. Show the output of the following ancestor traversal routine against the data to the right with a call to
Ancs(BobHowe). Note carefully the order of the recursive calls and note mothers and fathers are not
consistently left or right.
[8 pts]
public static void Ancs(Relative inx)
{
if(inx==Unknown){
return;
} else {
System.out.println(inx);
Ancs(father[inx]);
Ancs(mother[inx]);
}
}
5. Trees. Answer the questions based on the tree to the right.
[12 pts]
a) Assume the root is at level 0. What is the level of node I? ____
b) Which node is the root of the largest subtree that is a binary tree?
_____
c) If each node were limited to two children, how many nodes total could
be stored in such a binary tree without adding any more levels? ______
d) How many non-terminals are there in this tree? _____
e) List the ancestors of node H ______________________
f) List the descendants of node F. _______________________
A
/ \
B
C
/ \ \
D
E
F
/ \
/|\
G
H
I J K
/ \ |\
L
M N O
g) Draw the conversion of this general tree as a binary tree (left child as first child of the general tree and
right child as a sibling in the general tree)
CS 240 Computer Science II Exam 3
Page 3
6. Create a binary search tree from the following 10 characters entered into a BST left to right.
[4 pts]
LINKPAUSED
List the data of your tree above from a inorder traversal.
[4 pts]
8. Below is the method for pop in an array implementation of an Object stack. Fill in the code to complete the
implementation. An instance variable top is part of the stack class and is initialized to -1.
[10 pts]
public _________ pop(){
if ( ____________________ )
________ new StackUnderflowException("Stack is empty");
_________ temp = stack [__________];
________ = _________ ____ 1 ;
return _________;
}
9. More Unix concepts. Give the command to carry out the tasks described.
[10 pts]
Get a word and line count of all your java source files. ___________________________________
Capture a long, detailed listing of your files stored in a file called directory _____________________________
Search your java source files for the string LLNode __________________________________________
You want to create an alias for a complex command. You store the alias command in the file______________
To activate the changes in the file above, ________________________________
To run the java program bigoutput and control the output page by page ________________________________
CS 240 Computer Science II Exam 3
Page 4
10. Answer the following questions about the refactoring of classes for image and sound media
[14 pts]
True/false:
______ An object can be refered to by an LLNode variable.
______ DrawableNode is a concrete class.
______ CollectableNode contains methods common to SoundBranch and SoundNode and ScaleBranch.
______ Branch extends its superclass to a tree structure.
______ A SoundNode object’s call to getNext() would return a SoundNode object. getNext() is inherited from the
class LLNode.
______ This class hierarchy prevents us to mix sounds and images into a common linked list.
Assume we three basic images s,t,u that we want to put into a scene graph: We want three instances of s drawn
horizontally, 2 instances of t drawn vertically, one instance u and 2 more instances of s drawn vertically.
Draw a tree of these sounds using the appropriate classes.
Download