Project 3: Traveller of Trees
Educational Objectives:
On successful completion of this assignment, the student should be able to
- Define iterator order for preorder, inorder, postorder, and levelorder
iterators for binary trees
- Understand and be able to explain which of these iterators is based on
depth-first search and which is based on breadth-first search
- Describe in pseudo code or other precise language the initialize and increment
operations for the depth-first iterator types using a stack controller
- Describe in pseudo code or other precise language the initialize and increment
operations for the breadth-first iterator types using a queue controller
- Describe in pseudo code or other precise language the initialize, increment,
and decrement operations for the depth-first iterator types using iterative control
- Implement the member functions Initialize() and operator++()
for each of the four iterator types using stack or queue control
- Implement the member functions Initialize(), operator++(),
and operator--() for the depth-first iterators using iterative control
- Define the concept of traversal of a container in terms of iterators
- State the asymptotic runtime and runspace costs of tree traversals
for each iterator type and for each design for that type
- Give an argument that your asymptotic cost estimates are correct
- Explain how the asymptotic cost estimates for traversals and asymptotic cost estimates for individual
iterator operations are related
Operational Objectives:
implement three binary tree iterator types:
BinaryTreePostorderIterator<T>,
BinaryTreePreorderIterator<T>, and
BinaryTreeLevelorderIterator<T>
as declared in the file tbt.h. Also implement the method
TBinaryTree<T>::Dump(std::ostream& os).
Deliverables:
Two files:
tbt3.cpp // contains implementation of Dump(std::ostream&)
tbt5.cpp // contains implementations of preorder, postorder, and levelorder iterators
Background
Required background knowledge is reviewed in
Trees 1 ,
Trees 2 ,
Trees 3
The binary tree class and its associated navigator class, iterator classes, and
functions are all defined/prototyped in the header file tbt.h.
Slave files are used to house implementations of various components of
the TBinaryTree. The files in the package are as follows:
tbt.h // class definitions and function/operator prototypes
tbt1.cpp // implements TBinaryTree, TBinaryTreeNavigator, and all stand-alone operators
tbt2.cpp // implements function int Load(TBinaryTree<T>&, const char*)
tbt3.cpp // implements method void TBinaryTree<T>::Dump(std::ostream&)
tbt4.cpp // implements class InorderIterator
tbt5.cpp // implements classes PreorderIterator, PostorderIterator, LevelorderIterator,
tbt6.cpp // implements TBinaryTree<T> rotation methods
(Recall that a "slave" file is one that is included into its master file with
a #include<> directive. Slave files should not define a namespace
or #include their associated header file, otherwise an infinite loop of
#includes is created. The effect on client programs is that
only the master header file is #included into source files. A slave file makes
no sense to the compiler by itself. A slave file only makes sense when considerd
a logical part of its master file.)
Procedural Requirements:
- Implement the 1-parameter dump method, as prototyped in LIB/tcpp/tbt.h. Place this implementation in
file tbt3.cpp.
- Complete the implementations of the three alternative iterator types (preorder,
postorder, and levelorder) for binary trees, as defined in
LIB/tcpp/tbt.h. Place these implementations in file tbt5.cpp.
- Test your implementations thoroughly.
-
Turn in two files tbt3.cpp tbt5.cpp using the proj3submit.sh submit script.
Warning: Submit scripts do not work on the program and
linprog servers. Use shell.cs.fsu.edu to submit projects. If you do
not receive the second confirmation with the contents of your project, there has
been a malfunction.
Technical Requirements and Specifications
- Implement the three iterator classes as specified in tbt.h,
starting with LIB/proj3/tbt5.partial.
- Use iteration-based depth-first search to implement PostorderIterator.
- Use stack-based depth-first search to implement PreorderIterator.
- Use queue-based breadth-first search to implement LevelorderIterator.
- Note that PreorderIterator and LevelorderIterator are somewhat curtailed forward
iterators, while PostorderIterator is a fully functional bidirectional iterator.
- Implement the 1-parameter Dump() method as prototyped in
tbt.h and demonstrated in the distributed executables, beginning from
the file LIB/proj3/tbt3.partial.
Hints
- Techniques and many details are discussed in the lecture notes.
- The files tcpp/tbt.h, tcpp/tbt1.cpp, tcpp/tbt2.cpp, and
tcpp/tbt4.cpp are distributed.
Particularly tcpp/tbt4.cpp is useful, as
it implements the TBinaryTreeInorderIterator<T> iterator
class.
- The 2- and 3-parameter Dump() methods are implemented in tbt1.cpp.
- The method header for Dump(std::ostream&) is already in the
file LIB/proj3/tbt3.partial. You only have to provide the
implementation code.
- Most of the "busy" work implementing the iterators has been done and is
supplied in the file LIB/proj3/tbt5.partial. You only have to implement
the main algorithms for Initialize(), operator++(), and
operator--() (the last for postorder only).
- Your submission will be assessed using
LIB/proj3/itertest1.cpp and LIB/proj3/itertest2.cpp, the
latter checking for "reciprocity" of postorder iterators. Reciprocity means that
i == --(++i) whenever ++i is valid and
i == ++(--i) whenever --i is valid.
- Several sample tree data files for use by itertest1 are in LIB/proj3 The format for these files
is discussed in tbt.h and below.
-
Function Load() is a stand-alone function to
facilitate loading of a tree structure from a file that can be prepared using a
text editor. The function reads the contents of the file in level order,
releasing any memory associated with the tree prior to read, if neccessary. The
function returns 1 = true iff the entire file is loaded into the tree.
-
The file structure assumed by Load() satisfies: (1) each token (i.e.,
string representing an element of type T) is followed by a separator
character;
(2) the separator characters are tab ('\t'), newline ('\n'),
and end-of-file; (3) "missing" tree nodes are indicated by a separator character
with no preceeding token.
-
Sample executables of the test harnesses are posted as
area51/itertest?_s.x (for Sun/Unix) and area51/itertest?_i.x
(for Intel/Linux).