1
0

added more code

This commit is contained in:
Andrew W
2022-08-28 16:12:16 -05:00
parent 5a2894ed1b
commit 7dabaef6f6
2345 changed files with 1343530 additions and 0 deletions

View File

@ -0,0 +1,329 @@
#include <iostream>
#include <new>
#include "bstree.h"
template <typename SomeType>
BSTree<SomeType>::BSTree()
{
BSTreeNode<SomeType>* rootPtr = NULL;
}
template <typename SomeType>
// start of Delete(...)
// Recursive function that traverses the tree starting at treePtr to locate the data value to be removed
// Once located, DeleteNode is invoked to remove the value from the tree
// If tree is not empty and item is NOT present, throw NotFoundBSTree
void BSTree<SomeType>::Delete(BSTreeNode<SomeType>*& treePtr, SomeType& item)
{
if(item < treePtr->data)
Delete(treePtr->leftPtr, item); // Look in left subtree
else if(item > treePtr->data)
Delete(treePtr->rightPtr, item); // Look in right subtree
else if (treePtr->data == item)
DeleteNode(treePtr); // Node found; call DeleteNode
else
NotFoundBSTree();
}
template <typename SomeType>
void BSTree<SomeType>::DeleteNode(BSTreeNode<SomeType>*& treePtr)
// start of DeleteNode()
// Removes the node pointed to by treePtr from the tree
// Hint: calls GetPredecessor and Delete
{
SomeType data;
BSTreeNode<SomeType>* tempPtr;
if (treePtr->leftPtr==NULL)
{
treePtr=treePtr->rightPtr;
delete tempPtr;
}
else if (treePtr->rightPtr==NULL)
{
treePtr = treePtr->leftPtr;
delete tempPtr;
}
else
{
GetPredecessor(treePtr->leftPtr);
treePtr->data = data;
Delete(treePtr->leftPtr, data);
}
} // end of DeleteNode(...)
template <typename SomeType>
void BSTree<SomeType>::Insert(BSTreeNode<SomeType>*& ptr, SomeType item)
// Start of Insert(...)
// Recursive function that finds the correct position of item and adds it to the tree
// Throws FoundInBSTree if item is already in the tree
{
if (ptr==NULL)
{
ptr = new BSTreeNode<SomeType>;
ptr->rightPtr = NULL;
ptr->leftPtr = NULL;
ptr->data = item;
}
else if (item < ptr->data)
Insert(ptr->leftPtr, item);
else
Insert(ptr->rightPtr, item);
//else if (ptr->data == item) FoundInBSTree();
}
template <typename SomeType>
void BSTree<SomeType>::Destroy(BSTreeNode<SomeType>*& ptr)
// Destroy()
// Recursively deallocates every node in the tree pointed to by ptr
{
if (&ptr==NULL)
{
Destroy(ptr->leftPtr);
Destroy(ptr->rightPtr);
delete ptr;
}
} // end of Destroy()
template <typename SomeType>
void BSTree<SomeType>::CopyTree(BSTreeNode<SomeType>*& copy, const BSTreeNode<SomeType>* originalTree)
// CopyTree()
// Recursively copies all data from original tree into copy
{
if (originalTree == NULL)
copy = NULL;
else
{
copy = new BSTreeNode<SomeType>;
copy->data = originalTree->data;
CopyTree(copy->leftPtr, originalTree->leftPtr);
CopyTree(copy->rightPtr, originalTree->rightPtr);
}
}
template <typename SomeType>
SomeType BSTree<SomeType>::GetPredecessor(BSTreeNode<SomeType>* treePtr) const
// GetPredecessor()
// Finds the largest data value in the tree pointed to by treePtr and returns that data value
// as the functions return value
{
while (treePtr->rightPtr != NULL)
treePtr = treePtr->rightPtr;
return (treePtr->data);
}
template <typename SomeType>
int BSTree<SomeType>::CountNodes(BSTreeNode<SomeType>* treePtr) const
// CountNodes()
// Recursive function that counts every node in the tree pointed to by treePtr and returns the
// count as the function return value
{
if (&treePtr == NULL)
return 0;
else
return CountNodes(treePtr->leftPtr) + CountNodes(treePtr->rightPtr) + 1;
}
template <typename SomeType>
int BSTree<SomeType>::LevelCount(BSTreeNode<SomeType>* treePtr) const
// LevelCount()
// Recursive function that traverses the entire tree to determine the total number of levels in the tree
{
if(treePtr->leftPtr == NULL && treePtr->rightPtr == NULL)
return 0;
int left = 0;
if (treePtr->leftPtr != NULL)
left = LevelCount(treePtr->leftPtr);
int right = 0;
if (treePtr->rightPtr != NULL)
right = LevelCount(treePtr->rightPtr);
return (max(left, right) + 1);
}
template <typename SomeType>
int BSTree<SomeType>::FindLevel(BSTreeNode<SomeType>* treePtr, SomeType item) const
// FindLevel()
// Recursive function that traverses the tree looking for item and returns the level where
// item was found
{
int static level = 0;
// if (treePtr == NULL)
// return 0;
// int left = 0;
// if (treePtr->leftPtr != NULL && item != treePtr->data)
// left = LevelCount(treePtr->leftPtr);
// int right = 0;
// if (treePtr->rightPtr != NULL)
// right = LevelCount(treePtr->rightPtr);
// return (max(left, right));
}
template <typename SomeType>
//start of BSTree(...)
BSTree<SomeType>::BSTree(const BSTree<SomeType>& someTree)
// BSTree()
// Copy constructor for BSTree
// Hint: calls CopyTree
{
if (someTree.rootPtr==NULL)
{
rootPtr=NULL;
}
else
CopyTree(this->rootPtr, someTree.rootPtr);
}
template <typename SomeType>
void BSTree<SomeType>::operator=(const BSTree<SomeType>& originalTree)
// operator=()
// Overloaded assignment operator for BSTree.
// Hint: calls CopyTree
{
if (&originalTree == this)
return;
Destroy(rootPtr);
CopyTree(rootPtr, originalTree.rootPtr);
}
template <typename SomeType>
BSTree<SomeType>::~BSTree()
// ~BSTree()
// Destructor deallocates all tree nodes
// Hint: calls the private helper function Destroy
{
Destroy(rootPtr);
}
template <typename SomeType>
void BSTree<SomeType>::InsertItem(SomeType item)
// InsertItem()
// Inserts item into BSTree; if tree already full, throws FullBSTree exception
// If item is already in BSTree, throw FoundInBSTree exception
// Hint: calls the private helper function Insert
{
Insert(rootPtr,item);
}
template <typename SomeType>
SomeType BSTree<SomeType>::DeleteItem(SomeType item)
// DeleteItem()
// Deletes item from BSTree if item is present AND returns object via function return value
// If tree is empty, throw the EmptyBSTree exception
// If tree is not empty and item is NOT present, throw NotFoundBSTree
// Hint: calls the private helper function Delete
{
//if (&rootPtr == NULL) EmptyBSTree();
if (IsEmpty())
throw EmptyBSTree();
else
SomeType itemCopy = item; // Make an copy for item
Delete(rootPtr, item); // Delete item in tree
// return itemCopy;
}
template <typename SomeType>
void BSTree<SomeType>::MakeEmpty()
// MakeEmpty()
// Deallocates all BSTree nodes and sets root pointer to NULL
// Hint: calls the private helper function Destroy
{
Destroy(rootPtr);
rootPtr=NULL;
}
template <typename SomeType>
int BSTree<SomeType>::Size() const
// Size()
// Returns total number of data values stored in tree
{
return CountNodes(rootPtr);
}
template <typename SomeType>
bool BSTree<SomeType>::IsFull() const
{
BSTreeNode<SomeType>* location;
try
{
location = new BSTreeNode<SomeType>;
delete location;
return false;
}
catch( bad_alloc )
{
return true;
}
}
template <typename SomeType>
bool BSTree<SomeType>::IsEmpty() const
{
if (rootPtr == NULL) return true;
return false;
}
template <typename SomeType>
SomeType BSTree<SomeType>::Min() const
{
// if (rootPtr == NULL) EmptyBSTree();
// BSTreeNode<SomeType>* current;
// &current = rootPtr;
// while (current > current->data != NULL)
// {
// current = current->leftPtr;
// }
// return(current->leftPtr);
}
template <typename SomeType>
SomeType BSTree<SomeType>::Max() const
{
// if (rootPtr == NULL) EmptyBSTree();
// BSTreeNode<SomeType>* current;
// current = rootPtr;
// while (current > current->data != NULL)
// {
// current = current->rightPtr;
// }
// return(&current->rightPtr);
}
template <typename SomeType>
int BSTree<SomeType>::TotalLevels() const
// TotalLevels()
// Returns the maximum level value for current tree contents
// Levels are numbered 0, 1, ..., N-1. This function returns N
// Throws EmptyBSTree if empty
// Hint: calls the private helper function LevelCount
{
if (&rootPtr == NULL) EmptyBSTree();
int level = LevelCount(rootPtr);
return level+1;
}
template <typename SomeType>
int BSTree<SomeType>::Level(SomeType item) const
// Level()
// Returns the level within the BSTree at which the value item is found
// If tree is empty, throws EmptyBSTree
// If tree is not empty and item is not found, throws NotFoundBSTree
// Hint: calls the private helper funtion FindLevel
{
if (rootPtr == NULL) EmptyBSTree();
return FindLevel(rootPtr, item);
}

