I am writing a program that will evaluate expressions using binary trees. Most of the code has been provided, I just have to write the code for the class functions as listed in the header file. However, I am really new to recursion and trees...and this program uses public functions and private helper functions, which I am completely lost in. Here is the header file:
If I am not mistaken, the public functions are quite simple, and primarily call the helper functions to do the work, right? I am just kind of lost with the helper functions and the whole concept of recursion. Here are some the functions I already have:
Am I even doing this right? I am so lost. Any help or guidance would be greatly appreciated!
Code:
struct CTNode
{
NodeType type;
int operand;
CTNode *left, *right;
};
class CalcTree
{
public:
CalcTree(); //default constructor
CalcTree( CalcTree& otherTree ); //copy constructor
~CalcTree(); //destructor
void setRoot( NodeType type_, int operand_ );
//construct a node with the given characteristics and place it at the root of this tree
void setLeft( CalcTree& otherTree );
//place a copy of the parameter tree as the left subtree of this tree
void setRight( CalcTree& otherTree );
//place a copy of the parameter tree as the right subtree of this tree
void printIN( );
//member function to print the stored expression in this tree using INFIX notation
void printPOST( );
//member function to print the stored expression in this tree using POSTFIX notation
float evaluate( );
//member function to evaluate the stored expression and return an answer
//assumes that the expression tree is well-formed
//the client must make sure the expression tree is well fromed before calling this function
private:
CTNode *copyHelper( CTNode *thatroot ); //recursive helper for copy constructor and "set" functions
//constructs an exact copy of the tree rooted at "thatroot" and returns a pointer to it
void destHelper( CTNode *thisroot ); //recursive helper for destructor
//completely deallocates all dynamic memory in the tree rooted at "thisroot"
void printINhelper( CTNode *thisroot ); //recursive helper for INFIX printing
//prints the expression tree rooted at "thisroot" in INFIX order
void printPOSThelper( CTNode *thisroot ); //recursive helper for POSTFIX printing
//prints the expression tree rooted at "thisroot" in POSTFIX order
float evalHelper( CTNode *thisroot ); //recursive helper for expression evaluation
//evaluates the expression tree rooted at "thisroot" and returns the answer
//returns a float so that integer division is avoided
CTNode *root; //pointer to the root node of this expression tree
};
#endif
Code:
CalcTree::destHelper(CTNode *thisroot){
if(root==NULL) return;
//if the current root has any subtrees, we must delete them first to prevent memory leak
if(( root->left != NULL ) || ( root->right != NULL ))
{
if( root->left != NULL ) //if there is a left subtree
{destHelper(root->left); //recursively deallocate it
root->left=NULL;} //set its pointer to NULL
if( root->right != NULL ) //if there is a right subtree
{destHelper(root->right); //recursively deallocate it
root->right=NULL;} //set its pointer to NULL
}
//at this point we are sure this root has no subtrees so we can delete it
delete root;
}
Code:
CalcTree::printINhelper(CTNode *thisroot){
//prints the expression tree rooted at "thisroot" in INFIX order
if ( thisroot != NULL ) { // Otherwise, there's nothing to print
printINhelper( thisroot->left ); // Print items in left subtree.
cout << thisroot->operand << " "; // Print the root item.
printINhelper( thisroot->right ); // Print items in right subtree.
}
}
Comment