Troubles with a recursive function

So here's my recursive function that's supposed to print out an expression tree. The root of the following tree is passed in. I'm pretty sure the tree is created correctly based on some print statements I put earlier in the program. It prints the check three times, then seg faults,(I also tried putting the print statement between the int declaration and the if statement and got the same printout: three checks) so there must be some issue with calling the function when currentPtr is pointing at 5?? That is the first integer it comes to, but all the nodes are of type TreeNode(which just consists of a a string called Op which holds the operator/operand and two TreeNodePointers, leftPtr and rightPtr.
3 *
25 +
12 5

void arithmetic_expression::printExpressionTreeHelper(TreeNodePtr currentPtr, int indent)
int i;
if (currentPtr != NULL)
cout << "check";
printExpressionTreeHelper(currentPtr->rightPtr, indent+5); //doesn't run when calls '5'

cout << "check2";
for (i=1; i <= indent; i++)
cout << " ";
cout << "2";

cout << currentPtr->Op << endl;
printExpressionTreeHelper(currentPtr->leftPtr, indent+5);
else if (currentPtr ==NULL){
cout << "check3";

Have you initialized the rightPtr and leftPtr to null in case they don't point to anything?
Yeah, this is where I created the node.

newNodePtr = new TreeNode;
newNodePtr->Op = expression[i];
newNodePtr->rightPtr = NULL;
newNodePtr->leftPtr = NULL;

Btw, I just realized the spacing on that expression tree failed, but the + and 25 are the right and left pointers of the * and the 5 and 12 are right and left pointers of the +.
Topic archived. No new replies allowed.