CS 311 Fall 2024 > Assignment 7
CS 311 Fall 2024
Assignment 7
Assignment 7 is due at 5 pm Tuesday, November 19. It is worth 65 points.
Procedures
This is, optionally, a group assignment. You may work in a group of two, if you wish. Each group only needs to turn in a single copy of the assignment. Under normal circumstances, each group member will receive the same grade on the assignment.
Turn in answers to the exercises below on the UA Canvas site, under Assignment 7 for this class.
- Your answers must consist of
the source code for Exercise A
(file
treesort.hpp
). Attach this file to your homework submission. - I may not look at your homework submission immediately. If you have questions, e-mail me.
If you work in a group:
- The assignment files must contain the names of both group members.
- One student submits the assignment as usual, on Canvas.
- The other student submits an empty assignment (no attached source or header files) on Canvas, with a note indicating whom the assignment was done with.
Exercises (65 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, and use of iterators.
Instructions
This assignment may be done individually or in a group of 2.
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!
- Call your function template
treesort
, and prototype it as follows.template <typename FDIter> void treesort(FDIter first, FDIter last);
- Implement your function template in file
treesort.hpp
.- Since this is a template, there should be no associated source file.
- Your header file may define any other functions and classes you want.
- What the function should do.
- Function
treesort
takes two forward iterators (see Notes on Iterators, below). These specify a range to be sorted, in the usual manner:first
points to the first item in the list, andlast
points to just past the last item. Iffirst
andlast
are equal, then the range is empty. - Function
treesort
sorts the given range, in a stable manner, usingoperator<
. The sorted data are stored in the same space as the original data. - The function must use the Treesort algorithm, as described in class.
- Function
- The Binary Search Tree.
- Your file must include at least a partial implementation of a Binary Search Tree, including the insert and inorder-traversal operations.
- Other than the coding standards, there are no restrictions on how you implement the Binary Search Tree. You are not required to create a separate tree class. Your nodes may be separately allocated and referenced by pointers or smart pointers, or you may store your nodes in an array and reference them with array indices. (Note that the complete Binary Tree array implementation is not appropriate, since the tree may not be complete.)
- Function
treesort
should have the highest reasonable levels of efficiency and exception safety. - Function
treesort
must be exception-neutral; that is, exceptions thrown by value-type operations must propagate unchanged to the caller. - You may not forbid any member function of the value type from throwing, except for the destructor, move constructor, and move assignment operator.
- You may make use of any C++ Standard Library functionality you wish.
Slides & Skeleton File
Thoughts on writing function treesort
can be found in the lecture slides for
Monday, November 11.
I have provided an incomplete “skeleton” version of
treesort.hpp
;
this is in the Git repository.
You may use this as the basis for your own work, if you wish.
This is not required.
Test Program & Grading
A test program
is available:
treesort_test.cpp
.
If you compile and run the test program (unmodified!) with your code,
then it will test
whether your code works properly.
The test program requires doctest.h
,
the header for the doctest unit-testing framework,
version 2.
Do not turn in the test program or the doctest framework.
Note: The test program does not check whether you actually use a Binary Search Tree. However, code that does not at least appear to 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:
- Default constructor.
- Copy constructor.
- Copy assignment.
- Destructor.
- Dereference (
operator*
). - Arrow (
operator->
) - Equality test (
operator==
). - Inequality test (
operator!=
). - Increment (
operator++
, both pre and post).
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 a number 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 an iterator of type FDIter
points to.
You can use code like the following
(std::iterator_traits
is declared
in header <iterator>
).
[C++]
using Value = typename iterator_traits<FDIter>::value_type;
You can then use Value
as you would any other type.
In particular,
you could do something like the following
[C++]
auto p = make_shared<BSTreeNode<Value>>(item, nullptr, nullptr);
... assuming, of course, that you have an appropriate class template
named BSTreeNode
,
which has a 3-parameter constructor.
See treesort.hpp
, in the Git repository,
for example code that determines and uses the value type of an iterator,