Please read the remarks about programming problems for this course.
A binary tree may be traversed in one of three standard ways:
Consider a binary tree (not necessarily a binary search tree) that contains unique elements (that is, it has no duplicate elements). It is not possible to deduce the tree's structure solely from the order of the list of elements visited during a single traversal. It is possible, however, to construct a binary tree containing unique elements from a preorder and an inorder traversal.
Given a binary tree node defined as
complete the following function that constructs a binary tree given iterators to both preorder and inorder traversals:
Note that our binary trees here are generic binary trees and, thus, are not necessarily binary search trees. The tree will contain no duplicate elements.
To check that your function is correct, supply the missing code in the following function that draws a binary tree sideways:
Additionally, implement the following dispose
function that
frees up the memory held by a binary tree:
To streamline your development and help you better understand your task in this assignment, I have provided the following three files:
build_tree
and draw
functions) that make it easier for
callers to use your build_tree
and
draw
functions.
This tree.hpp header file #include
s your
tree_extra.hpp file. Your code makes this header file
complete.
You should not modify this file. My test code will use this file as is and will use your tree_extra.hpp file as is, so if you modify tree.hpp to somehow "help" your tree_extra.hpp file, your code likely will not pass my tests and possibly will not compile.
The first thing you should do is download all the files, build, and run the simpletest.cpp program. It should compile only with warnings (the function stubs are not using the parameters passed to them, so the compiler issues warnings for unused parameters). When executed, the program should print the following:
After you have correctly implemented the functions, the program should print the following:
(Rotate your head 90 degrees counter-clockwise to see the trees as we normally would orient them.) The output of the first tree corresponds to the following graphical rendering of the tree:
Hint: Notice that the draw
function performs a backwards
inorder traversal (right subtree, node, left subtree), and the
depth of the recursion controls the amount of horizontal space to the left
of the printed node.
Since it can be challenging to write both the
draw
and the
build_tree
functions at the same time, I suggest
writing the
draw
function first. You can use the following code to
test your draw
function:
If your draw
function is correct, the above code
should display the following:
Once you get draw
working you can use it to verify
that your build_tree
function is correct. (You also
can check your build_tree
function by doing preorder and
postorder traversals on the tree it returns and comparing the
result to the original
traversals.)
Before you submit your code, check once more that your tree_extra.hpp file builds correctly with the original tree.hpp and simpletest.cpp files. Submit your completed tree_extra.hpp source file to eclass.e.southern.edu.