building a binary tree out of postfix notation and printing prefix

There are a couple problems so far I mostly don't understand why I cant make a new node by poping my node stack and trying to put the values into a new node in lines 42 43 any comments would be helpful thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <string>
#include <stack>
#include <conio.h>

using namespace std;

struct node{
    char data;
    node *left;
    node *right;
};

void inOrder(node *tree)
{
    if( tree!=NULL)
	{
        cout << "(";
		inOrder( tree->left);
        cout << tree->data;
        inOrder(tree->right);
		cout << ")";
    }
}



void main()
{

	string in; //USER INPUT
	stack<node> tmp; //temporary stack to hold operands and operators
    cout<<"Enter Postfix Expression :";
    cin>>in;

	int i;
    node *new1,*ptr1,*ptr2; //nodes used to create and connect tree nodes
	for ( i = 0; i < in.length(); i++ )
	{
		if ( in[i] == '+' || in[i] == '-' || in[i] == '*' || in[i] == '/' || in[i] == '^' ) //if an operator pop two nodes from stack and attach
		{
			node ptr1 = tmp.pop();
            node ptr2 = tmp.pop();
            node newl = new node;
            newl->data = in[i];
            newl->left = ptr2;
            newl->right = ptr1;
            tmp.push(newl);
        }
		else			// else then make new node attach and push to stack
        {
            newl = new node;
            newl->data = in[i];
            newl->left = NULL;
            newl->right = NULL;
            tmp.push(newl);
        }
    }
	new1 = tmp.pop();
    cout<<"Output: ";
    inOrder(new1);
    _getch();
}
Topic archived. No new replies allowed.