View File

@ -0,0 +1,316 @@
#include <iostream>
#include <new>
#include "bstree.h"
template <typename SomeType>
// start of Delete(...)
// Recursive function that traverses the tree starting at treePtr to locate the data value to be removed
// Once located, DeleteNode is invoked to remove the value from the tree
// If tree is not empty and item is NOT present, throw NotFoundBSTree
void BSTree<SomeType>::Delete(BSTreeNode<SomeType>*& treePtr, SomeType& item)
{
if(item < treePtr->data)
Delete(treePtr->leftPtr, item); // Look in left subtree
else if(item > treePtr->data)
Delete(treePtr->rightPtr, item); // Look in right subtree
else if (treePtr == item)
DeleteNode(treePtr); // Node found; call DeleteNode
else
NotFoundBSTree();
}
template <typename SomeType>
void BSTree<SomeType>::DeleteNode(BSTreeNode<SomeType>*& treePtr)
// start of DeleteNode()
// Removes the node pointed to by treePtr from the tree
// Hint: calls GetPredecessor and Delete
{
SomeType data;
BSTreeNode<SomeType>* tempPtr;
if (treePtr->leftPtr==NULL)
{
treePtr=treePtr->rightPtr;
delete tempPtr;
}
else if (treePtr->rightPtr==NULL)
{
treePtr = treePtr->leftPtr;
delete tempPtr;
}
else
{
GetPredecessor(treePtr->leftPtr, data);
treePtr->data = data;
Delete(treePtr->leftPtr, data);
}
} // end of DeleteNode(...)
template <typename SomeType>
void BSTree<SomeType>::Insert(BSTreeNode<SomeType>*& ptr, SomeType item)
// Start of Insert(...)
// Recursive function that finds the correct position of item and adds it to the tree
// Throws FoundInBSTree if item is already in the tree
{
if (ptr == item) FoundInBSTree();
if (ptr==NULL)
{
ptr = new BSTreeNode<SomeType>;
ptr->rightPtr = NULL;
ptr->leftPtr = NULL;
ptr->data = item;
}
else if (item < ptr->data)
Insert(ptr->leftPtr, item);
else
Insert(item->rightptr, item);
if (ptr ==item) FoundInBSTree();
}
template <typename SomeType>
void BSTree<SomeType>::Destroy(BSTreeNode<SomeType>*& ptr)
// Destroy()
// Recursively deallocates every node in the tree pointed to by ptr
{
if (ptr==NULL)
{
Destroy(ptr->leftPtr);
Destroy(ptr->rightPtr);
delete ptr;
}
} // end of Destroy()
template <typename SomeType>
void BSTree<SomeType>::CopyTree(BSTreeNode<SomeType>*& copy, const BSTreeNode<SomeType>* originalTree)
// CopyTree()
// Recursively copies all data from original tree into copy
{
if (originalTree == NULL)
copy = NULL;
else
{
copy = new BSTreeNode<typename SomeType>;
copy->data = originalTree->data;
CopyTree(copy->leftPtr, originalTree->leftPtr);
CopyTree(copy->rightPtr, originalTree->rightPtr);
}
}
template <typename SomeType>
SomeType BSTree<SomeType>::GetPredecessor(BSTreeNode<SomeType>* treePtr) const
// GetPredecessor()
// Finds the largest data value in the tree pointed to by treePtr and returns that data value
// as the functions return value
{
while (treePtr->rightPtr != NULL)
treePtr = treePtr->rightPtr;
data = treePtr->data;
}
template <typename SomeType>
int BSTree<SomeType>::CountNodes(BSTreeNode<SomeType>* treePtr) const
// CountNodes()
// Recursive function that counts every node in the tree pointed to by treePtr and returns the
// count as the function return value
{
if (treePtr == NULL)
return 0;
else
return CountNodes(treePtr->leftPtr) + CountNodes(treePtr->rightPtr) + 1;
}
template <typename SomeType>
int BSTree<SomeType>::LevelCount(BSTreeNode<SomeType>* treePtr) const
// LevelCount()
// Recursive function that traverses the entire tree to determine the total number of levels in the tree
{
if(treePtr->leftPtr == NULL && treePtr->rightPtr == NULL)
return 0;
int left = 0;
if (treePtr->leftPtr != NULL)
left = LevelCount(treePtr->leftPtr);
int right = 0;
if (treePtr->rightPtr != NULL)
right = LevelCount(treePtr->rightPtr);
return (max(left, right) + 1);
}
template <typename SomeType>
int BSTree<SomeType>::FindLevel(BSTreeNode<SomeType>* treePtr, SomeType item) const
// FindLevel()
// Recursive function that traverses the tree looking for item and returns the level where
// item was found
{
int static level = 0;
if (treePtr == NULL)
return 0;
int left = 0;
if (treePtr->leftPtr != NULL && treePtr->data != item)
left = LevelCount(treePtr->leftPtr);
int right = 0;
if (treePtr->rightPtr != NULL)
right = LevelCount(treePtr->rightPtr);
return (max(left, right));
}
template <typename SomeType>
BSTree<SomeType>::BSTree()
{
rootPtr=NULL;
}
template <typename SomeType>
//start of BSTree(...)
BSTree<SomeType>::BSTree(const BSTree<SomeType>& someTree)
// BSTree()
// Copy constructor for BSTree
// Hint: calls CopyTree
{
CopyTree(someTree);
}
template <typename SomeType>
void BSTree<SomeType>::operator=(const BSTree<SomeType>& originalTree)
// operator=()
// Overloaded assignment operator for BSTree.
// Hint: calls CopyTree
{
if (originalTree==this)
return;
Destroy(rootPtr);
CopyTree(originalTree);
}
template <typename SomeType>
BSTree<SomeType>::~BSTree()
// ~BSTree()
// Destructor deallocates all tree nodes
// Hint: calls the private helper function Destroy
{
Destroy(rootPtr);
}
template <typename SomeType>
void BSTree<SomeType>::InsertItem(SomeType item)
// InsertItem()
// Inserts item into BSTree; if tree already full, throws FullBSTree exception
// If item is already in BSTree, throw FoundInBSTree exception
// Hint: calls the private helper function Insert
{
Insert(item);
}
template <typename SomeType>
SomeType BSTree<SomeType>::DeleteItem(SomeType item)
// DeleteItem()
// Deletes item from BSTree if item is present AND returns object via function return value
// If tree is empty, throw the EmptyBSTree exception
// If tree is not empty and item is NOT present, throw NotFoundBSTree
// Hint: calls the private helper function Delete
{
if (rootPtr == NULL) EmptyBSTree();
Delete(item);
}
template <typename SomeType>
void BSTree<SomeType>::MakeEmpty()
// MakeEmpty()
// Deallocates all BSTree nodes and sets root pointer to NULL
// Hint: calls the private helper function Destroy
{
Delete(rootPtr);
rootPtr=NULL;
}
template <typename SomeType>
int BSTree<SomeType>::Size() const
// Size()
// Returns total number of data values stored in tree
{
return CountNodes(rootPtr);
}
template <typename SomeType>
bool BSTree<SomeType>::IsFull() const
{
BSTreeNode<SomeType>* location;
try
{
location = new BSTreeNode<SomeType>;
delete location;
return false;
}
catch( bad_alloc )
{
return true;
}
}
template <typename SomeType>
bool BSTree<SomeType>::IsEmpty() const
{
if (rootPtr == NULL) return true;
return false;
}
template <typename SomeType>
SomeType BSTree<SomeType>::Min() const
{
if (rootPtr == NULL) EmptyBSTree();
int res = rootPtr->data;
BSTreeNode<SomeType>* current;
while (current->leftPtr != NULL)
{
current = current->leftPtr;
}
return(current->leftPtr);
}
template <typename SomeType>
SomeType BSTree<SomeType>::Max() const
{
if (rootPtr == NULL) EmptyBSTree();
BSTreeNode<SomeType>* current;
while (current->rightPtr != NULL)
{
current = current->rightPtr;
}
return(current->rightPtr);
}
template <typename SomeType>
int BSTree<SomeType>::TotalLevels() const
// TotalLevels()
// Returns the maximum level value for current tree contents
// Levels are numbered 0, 1, ..., N-1. This function returns N
// Throws EmptyBSTree if empty
// Hint: calls the private helper function LevelCount
{
if (rootPtr == NULL) EmptyBSTree();
int level = LevelCount(rootPtr);
return level+1;
}
template <typename SomeType>
int BSTree<SomeType>::Level(SomeType item) const
// Level()
// Returns the level within the BSTree at which the value item is found
// If tree is empty, throws EmptyBSTree
// If tree is not empty and item is not found, throws NotFoundBSTree
// Hint: calls the private helper funtion FindLevel
{
if (rootPtr == NULL) EmptyBSTree();
return FindLevel(rootPtr, item);
}

