1

I need help with adding data to a tree. For example if i have 7+8*9-18/(1+2), how am I supposed to add it to a binary tree in a way that i can compute the result using a binary tree. I am beginner in learning tree structures so I am not very familiar with it.

Transcendental
  • 983
  • 2
  • 11
  • 27
  • 1
    I don't see why u added homework tag. I just made an example. If i want to learn something new it doesn't mean that it is my homework. I didn't ask for you to do this, I just wanted an explanation or a waypoint. – Transcendental Mar 26 '12 at 18:20

2 Answers2

1

After converting the post fix expression to infix, follow the below steps to construct a tree.

  1. If it is number, add it to add to the stack.

  2. If is is an operator, make the operator as parent node, pop the element and make it as right child to the parent node, pop the element and make it as left child to the parent node and add the parent node to the stack.

user1229441
  • 344
  • 4
  • 11
0

See How to write an evaluator for a string like "(1+3 * ( 5 / 4)) and get a numeric result and the Shunting Yard Algorithm

Community
  • 1
  • 1
AShelly
  • 34,686
  • 15
  • 91
  • 152