Sample Exam 2 KEY

advertisement
CS 3540 Practice Test (Midterm 2)
The following 3 questions are based on the Binary Tree Program.
1. Write a Java method to find (and print) the second smallest key in a binary search
tree. Print “no such key” if the tree only contains 1 element.
public void second(Node root)
{
current = root;
prev = root;
if (current.left == null) && (current.right == null)
print("No such key);
else {
while (current != null) { // RUN LEFT
prev=current;
current = current.left;
} // while
if (current.right == null)
// IF NO RIGHT SUBTREE, PRINT PARENT
print(prev.key);
else {
current = current.right; // TURN RIGHT
while (current.left !=null) // RUN LEFT
current = current.left;
} // else
print(current.key);
} // else
}
2. Write a Java method to compute the height of a binary tree. The method should
take a single parameter, which is the root of the tree (Hint: compute the height of
each vertex, then find the maximum height).
private int treeheight(Node localRoot)
{
int left_height =0; int right_height = 0;
int height = 0;
if(localRoot != null)
{
left_height = treeheight(localRoot.leftChild);
right_height = treeheight(localRoot.rightChild);
height = math.max(left_height, right_height) + 1;
}
return height;
}
3. Write a Java method to compute the “weight” of a node W, the number of nodes
in the subtree rooted at W (W will be a parameter to the method).
private void getweight(Node localRoot)
{
if(localRoot != null)
{
inOrder(localRoot.leftChild);
weight++;
// weight is variable in
// Tree class, set to 0 before
// call to getweight
inOrder(localRoot.rightChild);
}
}
The following 3 questions are based on the Linked List Program.
4. Write a Java Method to delete the next-to-last link in a linked list in two ways:
a) Using calls to the delete method, once the desired link is found
b) Without using calls to the delete method
a)
prev = first;
current = first;
while (current.next !=null) {
prev = current;
current = current.left;
}
if (current != first) theList.delete(prev.iData);
b)
pprev = first;
prev = first;
current = first;
while (current.next !=null) {
pprev = prev;
prev = current;
current = current.left;
}
if (current != first) {
if (prev = first)
first = current;
else
pprev.next = current;
}
5. Write a Java Method to “fold” the linked list: take a link as a parameter. Cut the
list just before this link, making this link the new front of the list, and attach the
(old) front part of the list at the end of the list. See picture:
public void fold (Link foo)
{
Link current = foo;
Link last = first;
if (foo == first) return;
// nothing to do
while (current.next != null)
current=current.next;
// when finished, current = last link in list
while (last.next != foo)
last=last.next;
// find node just before foo
current.next = first;
first = foo;
last.next = null;
// cut-and-paste
} // fold
6. Reverse a linked list:
linkList two = new linkList();
current=theList.first;
// would need to make first public
// no other easy way to do it in “main”
while (current !=null) {
two.insertFirst(current.iData, current.dData);
current=current.next;
}
Disclaimer: Answer to #6 is kind of weak. But it was written in a way to ensure the
old linked list IS NOT destroyed in the process of creating the new one. An alternate
solution, which would destroy the old linked list, is to traverse the old linked list from
front to last pushing each node’s reference onto a stack as you go. Then pop the nodes
off the stack making the nth node popped off point to the n+1st.
Download