Parse trees and "("")" 
Author Message
 Parse trees and "("")"

        I'm having a bit of a problem. I've created a parse tree in memory
        like the following.

                             *
                           /   \
                          !     c
                           \
                            +
                          /   \
                         a     b

        When I traverse the tree in a inorder traversal I get !a+b*c,
        which is correct. The problem is I have to restore the parentheses
        so that I get !(a+b)*c. How can I do this when I and doing
        the traversal recursively. For example,

        void inorder( btree t)
        {
              if( t==NULL )
                 return;
               inorder( t->l);
               putchar(t->data);
               inorder(t->r);
        }

        The use of stacks or queues may be possible I also have functions
        to determine node height and operator presidence. I think I've
        run into a programmers mental block and am in need of some
        assistance.

--
                           Can anyone help                          
                                                Los.

_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
/ Carlos A. Iglesias       / Florida Atlantic University                      //

/ Computer Science Major   / Life is full of BS! Please, I don't need any more./
/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_//



Fri, 10 Jan 1997 22:06:53 GMT  
 Parse trees and "("")"


Quote:

>    When I traverse the tree in a inorder traversal I get !a+b*c,
>    which is correct. The problem is I have to restore the parentheses
>    so that I get !(a+b)*c. How can I do this when I and doing
>    the traversal recursively. For example,
Real simply:
>    void inorder( btree t)
>        {
>          if( t==NULL )
>             return;
                putchar('(');
>               inorder( t->l);
>           putchar(t->data);
>           inorder(t->r);
                putchar(')');
>    }

That will give you excess parens, (or at least more than you need), but it
should work.
--



Sat, 11 Jan 1997 04:22:37 GMT  
 Parse trees and "("")"

|       I'm having a bit of a problem. I've created a parse tree in memory
|       like the following.
|
|
|                            *
|                           / \
|                          !   c
|                           \
|                           +
|                          /  \
|                         a    b
|
|       When I traverse the tree in a inorder traversal I get !a+b*c,
|       which is correct. The problem is I have to restore the parentheses
|       so that I get !(a+b)*c. How can I do this when I and doing
|       the traversal recursively. For example, [ deleted ... ]

Define a precedence relation for all the binary and monary
operators, as well as the operands (constants and identifiers):
something like:

        prec oper
        ----+----
        5   |NULL
        4   |const, ident
        3   |!
        2   |*, /
        1   |+, -

The (recursive) inorder traversal looks something like this:

        void in_trav(node_t* t, int par) {

        if (t) {
                if (par) putchar('(');
                in_trav(t->left, prec(t->left, t));
                printf("%c", t->oper);
                in_trac(t->right, prec(t->right, t));
                if (par) putchar(')');
        }

        }

The function prec(x, y) returns a non-zero value iff
prec(x->oper) < prec(y->oper) (including the special case where
x or y equals NULL, in which case prec() returns any value.)

The top level call of in_trav() passes a zero value as the
second argument, i.e. the entire expression need not be
parenthesized ...

kind regards,


----------------------------------------------------------------------------
Two things are omnipresent in the universe: hydrogen and my stupidity



Sat, 11 Jan 1997 17:51:22 GMT  
 Parse trees and "("")"
        Hey! I'm finally able to post. Turns out my NNTP
        server was full. I want to thank everyone who
        responded to me e-mail. I used something like
        this in my final solution.

        void inorder( btree t)
        {
          if(t->l){
                flag = need_parents( t->data, t->l->data )
                if( flag )
                        putchar('(');
                inorder(t->l);
                if( flag )      
                        putchar(')');

          }
          putchar(t->data);
          if(t->r){
                flag = need_parents( t->data, t->r->data )
                if( flag )
                        putchar('(');
                inorder(t->r);
                if( flag )
                        putchar(')');
          }            
        }

        This was the code originally give to me by Olaf Weber
        If you see this function calls another function called
        need_parents( ) which determines the presidence of
        the operators and operands and returns a flag used
        to determine the need to include parentheses.

                                Thanks again everyone.

                                                Los.

_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
/ Carlos A. Iglesias     / Florida Atlantic University                      //

/ Computer Science Major / Life is full of BS! Please, I don't need any more./
/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_//



Tue, 14 Jan 1997 05:59:32 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. remove() vrs fopen("""w")

2. Displaying binary data as ascii "1"'s and "0"'s

3. Looking for "Shroud"/"Obfus"

4. ""help with TSR""

5. Error "free"-ing "malloc"-ed memory

6. Displaying binary data as ascii "1"'s and "0"'s

7. Attention "C"/"C++" gurus and "C" newbees

8. merits of "#define", "const", and "enum" ???

9. why not "cout", "<<" , and "endl"??

10. parsing "path" from arvg[1]

11. parsing error: expected ")".

12. "Parsing the Commandline"-question

 

 
Powered by phpBB® Forum Software