187
CPE212/Project_05/bstree.h Normal file
View File

@ -0,0 +1,187 @@
//
// bstree.h 2010 Fall CPE 212 - Project05 - Binary Search Tree Template
//
// Provides a declaration of the BSTree class template.
//
// DO NOT MODIFY OR SUBMIT THIS FILE
//
#ifndef BSTREE_H
#define BSTREE_H
#include <cstddef>
#include <new>
#include <string>
using namespace std;
class FullBSTree // Exception class models full BSTree condition
{
/* No code here */
};
class EmptyBSTree // Exception class models empty BSTree condition
{
/* No code here */
};
class NotFoundBSTree // Exception class models not found in BSTree condition
{
/* No code here */
};
class FoundInBSTree // Exception class models found in BSTree condition
{
/* No code here */
};
template <typename SomeType>
struct BSTreeNode // Node of BSTree
{
SomeType data; // Data stored in node
BSTreeNode<SomeType>* leftPtr; // Pointer to left subtree
BSTreeNode<SomeType>* rightPtr; // Pointer to right subtree
};
template <typename SomeType>
class BSTree // BSTree Abstract Data Type
{
private:
BSTreeNode<SomeType>* rootPtr; // Pointer to root of BSTree
/************** Start of Private Helper Functions You Must Implement ****************/
void Delete(BSTreeNode<SomeType>*& treePtr, SomeType& item);
// Delete()
// Recursive function that traverses the tree starting at treePtr to locate the data value to be removed
// Once located, DeleteNode is invoked to remove the value from the tree
// If tree is not empty and item is NOT present, throw NotFoundBSTree
void DeleteNode(BSTreeNode<SomeType>*& treePtr);
// DeleteNode()
// Removes the node pointed to by treePtr from the tree
// Hint: calls GetPredecessor and Delete
void Insert(BSTreeNode<SomeType>*& ptr, SomeType item);
// Insert()
// Recursive function that finds the correct position of item and adds it to the tree
// Throws FoundInBSTree if item is already in the tree
void Destroy(BSTreeNode<SomeType>*& ptr);
// Destroy()
// Recursively deallocates every node in the tree pointed to by ptr
void CopyTree(BSTreeNode<SomeType>*& copy, const BSTreeNode<SomeType>* originalTree);
// CopyTree()
// Recursively copies all data from original tree into copy
SomeType GetPredecessor(BSTreeNode<SomeType>* treePtr) const;
// GetPredecessor()
// Finds the largest data value in the tree pointed to by treePtr and returns that data value
// as the functions return value
int CountNodes(BSTreeNode<SomeType>* treePtr) const;
// CountNodes()
// Recursive function that counts every node in the tree pointed to by treePtr and returns the
// count as the function return value
int LevelCount(BSTreeNode<SomeType>* treePtr) const;
// LevelCount()
// Recursive function that traverses the entire tree to determine the total number of levels in the tree
int FindLevel(BSTreeNode<SomeType>* treePtr, SomeType item) const;
// FindLevel()
// Recursive function that traverses the tree looking for item and returns the level where
// item was found
/************** End of Private Helper Functions You Must Implement ****************/
public:
/************** Start of Public Interface Functions You Must Implement ****************/
BSTree();
// BSTree()
// Default constructor initializes root pointer to NULL
BSTree(const BSTree<SomeType>& someTree);
// BSTree()
// Copy constructor for BSTree
// Hint: calls CopyTree
void operator=(const BSTree<SomeType>& originalTree);
// operator=()
// Overloaded assignment operator for BSTree.
// Hint: calls CopyTree
~BSTree();
// ~BSTree()
// Destructor deallocates all tree nodes
// Hint: calls the private helper function Destroy
void InsertItem(SomeType item);
// InsertItem()
// Inserts item into BSTree; if tree already full, throws FullBSTree exception
// If item is already in BSTree, throw FoundInBSTree exception
// Hint: calls the private helper function Insert
SomeType DeleteItem(SomeType item);
// DeleteItem()
// Deletes item from BSTree if item is present AND returns object via function return value
// If tree is empty, throw the EmptyBSTree exception
// If tree is not empty and item is NOT present, throw NotFoundBSTree
// Hint: calls the private helper function Delete
void MakeEmpty();
// MakeEmpty()
// Deallocates all BSTree nodes and sets root pointer to NULL
// Hint: calls the private helper function Destroy
int Size() const;
// Size()
// Returns total number of data values stored in tree
bool IsFull() const;
// IsFull()
// Returns true if BSTree is full; returns false otherwise
bool IsEmpty() const;
// IsEmpty()
// Returns true if BSTree is empty; returns false otherwise
SomeType Min() const;
// Min()
// Returns minimum value in tree; throws EmptyBSTree if tree is empty
SomeType Max() const;
// Max()
// Returns maximum value in tree; throws EmptyBSTree if tree is empty
int TotalLevels() const;
// TotalLevels()
// Returns the maximum level value for current tree contents
// Levels are numbered 0, 1, ..., N-1. This function returns N
// Throws EmptyBSTree if empty
// Hint: calls the private helper function LevelCount
int Level(SomeType item) const;
// Level()
// Returns the level within the BSTree at which the value item is found
// If tree is empty, throws EmptyBSTree
// If tree is not empty and item is not found, throws NotFoundBSTree
// Hint: calls the private helper funtion FindLevel
/************** End of Functions You Must Implement ****************/
void Print() const; // DO NOT WRITE THIS FUNCTION
// Print()
// Prints binary search tree contents in inorder, preorder, and postorder forms
// NOTE: THIS CODE HAS BEEN INCLUDED AT THE END OF main.cpp
};
#include "bstree.cpp" // Note: Template classes cannot be compiled on their own
// since the data type argument is found in the client code.
#endif

