InOrder traversal : show tree

This is a discussion on InOrder traversal : show tree within the C Programming forums, part of the General Programming Boards category; I am needing confirmation of the following: Show the expression tree whose InOrder traversal is: (5*2+(3*4-7))/(3+4/2) Code: '/' / \ ...

  1. #1
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157

    InOrder traversal : show tree

    I am needing confirmation of the following:

    Show the expression tree whose InOrder traversal is:

    (5*2+(3*4-7))/(3+4/2)

    Code:
                                        '/'
                                      /    \
                                     /      \
                                    /        \
                                   /          \
                                  /            \
                                '+'            '-' 
                                /\              /\
                               /  \            /  \
                              /    \          /    \
                             /      \        /      \  
                            '*'   '-'        '+'     2     
                            /\      /\       /\ 
                           /  \    /  \     /  \ 
                          /    \  /    \   /    \ 
                         5    2  '*'   7   3    4
                                 / \
                               3   4
    Sue B.

    dazed and confused


  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Your tree to the right of the root is wrong... that's the tree for
    3 + 4 - 2
    but your equation you need to make the right subtree for is...
    3 + 4 / 2
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157
    okay, 'cept for the keying error on my part on the one node;
    this is a correct representation of the expression I gave as :

    (5*2+(3*4-7))/(3+4/2) ???

    I wasn't sure about the parts (3*4-7) and (3+4/2); I take it that
    * and / are evaluated as the parent node over + and - . Is that right???

    Now the next evaluation is to take the following expression tree and give the postfix expression using PostOrder traveral.

    Code:
                                        '/'
                                      /    \
                                     /      \
                                    /        \
                                   /          \
                                  /            \
                                '+'            '/' 
                                /\              /\
                               /  \            /  \
                              /    \          /    \
                             /      \        /      \  
                            '*'   '-'        '+'     2     
                            /\      /\       /\ 
                           /  \    /  \     /  \ 
                          /    \  /    \   /    \ 
                         5    2  '*'   7   3    4
                                 / \
                               3   4
    postfix expression of above tree:

    5 2 * 3 4 * 7 - + 3 4 + 2 / /

    Is this right??
    Last edited by sballew; 12-17-2001 at 07:51 PM.
    Sue B.

    dazed and confused


  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Your postfix answer is correct, but your inffix tree is still incorrect.

    The tree you have now is for...
    (3 + 4) / 2
    The 3 + 4 is being evaluated, and it's result is divided by the 2. Because / has higher precedence than +, you need to add 3 to the division of 4 / 2...
    Callou collei we'll code the way
    Of prime numbers and pings!

  5. #5
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157
    buggers....
    I think this is confusing sometimes.

    okay so the correct tree expression of
    (5*2+(3*4-7))/(3+4/2)
    is:

    Code:
                                        '/'
                                      /    \
                                     /      \
                                    /        \
                                   /          \
                                  /            \
                                '+'            '+' 
                                /\              /\
                               /  \            /  \
                              /    \          /    \
                             /      \        /      \  
                            '*'   '-'        3     '/'     
                            /\      /\             / \
                           /  \    /  \           /   \
                          /    \  /    \         /     \
                         5    2  '*'   7        4      2
                                 / \
                               3   4
    Sue B.

    dazed and confused


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  2. Replies: 5
    Last Post: 11-17-2007, 02:47 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21