can not use copyHelper, buildHelper function and so on I just needed a .cpp file
ID: 3725903 • Letter: C
Question
can not use copyHelper, buildHelper function and so on
I just needed a .cpp file for these function please
// Laboratory 8 ExpressionTree.h
//
// Class declarations for the linked implementation of the
// Expression Tree ADT -- including the recursive helpers for the
// public member functions
//
// Instructor copy with the recursive helper function declarations.
// The student version does not have those, but has a place to write
// the declarations in the private section.
//
//--------------------------------------------------------------------
#ifndef EXPRESSIONTREE_H
#define EXPRESSIONTREE_H
#include <stdexcept>
#include <iostream>
using namespace std;
template <typename DataType>
class ExprTree {
public:
// Constructor
ExprTree ();
ExprTree(const ExprTree& source);
ExprTree& operator=(const ExprTree& source);
// Destructor
~ExprTree ();
// Expression tree manipulation operations
void build ();
void expression () const;
DataType evaluate() const throw (logic_error);
void clear (); // Clear tree
void commute();
bool isEquivalent(const ExprTree& source) const;
// Output the tree structure -- used in testing/debugging
void showStructure () const;
private:
class ExprTreeNode {
public:
// Constructor
ExprTreeNode ( char elem,
ExprTreeNode *leftPtr, ExprTreeNode *rightPtr );
// Data members
char dataItem; // Expression tree data item
ExprTreeNode *left, // Pointer to the left child
*right; // Pointer to the right child
};
// Recursive helper functions for the public member functions -- insert
// prototypes of these functions here.
// Data member
ExprTreeNode *root; // Pointer to the root node
};
#endif // #ifndef EXPRESSIONTREE_H
Explanation / Answer
ExpressionTree.cpp
#include "ExpressionTree.h"
/**
*
* Sets both left and right pointer to parameter passed, also sets data to parameter passed
*
* @param Data item, left pointer, right pointer
* @return None, constructor
* @pre None, constructor
* @post Node will be initialized and ready for use
**/
template <typename DataType>
ExprTree<DataType>::ExprTreeNode::ExprTreeNode(char newDataItem, ExprTreeNode* leftPtr, ExprTreeNode* rightPtr) {
dataItem = newDataItem;
left = leftPtr;
right = rightPtr;
}
/**
*
* @param None, default constructor
* @return None, constructor
* @pre None, constructor
* @post Expression tree will be ready to be built
**/
template <typename DataType>
ExprTree<DataType>::ExprTree() : root(NULL) {
}
/**
*
* @param Other tree
* @return None, constructor
* @pre Other must be initialized
* @post This will contain all data within other
**/
template <typename DataType>
ExprTree<DataType>::ExprTree(const ExprTree<DataType>& other) : root(NULL) {
(*this) = other;
}
/**
*
* @param Other tree
* @return this, allows for a = b = c
* @pre Other must be initialized
* @post This will hold the data of other
**/
template <typename DataType>
ExprTree<DataType>& ExprTree<DataType>::operator=(const ExprTree<DataType>& other) {
if(this == &other) return (*this);
clear();
assignementHelper(root, other.root);
return (*this);
}
/**
*
* @param None
* @return None
* @pre Tree must have been initialized
* @post Memory will be deallocated
**/
template <typename DataType>
ExprTree<DataType>::~ExprTree() {
clear();
}
/**
*
* @param None
* @return None, void function
* @pre Tree must be initialized
* @post Tree will be populated by input values
**/
template <typename DataType>
void ExprTree<DataType>::build() {
buildHelper(root);
}
/**
*
* Checks if it is a single digit and outputs it; if not, prints a string created from expressionHelper()
*
* @param None
* @return None, void function
* @pre Tree must be initialized
* @post Constant, no change
**/
template <typename DataType>
void ExprTree<DataType>::expression() const {
if(isEmpty()) return;
if(isDigit(root->dataItem)) {
std::cout << root->dataItem;
return;
}
std::cout << expressionHelper(root);
}
/**
* rief Calls evaluateHelper with root to find the value of the tree
*
* @param None
* @return Value of the tree
* @pre Tree must be initialized
* @post Constant, no change
**/
template <typename DataType>
DataType ExprTree<DataType>::evaluate() const throw(logic_error) {
if(isEmpty()) throw(logic_error("evaluate() empty tree"));
return evaluateHelper(root);
}
/**
* rief Calls clearHelper, sets root back to NULL
*
* @param None
* @return None, void function
* @pre Tree must be initialized
* @post Tree will be empty
**/
template <typename DataType>
void ExprTree<DataType>::clear() {
if(isEmpty()) return;
clearHelper(root);
root = NULL;
}
/**
* rief Calls commuteHelper with root
*
* @param None
* @return None, void function
* @pre Tree must be initialized
* @post Tree will be commuted
**/
template <typename DataType>
void ExprTree<DataType>::commute() {
if(isEmpty()) return;
if(isDigit(root->dataItem)) return;
commuteHelper(root);
}
/**
* rief Checks if normally equivalent, if not checks if commuted and equivalent
*
* @param Other tree
* @return True if equiv, false if not
* @pre Both trees must be initialized
* @post Constant, no change
**/
template <typename DataType>
bool ExprTree<DataType>::isEquivalent(const ExprTree& source) const {
bool result;
if(root == NULL && source.root == NULL) return true;
if((root == NULL && source.root != NULL) || (root != NULL && source.root == NULL)) return false;
if((root->dataItem == source.root->dataItem) && isDigit(root->dataItem)) return true;
result = isEquivHelper(root, source.root);
if(!result) result = isCommuteHelper(root, source.root);
return result;
}
template <typename DataType>
void ExprTree<DataType>::showStructure() const {
if (isEmpty()) cout << "Empty tree" << endl;
else {
cout << endl;
showHelper(root,1);
cout << endl;
}
}
/**
*
* @param None
* @return True if empty, false if not
* @pre Tree must be initialized
* @post Constant, no change
**/
template <typename DataType>
bool ExprTree<DataType>::isEmpty() const {
if(root == NULL) return true;
else return false;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.