320
CPE212/Project_05/main.cpp Normal file
View File

@ -0,0 +1,320 @@
//
// main.cpp 2010 Fall CPE 212 - Project05 - Binary Search Tree Template
//
// Driver program for BSTree ADT Template -- The text files (read by this code) contain a series
// of commands that will help you test your BSTree ADT Template code by triggering various class methods.
//
// DO NOT SUBMIT THIS FILE
//
#include <iostream>
#include <fstream>
#include <string>
#include "bstree.h"
#include "student.h"
using namespace std;
int main (int argc, char * const argv[])
{
ifstream inputs; // Input file for commands
char op, ch; // Hold operation and optional char input
BSTree<Student>* tPtr = NULL; // Will point to BSTree object
string comment;
// Output usage message if one input file name is not provided
if (argc != 2)
{
cout << "Usage:\n project05 <inputfile>\n";
return 1;
}
// Attempt to open input file -- terminate if file does not open
inputs.open(argv[1]);
if (!inputs)
{
cout << "Error - unable to open input file" << endl;
return 1;
}
// Process commands from input file
getline(inputs, comment);
cout << endl << comment << endl << endl; // Output header comment
inputs >> op; // Attempt to input first command
while (inputs)
{
// Select and perform operation input from file
switch (op)
{
case '#': // Test file comment
getline(inputs, comment);
cout << '#' << comment;
break;
case 'c': // Constructor
cout << endl << "Constructor()";
try
{
tPtr = new BSTree<Student>;
cout << endl;
}
catch ( std::bad_alloc )
{
cout << "Failed : Terminating now..." << endl;
return 1;
}
break;
case '!': // Test copy constructor
try
{
cout << endl << "*** Start Copy Constructor Test ***" << endl;
BSTree<Student> dummy = *tPtr;
Student s;
inputs >> s;
cout << "Print Copy without new value" << endl;
dummy.Print();
dummy.InsertItem(s);
cout << "Print Copy plus new value" << endl;
dummy.Print();
cout << "Print Original without new value" << endl;
tPtr->Print();
cout << "CopyConstructor -- successful" << endl;
}
catch (...)
{
cout << "CopyConstructor -- Failed: copy constructor" << endl;
}
cout << "*** End Copy Constructor Test ***" << endl;
break;
case '=': // Test overloaded assignment operator
try
{
cout << endl << "*** Start Operator= Test ***" << endl;
BSTree<Student> dummy;
dummy = *tPtr;
Student s;
inputs >> s;
cout << "Print Copy without new value" << endl;
dummy.Print();
dummy.InsertItem(s);
cout << "Print Copy plus new value" << endl;
dummy.Print();
cout << "Print Original without new value" << endl;
tPtr->Print();
cout << "Operator= -- successful" << endl;
}
catch (...)
{
cout << "Operator= -- Failed: assignment operator" << endl;
}
cout << "*** End Operator= Test ***" << endl;
break;
case '+': // InsertItem
try
{
Student s; // Create temporary object to hold student info from file
inputs >> s; // Use overloaded operator to populate temporary object
cout << "InsertItem('";
s.Print();
cout << "')";
tPtr->InsertItem(s);
}
catch (FullBSTree)
{
cout << " -- Failed Full BSTree";
}
catch (FoundInBSTree)
{
cout << " -- Failed Item Already Found In BSTree";
}
cout << endl;
break;
case '-': // DeleteItem
try
{
int id;
inputs >> id;
Student s(id, "NULL", "NULL");
cout << "DeleteItem('" << id << "') -- ";
s = tPtr->DeleteItem(s);
cout << "Deleted ";
s.Print();
}
catch (EmptyBSTree)
{
cout << " -- Failed Empty BSTree";
}
catch (NotFoundBSTree)
{
cout << " -- Failed Not Found in BSTree";
}
cout << endl;
break;
case 'p': // Print BSTree
tPtr->Print();
break;
case 's': // Size of BSTree
cout << "Size() -- " << tPtr->Size() << endl;
break;
case 'm': // Make BSTree Empty
tPtr->MakeEmpty();
cout << "MakeEmpty()"<< endl;
break;
case 'd': // Destructor
delete tPtr;
tPtr = NULL;
cout << "Destructor()" << endl << endl;
break;
case '<': // Minimum
cout << "Min() -- ";
try
{
cout << tPtr->Min() << endl;
}
catch ( EmptyBSTree )
{
cout << "Failed Empty BSTree" << endl;
}
break;
case '>': // Maximum
cout << "Max() -- ";
try
{
cout << tPtr->Max() << endl;
}
catch ( EmptyBSTree )
{
cout << "Failed Empty BSTree" << endl;
}
break;
case 'l': // Size of BSTree
try
{
cout << "TotalLevels() -- " << tPtr->TotalLevels() << endl;
}
catch ( EmptyBSTree )
{
cout << "TotalLevels() -- Failed Empty BSTree" << endl;
}
break;
case '?': // Size of BSTree
try
{
int id;
inputs >> id;
Student s(id, "NULL", "NULL");
cout << "Level('" << s << "') -- ";
cout << tPtr->Level(s) << endl;
}
catch ( EmptyBSTree )
{
cout << "Failed Empty BSTree" << endl;
}
break;
default: // Error
cout << "Error - unrecognized operation '" << op << "'" << endl;
cout << "Terminating now..." << endl;
return 1;
break;
}
inputs >> op; // Attempt to input next command
}
return 0;
}
/************** Implementation of Print() function ********************/
// DO NOT MODIFY THIS CODE
#include <queue>
// This code uses the Standard Template Libary queue class, container adapter wrapper
// that makes the deque (double-ended queue) look more like a single-ended queue.
// Note the different names used for the enqueue and dequeue operations.
template <typename SomeType>
void PreOrder(BSTreeNode<SomeType>* tree, queue<SomeType>& preorder)
// Post: preorder contains the tree items in preorder.
{
if (tree != NULL)
{
preorder.push(tree->data);
PreOrder(tree->leftPtr, preorder);
PreOrder(tree->rightPtr, preorder);
}
}
template <typename SomeType>
void InOrder(BSTreeNode<SomeType>* tree, queue<SomeType>& inorder)
// Post: inorder contains the tree items in inorder.
{
if (tree != NULL)
{
InOrder(tree->leftPtr, inorder);
inorder.push(tree->data);
InOrder(tree->rightPtr, inorder);
}
}
template <typename SomeType>
void PostOrder(BSTreeNode<SomeType>* tree, queue<SomeType>& postorder)
// Post: postorder contains the tree items in postorder.
{
if (tree != NULL)
{
PostOrder(tree->leftPtr, postorder);
PostOrder(tree->rightPtr, postorder);
postorder.push(tree->data);
}
}
template <typename SomeType>
void BSTree<SomeType>::Print() const
{
queue<SomeType> preorder, inorder, postorder;
PreOrder(rootPtr, preorder);
InOrder(rootPtr, inorder);
PostOrder(rootPtr, postorder);
cout << "Print() \n-- Inorder = { ";
while (!inorder.empty())
{
cout << inorder.front() << " ";
inorder.pop();
}
cout << "} \n-- Preorder = { ";
while (!preorder.empty())
{
cout << preorder.front() << " ";
preorder.pop();
}
cout << "} \n-- Postorder = { ";
while (!postorder.empty())
{
cout << postorder.front() << " ";
postorder.pop();
}
cout << "}" << endl;
}

