Binary Search Tree Exercises - Assignment | CMSC 132, Assignments of Computer Science

Material Type: Assignment; Professor: Padua-Perez; Class: OBJECT-ORIENTED PROG II; Subject: Computer Science; University: University of Maryland; Term: Spring 2008;

Typology: Assignments

Pre 2010

Uploaded on 07/30/2009

koofers-user-cmx
koofers-user-cmx 🇺🇸

8 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Binary Search Tree Exercise
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<E>> {
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 the element to the proper location in the tree.
3. Define a recursive method int numLeaves( ) that returns the number of leaves in the tree.
4. Define a recursive method void print() that prints the tree in descending order.
5. Define a recursive method Set<E> leaves() that returns a set with the leaf node values.
6. Define a recursive method BinarySearchTree<E> copy() that returns a copy (shallow
copy) of the tree that has the same shape as the original tree.

Partial preview of the text

Download Binary Search Tree Exercises - Assignment | CMSC 132 and more Assignments Computer Science in PDF only on Docsity!

Binary Search Tree Exercise

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 the element to the proper location in the tree.
  3. Define a recursive method int numLeaves( ) that returns the number of leaves in the tree.
  4. Define a recursive method void print() that prints the tree in descending order.
  5. Define a recursive method Set leaves() that returns a set with the leaf node values.
  6. Define a recursive method BinarySearchTree copy() that returns a copy (shallow copy) of the tree that has the same shape as the original tree.