In destHelper:
should check thisroot instead of root.Code:if(root==NULL) return;
In general, when you have a segfault on a line with a pointer on it, check to see if you're somehow dereferencing a NULL pointer.
Printable View
In destHelper:
should check thisroot instead of root.Code:if(root==NULL) return;
In general, when you have a segfault on a line with a pointer on it, check to see if you're somehow dereferencing a NULL pointer.
yeah i changed that, it's still not working tho. I dont know what else to do with this code
.......Sorry to be such a pain but can u guys show me what your talking about, im just trying to get this code debugged before it's due tuesday . i mean overall the code looks mostly done correct? right?
Your SetRoot needs some work (I don't know what your copy looks like). You need to (1) set up temp the way the root should look, and then (2) set root to equal temp. Nothing else sticks out on first glance; that's why we test.
I changed the set root, and then it pointed to the desthelper....i dunno why?
You have three issues that have come up in this thread: (1) setroot using root after it's freed; (2) desthelper checking root==NULL instead of thisroot; and (3) copyHelper not setting the type of the new node.
I will tell you that when I made these three changes in your posted code, I got a working program. Reread everything (I know that the threading is confusing to me, but everything's in there somewhere) and double check you've made those fixes.
when u say working, did it actually calculate the numbers?
and when u say copyhelper not setting the type.
cause i fixed the thisroot and setroot
but im not sure what u mean by copyhelper
Like I seriously am not sure what type even is, so I should set operand to null.....or?
like i hate to ask can u just show me what u mean?
I haven't looked at the code at all [more than find what I think is the correct data structure], but I guess what iMalc is talking about is this type.Code:struct CTNode
{
NodeType type;
int operand;
CTNode *left, *right;
};
--
Mats
here is what i have so far, i believe I fixed the set root. im not getting any more errors when i run it, but it still doesnt do the equation
im pleading, I seriously dont know what u meant by the type....like add? or operand. if anyone could just show me the line, or at least explain their way to it, since apparently the program works with just a line of code misisng,?Code://Matt Smith
//Cs 215 Section 2
//Program 5
#include "CalcTree.h"
CalcTree::CalcTree()
{
root = NULL;
}
CalcTree::~CalcTree()
{
destHelper(root);
}
CalcTree::CalcTree(CalcTree& otherTree) //copy constructor
{
root = copyHelper(otherTree.root);
}
CTNode* CalcTree::copyHelper(CTNode *thatroot)
{
if(thatroot==NULL) return NULL;
CTNode *pwned;
pwned = new CTNode;
pwned->operand = thatroot->operand;
pwned->left=NULL; pwned->right=NULL;
if(( thatroot->left != NULL ) || ( thatroot->right != NULL ))
{
if( thatroot->left != NULL )
pwned->left = copyHelper(thatroot->left);
if( thatroot->right != NULL )
pwned->right = copyHelper(thatroot->right);
}
return pwned;
}
void CalcTree::destHelper( CTNode *thisroot )
{
if(thisroot==NULL) return;
else if(( thisroot->left != NULL ) || ( thisroot->right != NULL ))
{
if( thisroot->left != NULL )
{destHelper(thisroot->left);
thisroot->left=NULL;}
if( thisroot->right != NULL )
{destHelper(thisroot->right);
thisroot->right=NULL;}
}
delete thisroot;
}
void CalcTree::setRoot(NodeType type_, int operand_)
{
destHelper (root);
CTNode *temp = NULL;
temp= new CTNode;
temp-> type=type_;
temp-> operand=operand_;
temp->left=NULL;
temp->right=NULL;
root=temp;
delete temp;
}
void CalcTree::setLeft(CalcTree &otherTree)
{
// delete the old branch first
destHelper(root->left);
// copy the new tree as left branch
root->left= copyHelper(otherTree.root);
}
void CalcTree::setRight(CalcTree &otherTree)
{
// delete the old branch first
destHelper(root->right);
// copy the new tree as left branch
root->right= copyHelper(otherTree.root);
}
void CalcTree::printIN()
{
printINhelper(root);
}
void CalcTree::printINhelper(CTNode* thisroot)
{
if (thisroot == NULL)
return;
else if (thisroot->type == OPR)
cout << thisroot->operand;
else {
cout << "( ";
printINhelper (thisroot->left);
switch (thisroot->type){
case ADD: cout << " + ";
break;
case SUB: cout << " - ";
break;
case MUT: cout << " * ";
break;
case DIV: cout << " / ";
break;
}
printINhelper (thisroot->right);
cout << " )";
}
}
void CalcTree::printPOST( )
{
cout << "(";
printPOSThelper(root);
cout << ")";
}
void CalcTree::printPOSThelper(CTNode *thisroot){
if (thisroot->type == OPR)
cout << thisroot->operand;
else {
printPOSThelper (thisroot->left);
printPOSThelper (thisroot->right);
switch (thisroot->type){
case ADD: cout << " + ";
break;
case SUB: cout << " - ";
break;
case MUT: cout << " * ";
break;
case DIV: cout << " / ";
break;
}
}
}
float CalcTree::evaluate( ){
return evalHelper(root);
}
float CalcTree::evalHelper( CTNode *thisroot ){
if (thisroot == NULL)
return 0;
if (thisroot->type == OPR)
return (float (thisroot->operand));
else {
switch (thisroot->type){
case ADD: return (float (evalHelper(thisroot->left) + evalHelper(thisroot->right)));
case SUB: return (float (evalHelper(thisroot->left)- evalHelper(thisroot->right)));
case MUT: return (float (evalHelper(thisroot->left) * evalHelper(thisroot->right)));
case DIV:return (float (evalHelper(thisroot->left)/ evalHelper(thisroot->right)));
default: return 0;
}
}
}
Four variables.Code:struct CTNode
{
NodeType type;
int operand;
CTNode *left, *right;
};
Three get set.Code:CTNode* CalcTree::copyHelper(CTNode *thatroot)
{
if(thatroot==NULL) return NULL;
CTNode *pwned;
pwned = new CTNode;
pwned->operand = thatroot->operand;
pwned->left=NULL; pwned->right=NULL;
You need to copy type when you copy the node.
oh so it would be pwned->type= thatroot->type;?