How to create BST for arithmetic expression

Hi guys, I am a c++ leaner, I am trying to create a BST tree for this expression: 2346*+/8+, and do inorder and postorder to get the in-fix version, and postfix version of the expression. I am having difficulty to create the binary tree for the expression. Here is my peso code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Tree:
Stack:
inorder fn{}
postorder fn{}
main{
  input the file;
  while(expression){
    if(digit){
       s.push}
    else if(operator){
       s.top = right;
       s.pop;
       s.top = left;
       s.pop;
       T1 = new Tree(operator, left, right);
}
}
 


the tree I want to create is like this
1
2
3
4
5
6
7
8
9
10
               +
              / \
            (/)  8
            / \
           +   2  
          / \
         *   3
        / \
       4   6
 

my problem for this code is that after create (4*6) tree, I cant link (+3) with (4*6). Please help me.
In the pseudo code, I assume that line 11 means "assign s.top to right." In C++ that would be right = s.top, not s.top = right.

After line 15, you need to push T1 onto the stack.
Topic archived. No new replies allowed.