Commit 911e26a9 authored by laddbr's avatar laddbr
Browse files

Homework 9 upload

parent 1f5d3341
# Assignment Submission Form
## Walla Walla University -- Department of Computer Science
_Name:_ ?
_Course:_ CPTR242
_Assignment:_ ?
_Date:_ ?
_Estimated:_ ?
_Actual Time:_ ?
---
I hereby certify that the code included in this assignment is ENTIRELY my own original work, with the following exceptions:
* Exception one
* Exception two
---
_Digital Signature:_ ???
(Type name or include signature image)
SRCS = ancestors.cpp queue_type.cpp tree_type.cpp menu.cpp submitty_driver.cpp item_type.cpp
EXE = a.out
SUBMITTY_CXXFLAGS = -I.
ifneq ("$(wildcard main.cpp)","")
# Code.CS settings
SRCS = ancestors.cpp queue_type.cpp tree_type.cpp menu.cpp main.cpp item_type.cpp
EXE = tree.out
else ifneq ("$(wildcard ancestors.cpp)","")
# Submitty setting -- no changes
else
# Homework library settings
SRCS =../ancestors.cpp queue_type.cpp tree_type.cpp menu.cpp submitty_driver.cpp item_type.cpp
SUBMITTY_CXXFLAGS = -Iprovided_code -I. -I..
endif
# Everything below that should not have to change ever.
CXX=g++
LD=g++
CC=g++
TARGET = all
OBJS = $(SRCS:.cpp=.o)
CXXFLAGS = -g -std=c++17 $(SUBMITTY_CXXFLAGS)
LDFLAGS = -g
LIBS =
$(EXE): $(OBJS)
$(CXX) -o $(EXE) $(OBJS) $(LDFLAGS) $(LIBS)
.SUFFIXES: .cpp
.cpp.o:
$(CXX) $(CXXFLAGS) -c $< -o $@
all : $(EXE)
clean:
-rm -f $(OBJS)
-rm -f $(EXE)
-rm -f log.txt
# CPTR 242: Homework -- Binary Search Trees
## Problem Overview
Your task in this assignment is to implement __ancestors__ for a Binary Search Tree.
The function __ancestors__ prints the ancestors for a given node whose _info_ member contains _value_.
Do not print value.
The __ancestors__ function must be implemented in three ways: iterative, recursive and reverse.
## Solution Specifications
Your solution to this problem must meet the following criteria.
1. Implement the three versions of _ancestors_ for a linked implementation in `ancestors.cpp`.
A stub of the function has been given to you.
```cpp
void TreeType::ancestorsIterative(ItemType value, std::ostream &outStream) {
// TODO implement ancestors iterative print
}
void printAncestorsRecursive(TreeNode *tree, ItemType value,
std::ostream &outStream) {
// TODO implement ancestors recursive print
}
void printAncestorsReverse(TreeNode *tree, ItemType value,
std::ostream &outStream) {
// TODO implement ancestors reverse print
}
```
1. Pass all the tests in Submitty.
## Test Driver
The book describes the operations of binary search trees.
The submitty tests are based on previous test drivers.
It may be helpful to create your own test driver to validate your functions.
## Compiling List Implementations
A simple way to compile them is to list the cpp files after the g++ compiler.
You can run the test by executing the following:
```
g++ -std=c++17 *.cpp & ./a.out
```
Remember you only need to list the cpp files and must be in the local directory.
Since the header files are in the same folder they are included in the _#include_ statement.
## Test Driver
The Submitty tests use a test driver similar to the other homeworks.
In this assignment you may find it helpful to create your own test input for the driver to validate your functions.
A `main.cpp` has been provided to allow a user to run test input.
Please model the method we have used for homework 1 in creating your own test input for the driver.
These are the complete menu options available:
```txt
Select options
- Menu Display menu.
- AncestorsIterative <item> Uses an iterative function to travel the tree and print the ancestors of <item>.
- AncestorsRecursive <item> Uses a recursive function to travel the tree and print the ancestors of <item>.
- AncestorsReverse <item> Recursively travels the tree, printing the ancestors of <item>.
- PutItem <item> Puts an item in the tree.
- DeleteItem <item> Deletes an item from the tree.
- GetItem <item> Searches the list for <item>.
- GetLength Returns the amount of nodes in the tree.
- IsEmpty Returns whether the tree is empty or not.
- IsFull Returns whether the tree is full or not.
- PrintTree Prints the tree.
- ResetTree <PRE_ORDER | IN_ORDER | POST_ORDER> Resets the tree to the desired order.
- GetNextItem <PRE_ORDER | IN_ORDER | POST_ORDER> Gets the next item based on the given order traversal.
- MakeEmpty Clears the tree.
- Quit Quit application.
```
The code can be compiled by running the `make` command.
Example Input:
```sh
IsEmpty
PutItem P
PutItem F
PutItem S
PrintTree
PutItem B
PutItem H
PutItem G
PutItem R
PutItem Y
PutItem T
PutItem Z
PutItem W
IsFull
IsEmpty
GetItem P
GetItem G
GetItem T
GetItem A
GetItem N
PrintTree
ResetTree IN_ORDER
GetNextItem IN_ORDER
GetNextItem IN_ORDER
GetNextItem IN_ORDER
GetNextItem IN_ORDER
GetNextItem IN_ORDER
GetNextItem IN_ORDER
GetNextItem IN_ORDER
GetNextItem IN_ORDER
GetNextItem IN_ORDER
GetNextItem IN_ORDER
GetNextItem IN_ORDER
PrintTree
ResetTree POST_ORDER
GetNextItem POST_ORDER
GetNextItem POST_ORDER
GetNextItem POST_ORDER
GetNextItem POST_ORDER
GetNextItem POST_ORDER
GetNextItem POST_ORDER
GetNextItem POST_ORDER
GetNextItem POST_ORDER
GetNextItem POST_ORDER
GetNextItem POST_ORDER
GetNextItem POST_ORDER
PrintTree
ResetTree PRE_ORDER
GetNextItem PRE_ORDER
GetNextItem PRE_ORDER
GetNextItem PRE_ORDER
GetNextItem PRE_ORDER
GetNextItem PRE_ORDER
GetNextItem PRE_ORDER
GetNextItem PRE_ORDER
GetNextItem PRE_ORDER
GetNextItem PRE_ORDER
GetNextItem PRE_ORDER
GetNextItem PRE_ORDER
PrintTree
GetLength
AncestorsIterative H
AncestorsIterative P
AncestorsRecursive B
AncestorsRecursive F
AncestorsReverse Z
AncestorsReverse W
IsEmpty
DeleteItem W
DeleteItem Z
DeleteItem S
DeleteItem P
DeleteItem F
DeleteItem B
DeleteItem H
DeleteItem G
DeleteItem R
DeleteItem T
DeleteItem Y
IsEmpty
PutItem A
PutItem B
PrintTree
MakeEmpty
IsEmpty
Error
Quit
```
To test the code, run this command:
```sh
./tree.out
```
Resulting Standard Output:
```sh
Example Test
Tree is empty.
P is inserted
F is inserted
S is inserted
FPS
B is inserted
H is inserted
G is inserted
R is inserted
Y is inserted
T is inserted
Z is inserted
W is inserted
Tree is not full.
Tree is not empty.
P found in list.
G found in list.
T found in list.
A not in list.
N not in list.
BFGHPRSTWYZ
Next item is: B
Next item is: F
Next item is: G
Next item is: H
Next item is: P
Next item is: R
Next item is: S
Next item is: T
Next item is: W
Next item is: Y
Next item is: Z
IN_ORDER traversal is complete.
BFGHPRSTWYZ
Next item is: B
Next item is: G
Next item is: H
Next item is: F
Next item is: R
Next item is: W
Next item is: T
Next item is: Z
Next item is: Y
Next item is: S
Next item is: P
POST_ORDER traversal is complete.
BFGHPRSTWYZ
Next item is: P
Next item is: F
Next item is: B
Next item is: H
Next item is: G
Next item is: S
Next item is: R
Next item is: Y
Next item is: T
Next item is: W
Next item is: Z
PRE_ORDER traversal is complete.
BFGHPRSTWYZ
Number of nodes is 11
PF are the ancestors of H
are the ancestors of P
PSYZ are the ancestors of B
PSYZ are the ancestors of F
BFP are the ancestors of Z in reverse
BFP are the ancestors of W in reverse
Tree is not empty.
W is deleted
Z is deleted
S is deleted
P is deleted
F is deleted
B is deleted
H is deleted
G is deleted
R is deleted
T is deleted
Y is deleted
Tree is empty.
A is inserted
B is inserted
AB
Tree has been made empty.
Tree is empty.
```
Resulting `log.txt`
```sh
IsEmpty command number 1 is completed.
PutItem command number 2 is completed.
PutItem command number 3 is completed.
PutItem command number 4 is completed.
PrintTree command number 5 is completed.
PutItem command number 6 is completed.
PutItem command number 7 is completed.
PutItem command number 8 is completed.
PutItem command number 9 is completed.
PutItem command number 10 is completed.
PutItem command number 11 is completed.
PutItem command number 12 is completed.
PutItem command number 13 is completed.
IsFull command number 14 is completed.
IsEmpty command number 15 is completed.
GetItem command number 16 is completed.
GetItem command number 17 is completed.
GetItem command number 18 is completed.
GetItem command number 19 is completed.
GetItem command number 20 is completed.
PrintTree command number 21 is completed.
ResetTree command number 22 is completed.
GetNextItem command number 23 is completed.
GetNextItem command number 24 is completed.
GetNextItem command number 25 is completed.
GetNextItem command number 26 is completed.
GetNextItem command number 27 is completed.
GetNextItem command number 28 is completed.
GetNextItem command number 29 is completed.
GetNextItem command number 30 is completed.
GetNextItem command number 31 is completed.
GetNextItem command number 32 is completed.
GetNextItem command number 33 is completed.
PrintTree command number 34 is completed.
ResetTree command number 35 is completed.
GetNextItem command number 36 is completed.
GetNextItem command number 37 is completed.
GetNextItem command number 38 is completed.
GetNextItem command number 39 is completed.
GetNextItem command number 40 is completed.
GetNextItem command number 41 is completed.
GetNextItem command number 42 is completed.
GetNextItem command number 43 is completed.
GetNextItem command number 44 is completed.
GetNextItem command number 45 is completed.
GetNextItem command number 46 is completed.
PrintTree command number 47 is completed.
ResetTree command number 48 is completed.
GetNextItem command number 49 is completed.
GetNextItem command number 50 is completed.
GetNextItem command number 51 is completed.
GetNextItem command number 52 is completed.
GetNextItem command number 53 is completed.
GetNextItem command number 54 is completed.
GetNextItem command number 55 is completed.
GetNextItem command number 56 is completed.
GetNextItem command number 57 is completed.
GetNextItem command number 58 is completed.
GetNextItem command number 59 is completed.
PrintTree command number 60 is completed.
GetLength command number 61 is completed.
AncestorsIterative command number 62 is completed.
AncestorsIterative command number 63 is completed.
AncestorsRecursive command number 64 is completed.
AncestorsRecursive command number 65 is completed.
AncestorsReverse command number 66 is completed.
AncestorsReverse command number 67 is completed.
IsEmpty command number 68 is completed.
DeleteItem command number 69 is completed.
DeleteItem command number 70 is completed.
DeleteItem command number 71 is completed.
DeleteItem command number 72 is completed.
DeleteItem command number 73 is completed.
DeleteItem command number 74 is completed.
DeleteItem command number 75 is completed.
DeleteItem command number 76 is completed.
DeleteItem command number 77 is completed.
DeleteItem command number 78 is completed.
DeleteItem command number 79 is completed.
IsEmpty command number 80 is completed.
PutItem command number 81 is completed.
PutItem command number 82 is completed.
PrintTree command number 83 is completed.
MakeEmpty command number 84 is completed.
IsEmpty command number 85 is completed.
Error command number 86 is completed.
Quit
Testing completed.
```
## Tips
* You can create a text file with all the user input. Then, when you run the program you may pass in this file as in input.
```sh
./tree.out < sample.txt
```
This is a good way to recreate your test each time you run your program
## Submission
Your submission must include a `ancestors.cpp` file.
All other file _.h_ and _.cpp_ should remain unchanged.
/**
* Assignment 9: Ancestors header file
*/
#pragma once
#include "tree_type.h"
void printAncestorsRecursive(TreeNode *tree, ItemType item,
std::ostream &outStream);
// in implementation file
void printAncestorsReverse(TreeNode *tree, ItemType item,
std::ostream &outStream);
// in implementation file
/**
* The following definitions go into file item_type.cpp.
*
* Source: C++ Plus Data Structures 6th Edition
* by Nell Dale, Chip Weems, Tim Richards
*/
#include "item_type.h"
#include <iostream>
ItemType::ItemType() { value = ' '; }
ItemType::ItemType(char newValue) { value = newValue; }
RelationType ItemType::comparedTo(ItemType otherItemType) const {
if (value < otherItemType.value) {
return LESS;
} else if (value > otherItemType.value) {
return GREATER;
} else {
return EQUAL;
}
}
void ItemType::setValue(char newValue) { value = newValue; }
char ItemType::getValue() { return value; }
std::ostream &operator<<(std::ostream &os, const ItemType &item) {
os << item.value;
return os;
}
/**
* The following declarations and definitions go into file item_type.h
*
* Source: C++ Plus Data Structures 6th Edition
* by Nell Dale, Chip Weems, Tim Richards
*/
#pragma once
#include <iostream>
// This code was added by Bradon Ladd to fit Homework 5
const int MAX_ITEMS = 5;
enum RelationType { LESS, GREATER, EQUAL };
class ItemType {
public:
ItemType();
ItemType(char newValue);
RelationType comparedTo(ItemType) const;
char getValue();
void setValue(char newValue);
friend std::ostream &operator<<(std::ostream &os, const ItemType &tc);
private:
char value;
};
/**
* Assignment 9: Student Test Driver
*/
#include "menu.h"
#include <iostream>
#include <string>
int main() { menu(std::cin, std::cout, "Student Test"); }
/**
* Assignment 9: Menu implementation file
*/
#include "menu.h"
#include "item_type.h"
#include "tree_type.h"
#include <cctype>
#include <cstring>
#include <fstream>
#include <iostream>
#include <string>
void menu(std::istream &inStream, std::ostream &outStream,
std::string outputLabel) {
std::string option; // operation to be executed
ItemType item;
std::string orderItem;
TreeType tree;
OrderType order;
char key;
bool found;
bool finished;
int numCommands;
std::ofstream logStream;
logStream.open("log.txt");
// Prepare files
outStream << outputLabel << std::endl;
inStream >> option;
numCommands = 0;
while (option != "Quit") {
if (option == "Menu") {
outStream << "Select options" << std::endl;
outStream << " - Menu "
"Display menu."
<< std::endl;
outStream
<< " - AncestorsIterative <item> Uses an "
"iterative function to travel the tree and print the "
"ancestors of <item>."
<< std::endl;
outStream
<< " - AncestorsRecursive <item> Uses a "
"recursive function to travel the tree and print the "
"ancestors of <item>."
<< std::endl;
outStream
<< " - AncestorsReverse <item> Recursively "
"travels the tree, printing the ancestors of <item>."
<< std::endl;
outStream
<< " - PutItem <item> Puts an "
"item in the tree."
<< std::endl;
outStream
<< " - DeleteItem <item> Deletes "
"an item from the tree."
<< std::endl;
outStream
<< " - GetItem <item> Searches "
"the list for <item>."
<< std::endl;
outStream
<< " - GetLength Returns "
"the amount of nodes in the tree."
<< std::endl;
outStream
<< " - IsEmpty Returns "
"whether the tree is empty or not."
<< std::endl;
outStream
<< " - IsFull Returns "
"whether the tree is full or not."
<< std::endl;
outStream
<< " - PrintTree Prints "
"the tree."
<< std::endl;
outStream
<< " - ResetTree <PRE_ORDER | IN_ORDER | POST_ORDER> Resets "
"the tree to the desired order."
<< std::endl;
outStream
<< " - GetNextItem <PRE_ORDER | IN_ORDER | POST_ORDER> Gets the "
"next item based on the given order traversal."
<< std::endl;
outStream