BIN
CPE212/Project_05/main.o Normal file

Binary file not shown.

View File

@ -0,0 +1,19 @@
# Project06 makefile
# To disable the gcov options use a # to comment out the
# following line and uncomment the line below it
#CC = g++ -fprofile-arcs -ftest-coverage
CC = g++
project05: main.o student.o
$(CC) main.o student.o -o project05
main.o: main.cpp bstree.h
$(CC) -c main.cpp
student.o: student.cpp student.h
$(CC) -c student.cpp
clean:
rm *.o *.gcda *.gcno *.gcov project05

View File

@ -0,0 +1,68 @@
// p05input1.txt - Test BSTree(), InsertItem(), ~BSTree()
# Test creating empty binary search tree
c
p
d
# Test adding one student object to binary search tree
c
+ 5678 Washington George
p
d
# Test adding student objects pre-sorted highest to lowest id
c
+ 5678 Washington George
p
+ 1234 Jefferson Thomas
p
+ 1000 Madison James
p
+ 888 Nixon Richard
p
+ 555 Lincoln Abraham
p
d
# Test adding student objects pre-sorted lowest to highest id
c
+ 555 Lincoln Abraham
p
+ 888 Nixon Richard
p
+ 1000 Madison James
p
+ 1234 Jefferson Thomas
p
+ 5678 Washington George
p
d
# Test adding an unsorted sequence of student objects
c
+ 1234 Jefferson Thomas
p
+ 888 Nixon Richard
p
+ 5678 Washington George
p
+ 555 Lincoln Abraham
p
+ 1000 Madison James
p
d
# Test adding duplicate student object to tree
c
+ 1234 Jefferson Thomas
p
+ 1234 Jefferson Thomas
p
d

