This lab will familiarize you with the Tree ADT. You'll be tasked with implementing some basic tree functionality that will solidify your understanding of how trees work by building a binary tree and supporting functions. Also note that you'll need to use recursion to accomplish some parts in the most efficient manner.
Be sure to read and follow the instructions carefully to ensure the highest possible grade!
For this lab, you'll need these files: lab10_files.
Once you've downloaded and unzipped the archive, you should have several files ready to go. In their current state, the program will compile and run without error! Don't worry...I've left plenty of work for you. But before you start hacking away at your keyboard, compile the program to reassure yourself that there are no problems with the code I've provided. Here's the command to compile the code. This will not change throughout the lab, and is what I will use when testing your submission:
g++ -o temp treelab.cpp
Once you've compiled the program, run it by passing input1.txt into stdin. You should see this:
Notice that the Max Value, Min Value and Max Depth are -1. You'll fix that shortly. Now that you've confirmed the program works, you can be assured that any errors (e.g. segmentation faults) from this point forward are the result of something you've added.
The way the main
function is written, the first integer value is used to create the root Node (see TreeNode.h). After that, each integer is inserted into the Tree using the insertInt()
function found at the top of the treelab.cpp file. Your task is to complete the insertInt
function by implementing the insert functionality needed to create a binary tree using the methods provided to you by the Tree class (defined in Tree.h), and following these guidelines:
For example, given the values (in this order) [50, 25, 75, 15, 20], the Tree would be represented like this:
However, changing the order of the values to [50, 15, 25, 75, 20] would result in the following Tree structure:
Notice how the values in the left subtree have changed based on the order elements were inserted into the Tree. The prettyPrint
function (provided as part of TreeNode.h) can be used to verify proper tree placement when testing your solution. See the sample output below for what the Tree structure should look like when using the provided file input1.txt.
Your next task is to implement the getMax()
and getMin()
functions at the top of treelab.cpp following the below specifications:
For example, both samples shown in Part 1 would have a Max value of 75, and a Min value of 15.
Your final task is to implement the maxDepth()
function found at the bottom of Tree.h. This function should determine the maximum depth of the Tree from a given node. Remember from lecture that we start counting depth from 0, so if maxDepth is called from the Tree's root node, and the Tree has no other nodes, it should return 0.
For example, both Trees shown in Part 1 have a maximum depth of 3.
If you've implemented all three parts correctly, your final output using input1.txt should look like this:
Your output using input2.txt should look like this:
Submit your modified source code via the the submisison website: submit.cs.usna.edu.
Specifically:
treelab.cpp
Tree.h
.o
, or provided/unaltered files (i.e. TreeNode.h
, input1.txt
, or input2.txt
)!
~/bin/submit -c=SI221 -p=Lab10 treelab.cpp Tree.h