Homework 4: Traveller of Trees
DRAFT: Open for comment in the discussion forum.
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.
Deliverables:
Two files:
tbt5.cpp // contains implementations of preorder, postorder, and levelorder iterators
log.txt // development & testing log file
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 core of TBinaryTree, TBinaryTreeNavigator, and non-member operators
tbt2.cpp // implements function bool Load(TBinaryTree<T>&, const char*)
tbt3.cpp // implements 1-argument 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:
- 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.
Be sure to log your activities in log.txt.
- Test your implementations thoroughly, logging the results in log.txt.
-
Turn in the files tbt5.cpp and log.txt using the hw4submit.sh submit script.
Warning: Submit scripts do not work on the program and
linprog servers. Use shell.cs.fsu.edu to submit assignments. If you do
not receive the second confirmation with the contents of your assignment, there has
been a malfunction.
Technical Requirements and Specifications
- Implement the three iterator classes as specified in tbt.h,
starting with LIB/hw4/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.
Hints
- Techniques and many details are discussed in the lecture notes.
- The files tcpp/tbt.h, tcpp/tbt1.cpp, tcpp/tbt2.cpp, tcpp/tbt3.cpp, and
tcpp/tbt4.cpp are distributed.
Particularly tcpp/tbt4.cpp is useful, as
it implements the TBinaryTreeInorderIterator<T> iterator
class. tcpp/tbt6.cpp is just a stub, but you don't need rotations (yet!).
- Most of the "busy" work implementing the iterators has been done and is
supplied in the file LIB/hw4/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/hw4/itertest1.cpp and LIB/hw4/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/hw4 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).