View File

@ -0,0 +1,125 @@
// p05input2.txt - Test BSTree(), InsertItem(), DeleteItem(), MakeEmpty(), ~BSTree()
# Test deleting leaf node that is the left child of its parent
c
+ 5678 Washington George
+ 1234 Jefferson Thomas
+ 1000 Madison James
+ 555 Lincoln Abraham
p
- 555
p
d
# Test deleting leaf node that is the right child of its parent
c
+ 555 Lincoln Abraham
+ 1000 Madison James
+ 1234 Jefferson Thomas
+ 5678 Washington George
p
- 5678
p
d
# Test deleting parent node that has only a left child
c
+ 5678 Washington George
+ 1234 Jefferson Thomas
+ 1000 Madison James
+ 555 Lincoln Abraham
p
- 1000
p
d
# Test deleting parent node that has only a right child
c
+ 555 Lincoln Abraham
+ 1000 Madison James
+ 1234 Jefferson Thomas
+ 5678 Washington George
p
- 1234
p
d
# Test deleting parent node that has two children
c
+ 555 Lincoln Abraham
+ 1000 Madison James
+ 1234 Jefferson Thomas
+ 5678 Washington George
+ 888 Nixon Richard
p
- 1000
p
d
# Test deleting root node
c
+ 1000 Madison James
+ 1234 Jefferson Thomas
+ 555 Lincoln Abraham
p
- 1000
p
d
# Test deleting only node leaving empty tree
c
+ 888 Nixon Richard
p
- 888
p
d
# Test deleting value not in tree
c
+ 888 Nixon Richard
p
- 555
p
d
# Test deleting value from empty tree
c
- 333
p
d
# Test MakeEmpty deallocating all nodes
c
+ 1234 Jefferson Thomas
+ 888 Nixon Richard
+ 5678 Washington George
+ 555 Lincoln Abraham
+ 1000 Madison James
p
m
p
d
# Test MakeEmpty deallocating nodes from empty tree
c
p
m
p
d

