Project 6: Recursion and lists

This project has two parts: The first part gives you practice writing recursive methods. The second part is to practice writing methods for lists. These two parts are unrelated; I've put them together into one project for scheduling purposes.

Make a directory for this assignment, and change into it.

cd cs235
mkdir proj6
cd proj6

Copy the following file from the public directory for this class:

cp /homes/tvandrun/Public/cs235/proj6/* .

Part I

1. Setup

Open the file RecursiveString.java.

This file contains the stubs for three methods for you to write, as well as a simple main method to test these methods out using information from the args array. In each section below, you will be instructed to fill-in the body for one of these methods; you also should uncomment the code in the main method which tests it.

2. Computing palindromes recursively

We've already seen at least one algorithm for determining whether or not a String is a palindrome. Now you need to figure out a recursive method to do it. Figure the algorithm on your own, but here's a recursive definition of a palindrome to get you started:

A palindrome is

(Remember, "prepend" means "concatenate to the front" and "append" means "concatenate to the back.")

Write the isPalindrome() method and test. You need concern yourself only with single words, not phrases (so you can assume that the string has only letters, not spaces, punctuation, etc). Notice that the main method tests your methods on strings from the command line using the args array. Therefore, to test this, you should give a string to test on the command line. For example

java RecursiveString racecar

3. Reversing strings

Write a method which will reverse a String. First write out a recursive definition of what the reverse of String is; then transform that definition into a recursive algorithm for computing the reverse of a String.

4. Computing prefixes

Write a method startsWith() which takes two strings and determines whether or not one is a prefix of the other.

Part II

The goal of this lab is to implement basic sorting algorithms.

1. Introduction

We saw the general problem of sorting back in Project 3. Some sorting algorithms are recursive. The most intuitive recursive sort if mergesort. It works this way

Suppose you have an unsorted pile of cards in your hand. First you split that pile up into two smaller piles. Then you sort each pile. Finally, you merge the two smaller (now sorted) piles into a full, sorted pile. This raises the question, how do we sort the smaller piles? The answers is-- the same way we sort the big one: we split those up each into two even smaller piles, sort those smaller piles, and merge them into a sorted small pile. We repeat the process (recursively) until we get to a pile of size 1, which is trivially sorted.

Like other sorting algorithms, mergesort is usually described in terms of how it is implemented on an array. We, however, will be implementing it on a linked list.

2. Setup

You have the following files given to you in this part of the project:

3. Your task

As in the lab (on 11/4), the driver and a big part of the list class is already provided for you. Also, the basic merge sort algorithm is given for you in the method sort() in class ListMerge.

Your task is to write the following methods that sort() needs:

It is possible that you will write merge in such a way that you wish List to have some extra operations; feel free to add more operations to List as you see fit.

4. Turn in

Do the normal turn-in procedure: Script file, cat the files you changed, compile, run a few times.

a2ps (the name of the script file)

Then turn in the hard copy by 5:00, Thurs, Nov 13.


Thomas VanDrunen
Last modified: Tue Nov 11 10:14:24 CST 2008