Expression Tree

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TRW
    New Member
    • Dec 2007
    • 1

    #1

    Expression Tree

    I have written an expression tree (binary tree) that works so that it gives priority to brackets.

    However I need to add the functionality of BODMAS to the making of the tree. How would I do this so it can solve expressions such as :

    1 + ((4 -2)*3)*3

    I though about putting brackets around the times and divide parts of an expression so that it would do those bits first. Though this isn't the best way as it would not give precedence of divide over times, or precedence of add over subtract (although in a lot of cases this would not have an effect on the result)

    I would like to point out that this is coursework, so the theory behind what I need to do would be brilliant.

    Thanks
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    It's not clear to me what your question is. Is your question: "how do I parse strings into expression trees?"

    Note, by the way, despite the order of letters in "BODMAS", + and - are normally given the same precedence, and * and / are normally given the same precedence, too. Are you certain your assignment gives then differing precedence?

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      In the Java 'howto' section I talk a bit about compilers and a bit about recursive
      descent parsers; they can handle those expressions perfectly well.

      If you can still find it: the book "data structures" by Horrowitz and Sahni (sorry,
      no ISBN, I'm not at my office room) explains a simple non-recursive stack algorithm
      that maps an operator to two different priorities: an in-stack and on-stack priority
      that tells the algorithm when to push/pop operator elements from that stack.

      kind regards,

      Jos

      Comment

      Working...