View File

@ -0,0 +1,28 @@
// p05input3.txt - Test BSTree(), CopyConstructor, = , ~BSTree()
# Test Copy Constructor
c
+ 1234 Jefferson Thomas
+ 888 Nixon Richard
+ 5678 Washington George
+ 555 Lincoln Abraham
+ 1000 Madison James
p
! 007 Bond James
d
# Test Assignment Operator
c
+ 1234 Jefferson Thomas
+ 888 Nixon Richard
+ 5678 Washington George
+ 555 Lincoln Abraham
+ 1000 Madison James
p
= 007 Bond James
d

View File

@ -0,0 +1,83 @@
// p05input4.txt - Test Size()
# Test correctness of size when adding student objects
c
p
s
+ 1234 Jefferson Thomas
p
s
+ 888 Nixon Richard
p
s
+ 5678 Washington George
p
s
+ 555 Lincoln Abraham
p
s
+ 1000 Madison James
p
s
d
# Test correctness of size when deleting student objects
c
s
+ 1234 Jefferson Thomas
+ 888 Nixon Richard
+ 5678 Washington George
+ 555 Lincoln Abraham
+ 1000 Madison James
p
s
- 555
p
s
- 1234
p
s
- 888
p
s
- 5678
p
s
- 1000
p
s
d
# Test correctness of size when adding and deleting student objects
c
+ 1234 Jefferson Thomas
s
+ 888 Nixon Richard
s
+ 5678 Washington George
p
s
- 888
p
s
+ 555 Lincoln Abraham
p
s
+ 1000 Madison James
p
s
- 1234
p
s
d
# Test correctness of size for empty tree
c
p
s
d

View File

@ -0,0 +1,132 @@
// p05input5.txt - Test Min(), Max()
# Test when min is a left child
c
+ 5678 Washington George
+ 1234 Jefferson Thomas
p
<
+ 1000 Madison James
+ 555 Lincoln Abraham
p
<
d
# Test when min is root
c
+ 555 Lincoln Abraham
+ 1000 Madison James
+ 1234 Jefferson Thomas
+ 5678 Washington George
p
<
d
# Test deleting parent node that has two children
c
+ 1234 Jefferson Thomas
+ 555 Lincoln Abraham
+ 1000 Madison James
p
<
+ 5678 Washington George
+ 888 Nixon Richard
p
<
d
# Test when min has a right child
c
+ 1000 Madison James
+ 1234 Jefferson Thomas
p
<
+ 555 Lincoln Abraham
+ 888 Nixon Richard
p
<
d
# Test when min is the only value
c
+ 888 Nixon Richard
p
<
d
# Test min when tree is empty
c
p
<
d
# Test when max is a left child
c
+ 5678 Washington George
+ 1234 Jefferson Thomas
+ 1000 Madison James
+ 555 Lincoln Abraham
p
>
d
# Test when max is root
c
+ 555 Lincoln Abraham
+ 1000 Madison James
+ 1234 Jefferson Thomas
+ 5678 Washington George
p
>
d
# Test max when parent node that has two children
c
+ 1234 Jefferson Thomas
+ 555 Lincoln Abraham
+ 1000 Madison James
p
>
+ 5678 Washington George
+ 888 Nixon Richard
p
>
d
# Test when max has a right child
c
+ 1000 Madison James
+ 1234 Jefferson Thomas
p
>
+ 555 Lincoln Abraham
+ 888 Nixon Richard
p
>
d
# Test when max is the only value
c
+ 888 Nixon Richard
p
>
d
# Test max when tree is empty
c
p
>
d

View File

