Download Exam 3

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

Linked list wikipedia , lookup

Lattice model (finance) wikipedia , lookup

Quadtree wikipedia , lookup

Red–black tree wikipedia , lookup

B-tree wikipedia , lookup

Interval tree wikipedia , lookup

Binary tree wikipedia , lookup

Binary search tree wikipedia , lookup

Transcript
CS 240 Computer Science II Exam 3
Page 1
Spring ‘04
March 31, 2004
(100 points out of 105)
Name_____________________________
1. Linked lists.
a) Draw a box and pointer diagram to represent the ordered list (“cat”, “dog”, “fish”) as pointed to by
head. The linked list starts with a dummy head node. Make the list circular.
[5 pts]
b) Supply the declarations and assignment statements in the Java LList class below to support the
implementation of a simple (non-circular) linked list in which each node holds one Listable item
and a next reference.
[12 pts]
public class LList implements ListInterface
private class Node {
_________________________________
__________________________________
private Node(Listable s){
______________________________
______________________________
}//end constructor
}//end Node inner class
// define and initialize the head pointer
______________________________
public LList(){
__________________________________
}//end constructor
}
c) Show the Java code that would add the value “ant” as a new first node of this ordered list (push).
_______________________________
_______________________________
_______________________________
[6 pts]
CS 240 Computer Science II Exam 3
Page 2
2. Fill in the code below to complete the dequeue operation using a singly linked list implementation (no dummy
head node). Assume head and tail are Node pointer instance variables for the queue.
[7 pts]
public Listable dequeue(){
if (______ == null)
throw new IllegalArgumentException
("Trying to dequeue an empty queue");
Node node = ________;
head = _________._________;
size_____;
if (size == 0){
tail = _______;
}
return ___________;
}
3. Give the complexities, linear or constant for each of the operations of the implied data structure that are
implemented as a linked list.
_________ dequeue()
[8 pts]
_________ insert(item) //into a sorted list
_________ enqueue(item)
_________ insert(item) //unsorted list
_________ pop()
_________ getNextItem()
_________ delete(item)
_________ isThere(item)
4. Fill in the blanks to implement a recursive linked list (unsorted) search algorithm. Assume there is a public
helper function that calls the recursive function:
public Listable LLSearch(Listable key){
return LLSearch(key, head);
}
[10 pts]
private Listable LLSearch(Comparable key, Node current){
if( __________ == _______ ) {
return null; //not found
} else {
if(______.compareTo(_________.info) != ___ ){
return LLSearch(______ , ________.________);
} else { // found it
return ___________.________ ;
}
}
}
CS 240 Computer Science II Exam 3
Page 3
5. The following defines a function that calculates an approximation of the square root of a number (number),
starting with an approximate answer(approx) to within the specified tolerance (tol).
sqrRoot(number,approx,tol) = approx,
if abs(approx*approx-number)<=tol;
otherwise, = sqrRoot(number, approx*approx+number)/(2*approx),tol)
[10 pts]
Write a recursive Java function to implement the calculation of the square root.
public static double sqrRoot(double number, double approx, double tol){
}
6. Trees.
[10 pts]
a) Assume the root is at level 0. What is the level of node I? ____
b) Circle the largest subtree that is a binary tree.
c) How many leaves are there in the whole tree? _____
d) If each node is limited to two children, how many nodes total could be
stored in this (binary) tree without adding any more levels? ______
e) List the ancestors of node H ______________________
f) List the descendants of node C. _______________________
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)
A
/ \
B
C
/ \
\
D
E
F
/|\
/ \
G H I
I
K
CS 240 Computer Science II Exam 3
Page 4
7. Assume nodes in a binary tree are defined in an inner class having the following instance variables.
Comparable info;
BSTNode left;
BSTNode right;
a) Finish the recursive Java function to sum up the values of the nodes in a binary tree. The algorithm
works as follows. If the root (of tree or subtree) is null then return 0; otherwise return the sum of
the sume of the nodes on the left and the sum of the nodes on the right, plus the value of the root.
[7 pts]
public int Sum( _________ curNode)
{
if(curNode _____________ ) return ____ ;
return Sum ( _________________ )
+ __________ ( _________________ )
+ (int)curNode._____ ;
}
b) Finish the recursive Java function to return the “equal” item from the binary search tree.
[8 pts]
public __________ retrieve( BSTNode root, Comparable item)
{
if (_______ == null) return null;
int comp = item.compareTo( _________ . ___________)
if(comp == 0) return _________.______ ;
if(comp < 0) return retrieve ( ______ . ______ , item )
else
return retrieve (_______ . _______, item);
}
8. Create a binary search tree from the following character data entered into the BST left to right.
[4 pts]
SGANILED
a) List the nodes of your tree above from an inorder traversal.
[3 pts]
b) List the nodes of your tree above from a postorder traversal.
[3 pts]
CS 240 Computer Science II Exam 3
Page 5
9. Draw the following infix arithmetic expression as an expression tree. Represent precedence properly.
[4 pts]
a/b-c*(d+e)
a) Show a preorder traversal of your expression tree.
[3 pts]
10. True/False
[5 pts]
______ A doubly linked list permits random access to the nodes like an array.
______ Most operations on a binary tree depends on the depth of the tree which is linear to its capacity.
______ A binary search tree always generates the same inorder traversal regardless of the sequence the
nodes were entered into the tree.
______ A postorder traversal is necessary to effectively remove all nodes from a tree.
______ The maximum number of nodes in a binary tree of N levels is 2N+1-1