Quiz 3 Worksheet for CMSC 132 - Binary Search Tree, Quizzes of Computer Science

Information about quiz 3 for the cmsc 132 course, which covers binary search trees. The quiz will be a written, closed book, closed notes exam. Students are recommended to use pencil and eraser. The following exercises are provided to help students prepare for the quiz, but solutions will not be provided. Recursion and auxiliary methods are to be used in solving the problems. A java class definition for a binary search tree, and students are not allowed to add any variables to the class to answer the questions. The quiz questions include defining a constructor for an empty tree, implementing methods for adding a node, finding the number of non-leaf nodes, the height of the tree, and adding leaf nodes to an arraylist, as well as implementing a recursive method to check if a binary search tree is degenerate.

Typology: Quizzes

Pre 2010

Uploaded on 02/13/2009

koofers-user-3vk
koofers-user-3vk 🇺🇸

10 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC 132 Quiz 3 Worksheet
The third quiz for the course will be on Friday, July 18, 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 tree size() method may use an auxiliary method that takes as parameter a reference to a Node.
Keep this in mind when solving the problems below.
The following Java class definition for a binary search tree will be used to answer the questions that
follow. Unlike project #3 the following tree is not polymorphic. Null is used to indicate an empty tree.
For example, an empty BinarySearchTree has a null root, and a leaf node has null left and right fields.
For this problem you may not add any variables (instance or static) to the class in order to answer the
questions below.
public class BinarySearchTree <E extends Comparable<E>> {
private class Node {
private E data;
private Node left, right;
}
private 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 recursive method int numInteriorNodes( ) that returns the number of non-leaf
nodes in the tree.
4. Define a recursive method int height( ) that returns the height of the tree.
5. Define a recursive method void leaves(ArrayList<E> L) that adds to the ArrayList L the
data component of leaf nodes.
6. Implement a recursive method called isDegenerate that returns true if a binary search tree
is a degenerate tree and false otherwise. A degenerate tree is one where every node has only
one child.

Partial preview of the text

Download Quiz 3 Worksheet for CMSC 132 - Binary Search Tree and more Quizzes Computer Science in PDF only on Docsity!

CMSC 132 Quiz 3 Worksheet

The third quiz for the course will be on Friday, July 18, 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 tree size() method may use an auxiliary method that takes as parameter a reference to a Node. Keep this in mind when solving the problems below.

The following Java class definition for a binary search tree will be used to answer the questions that follow. Unlike project #3 the following tree is not polymorphic. Null is used to indicate an empty tree. For example, an empty BinarySearchTree has a null root, and a leaf node has null left and right fields. For this problem you may not add any variables (instance or static) to the class in order to answer the questions below.

public class BinarySearchTree <E extends Comparable> { private class Node { private E data; private Node left, right; } private 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 recursive method int numInteriorNodes( ) that returns the number of non-leaf nodes in the tree.
  4. Define a recursive method int height( ) that returns the height of the tree.
  5. Define a recursive method void leaves(ArrayList L) that adds to the ArrayList L the data component of leaf nodes.
  6. Implement a recursive method called isDegenerate that returns true if a binary search tree is a degenerate tree and false otherwise. A degenerate tree is one where every node has only one child.