Docsity

Quiz 3 Worksheet for CMSC 132: Recursive Methods in Linked Lists and Binary Trees - Prof. , Quizzes of Computer Science

Toggle sidebar
bg1
CMSC 132 Quiz 3 Worksheet
The third quiz for the course will be on Wednesday, Oct 10, during your lab session. The following list
provides more information about the quiz:
• The quiz will be a written quiz (no computer).
• Closed book, closed notes quiz.
• Answers must be neat and legible. We recommend that you use pencil and eraser.
The following exercises cover the material to be included in this quiz. Solutions to these exercises will
not be provided, but you are welcome to discuss your solutions with the TA or instructor during office
hours. We strongly recommend you do not use Eclipse to write the code associated with
these exercises. Try to answer the exercises in a piece of paper and then use Eclipse to verify your
solutions. This approach will better prepare you for the quiz.
Some recursion problems require an auxiliary method. For example, a recursive implementation for
the size() method of a linked list calls for an auxiliary method that takes as parameter a reference to a
node. Keep this in mind when solving the problems below.
A. Linked Lists
The following Java class definition for a linked list will be used to answer the questions that
follow. You may not add any variables (instance or static) to the class in order to implement the
questions below.
public class LinkedList<E> {
public class Node {
private E data;
private Node next;
private Node (E data) {
this.data = data;
}
}
private Node head;
}
1. Define a recursive method int size() that computes the size of a list.
2. Define a recursive method void printReverse() that prints the list in reverse order.
3. Define a recursive method boolean find(E elem) that returns true if elem is in the list
and false otherwise.
4. Define a recursive method boolean equals(LinkedList<E> list) that determines
whether two lists have the same corresponding elements. For example, the lists 10, 20,
30 and 10, 20, 30 are considered equals, whereas 10, 20, 30 and 30, 20, 10 are different.
pf2

Partial preview of the text

Download Quiz 3 Worksheet for CMSC 132: Recursive Methods in Linked Lists and Binary Trees - Prof. and more Quizzes Computer Science in PDF only on Docsity!

CMSC 132 Quiz 3 Worksheet

The third quiz for the course will be on Wednesday, Oct 10, during your lab session. The following list provides more information about the quiz:

  • The quiz will be a written quiz (no computer).
  • Closed book, closed notes quiz.
  • Answers must be neat and legible. We recommend that you use pencil and eraser.

The following exercises cover the material to be included in this quiz. Solutions to these exercises will not be provided, but you are welcome to discuss your solutions with the TA or instructor during office hours. We strongly recommend you do not use Eclipse to write the code associated with these exercises. Try to answer the exercises in a piece of paper and then use Eclipse to verify your solutions. This approach will better prepare you for the quiz.

Some recursion problems require an auxiliary method. For example, a recursive implementation for the size() method of a linked list calls for an auxiliary method that takes as parameter a reference to a node. Keep this in mind when solving the problems below.

A. Linked Lists

The following Java class definition for a linked list will be used to answer the questions that

follow. You may not add any variables (instance or static) to the class in order to implement the

questions below.

public class LinkedList { public class Node { private E data; private Node next; private Node (E data) { this.data = data; } }

private Node head; }

  1. Define a recursive method int size() that computes the size of a list.
  2. Define a recursive method void printReverse() that prints the list in reverse order.
  3. Define a recursive method boolean find(E elem) that returns true if elem is in the list and false otherwise.
  4. Define a recursive method boolean equals(LinkedList list) that determines whether two lists have the same corresponding elements. For example, the lists 10, 20, 30 and 10, 20, 30 are considered equals, whereas 10, 20, 30 and 30, 20, 10 are different.

B. Binary Trees

The following Java class definition for a binary tree will be used to answer the questions that follow. Unlike project 3, the following tree is not polymorphic. .You may not add any variables (instance or static) to the class in order to implement the questions below.

public class BinarySearchTree <E extends Comparable> { private class Node { E data; Node left, right; } Node root; }

  1. Define a constructor that creates an empty tree.
  2. Define a method void add(E val) that adds a node to the proper location in the tree.
  3. Define a method void remove(E val) that removes a node from the tree.
  4. Define a recursive method int numLeaves( ) that returns the number of leaves in the tree.
  5. Define a recursive method int height( ) that returns the height of the tree.
  6. Define a recursive method boolean isFull( ) that returns true if every interior node in the tree has both a left and right subtree.