@ -0,0 +1,46 @@
// p05input6.txt - Test TotalLevels()
# Test totallevels as tree grows level by level
c
+ 555 Lincoln Abraham
p
l
+ 1000 Madison James
p
l
+ 1234 Jefferson Thomas
p
l
+ 5678 Washington George
p
l
d
# Test totallevels as with a more balance tree
c
+ 555 Lincoln Abraham
+ 1000 Madison James
+ 1234 Jefferson Thomas
+ 5678 Washington George
+ 888 Nixon Richard
p
l
d
# Test totallevels with single node tree
c
+ 888 Nixon Richard
p
l
d
# Test totallevels with empty tree
c
p
l
d

View File

@ -0,0 +1,47 @@
// p05input7.txt - Test Level()
# Test level as tree grows level by level
c
+ 555 Lincoln Abraham
+ 1000 Madison James
+ 1234 Jefferson Thomas
+ 5678 Washington George
p
? 555
? 1000
? 1234
? 5678
d
# Test level as with a more balance tree
c
+ 555 Lincoln Abraham
+ 1000 Madison James
+ 1234 Jefferson Thomas
+ 5678 Washington George
+ 888 Nixon Richard
p
? 555
? 1000
? 1234
? 5678
? 888
d
# Test level with single node tree
c
+ 888 Nixon Richard
p
? 888
d
# Test level with empty tree
c
p
? 888
d

BIN
CPE212/Project_05/project05 Normal file

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,66 @@
//
// student.cpp CPE 212 Fall 2010 -- Project05 - Binary Search Tree Template
//
// DO NOT MODIFY OR SUBMIT THIS FILE
//
#include "student.h"
Student::Student()
// Constructor initializes sid to -1, lastname and firstname to empty string
{
sid = -1;
lastname = "";
firstname = "";
} // End Student::Student()
Student::Student(int id, string lname, string fname)
// Constructor initializes sid, lastname, firstname to id, lname, and fname respectively
{
sid = id;
lastname = lname;
firstname = fname;
} // End Student::Student()
Student::Student(const Student& s)
// Copy constructor -- copies attributes of s into attribute variables of current object
{
sid = s.sid;
lastname = s.lastname;
firstname = s.firstname;
}
bool operator==(const Student& leftop, const Student& rightop)
// Overloaded SAME AS operator
// Returns true if leftop.sid == rightop.sid. Returns false otherwise
{
return (leftop.sid == rightop.sid);
} // End operator== for Student class
bool operator<(const Student& leftop, const Student& rightop)
// Overloaded LESS THAN operator
// Returns true if leftop.sid < rightop.sid. Returns false otherwise
{
return (leftop.sid < rightop.sid);
} // End operator < for Student class
bool operator>(const Student& leftop, const Student& rightop)
// Overloaded GREATER THAN operator
// Returns true if leftop.sid > rightop.sid. Returns false otherwise
{
return (leftop.sid > rightop.sid);
} // End operator < for Student class
void Student::operator=(const Student& op) // Overloaded ASSIGNMENT operator
// Sets this->sid = op.sid, this->lastname = op.lastname, this->firstname = op.firstname
{
this->sid = op.sid;
this->lastname = op.lastname;
this->firstname = op.firstname;
} // End Student::operator=()

View File

@ -0,0 +1,83 @@
//
// student.h CPE 212 Fall 2010 -- Project05 - Binary Search Tree Template
//
// DO NOT MODIFIY OR SUBMIT THIS FILE
//
#include <iostream>
using namespace std;
#ifndef STUDENT_H
#define STUDENT_H
class Student
{
private:
int sid; // Student ID number
string lastname; // Student last name
string firstname; // Student first name
public:
/**** Start of functions to implement ****/
Student();
// Default constructor initializes sid to -1, firstname and lastname to empty string
Student(int id, string lname, string fname);
// Constructor initializes sid, lastname, firstname to id, lname, and fname respectively
Student(const Student& s);
// Copy constructor -- copies attributes of s into attribute variables of current object
friend bool operator==(const Student& leftop, const Student& rightop); // Overloaded SAME AS operator
// Returns true if leftop.sid == rightop.sid. Returns false otherwise
friend bool operator<(const Student& leftop, const Student& rightop); // Overloaded LESS THAN operator
// Returns true if leftop.sid < rightop.sid. Returns false otherwise
friend bool operator>(const Student& leftop, const Student& rightop); // Overloaded GREATER THAN operator
// Returns true if leftop.sid > rightop.sid. Returns false otherwise
void operator=(const Student& op); // Overloaded ASSIGNMENT operator
// Sets this->sid = op.sid, this->lastname = op.lastname, this->firstname = op.firstname
/***** End of functions to implement *****/
/***** Below are additional functions for your Student class -- DO NOT MOVE OR MODIFY THE CODE BELOW *****/
friend istream& operator>>(istream& leftop, Student& rightop) // Overloaded >> operator
// This allows all data associated with a Student object to be input simultaneously from an input stream
{
leftop >> rightop.sid >> rightop.lastname >> rightop.firstname;
return leftop;
}
friend ostream& operator<<(ostream& leftop, const Student& rightop) // Overloaded << operator
// This allows all data associated with a Student object to be output simultaneously to an output stream
{
leftop << "(" << rightop.sid << ", " << rightop.firstname << " " << rightop.lastname << ")";
return leftop;
}
void Print() const // Outputs student information in desired format. DO NOT MOVE OR MODIFY
{
cout << "SID: " << sid << " Name: ";
if (lastname == "")
cout << "NULL";
else
cout << lastname;
cout << ", ";
if (firstname == "")
cout << "NULL";
else
cout << firstname;
} // End Print()
};
#endif

BIN
CPE212/Project_05/student.o Normal file

Binary file not shown.