We just started a unit on trees.. and I'm really confused right now.
For this function, I am suppose to build a BXT tree from a user inputed postfix expression. For example, "34*+23-23"
Even though the function compiles without errors, it crashes on me.
Anyways, here's the code:
Code:
void buildBXT(treetype &root)
// pre - root is NULL
// post- root points to the root node of a BXT
/* This algorithm builds an expression tree from a postfix expression.
The algorithm uses a temporary stack of pointers as it parses the
entered postfix expression.
--Going from left to right, get the next character.
--Create a new node for the character.
If it is an operand, make it a leaf node.
If it is an operator, pop a pointer from the stack and set
the new node's right child to it. Pop another pointer
from the stack and set the new node's left child to it.
--Push the pointer to the new node on the stack.
At the end, the pointer to the root of the BXT will be the only
remaining element on the stack.
*/
{
apstack<node *> b; //creates stack of pointers
apstring line; //user inputed line
char a;
node * r;
node * temp;
cout<<"Enter the postfix form"<<endl;
cin>>line;
cout<<"LINE: " <<line<<endl;
for(int i=0;i<line.length();i++)
{
a=line[i];
node *right;
node *left;
bool o = operand(a);
if(o)
{
temp=(a,NULL,NULL);
}
else
{
{
if(!b.isEmpty())
{
b.pop(r);
right = (r, NULL, NULL);
}
else
{
right = NULL;
}
}
{
if(!b.isEmpty())
{
b.pop(r);
left = (r, NULL,NULL);
}
else
{
left = NULL;
}
}
temp=(line[i], left, right);
}
b.push(temp);
}
}
Thanks for the help.