CS 311 Spring 2008  >  Assignment 7

CS 311 Spring 2008
Assignment 7

Assignment 7 is due at 5 p.m. Thursday, April 24. It is worth 20 points.

Procedures

E-mail answers to the exercises below to ffggc@uaf.edu, using the subjectDA7”.

Exercises (20 pts total)

Exercise A — Treesort

Purpose

In this exercise, you will implement the Treesort algorithm. This will require at least a partial implementation of a Binary Search Tree, including insertion and inorder traversal.

Instructions

Write a C++ function template that sorts a given range using the Treesort algorithm. Be sure to follow the coding standards. All standards now apply!

Test Program & Grading

I have written a test program: treesort_test.cpp. If you compile and run your package with this program (unmodified!), then it will test whether your code works properly.

Do not turn in treesort_test.cpp.

Note: The test program does not check whether you actually use a Binary Search Tree. However, code that does not at least make an attempt at using a Binary Search Tree will not be graded.

Notes on Iterators

Forward Iterators

A forward iterator is guaranteed to allow the following operations:

In particular, you can do this:

for (FDIter it = first; it != last; ++it)
{
    doSomething(*it);
}

You may dereference a forward iterator any number of times without changing the iterator. You may not decrement a forward iterator. You also may not add to a forward iterator or subtract two forward iterators; however, std::advance and std::distance may be used to perform these operations.

Finding the Value Type

It is likely that you will need to figure out what the value type is, that is, what type the iterator points to. To do this, use std::iterator_traits, as shown below.

typedef typename std::iterator_traits<FDIter>::value_type ValueType;

After this, ValueType is the item of the item referenced by a FDIter. Note: The “typename” above is a signal to the compiler that what follows is a type; many compilers require this keyword in the above situation.

In order to use std::iterator_traits, you will need to include the proper header:

#include <iterator>

You can find example code using std::iterator_traits on the web page: merge_sort.cpp. (See the beginning of function stableMerge.)

Note: In some versions of Visual C++, std::iterator_traits does not work properly when the iterator is a pointer. Therefore, the test program will only test your function using iterators to Standard Library containers (vector, deque, list), and possibly other types of iterators that are guaranteed not to be pointers.


CS 311 Spring 2008: Assignment 7 / Updated: 21 Apr 2008 / Glenn G. Chappell / ffggc@uaf.edu Valid HTML 4.01!