|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
hi everybody,
i want to build an expression tree from a mathematical expression involving binary operations. Can anybody suggest some idea for how to go about it. i don't want to use stack in the program(a queue may be used). eg: if a give an expression like (a+b)*(c-d) * / \ + - / \ / \ a b c d
__________________
IMHO
Last edited by Jaspreet Singh : April 22nd, 2004 at 03:25 AM. |
|
#2
|
||||
|
||||
|
We just had a topic on this not too long ago. I gave some sample code in my reply which you can probably use to make your tree.
Basically, make a recursive function. Everytime you hit a "(", then the function calls itself. It exits when it reaches the next ")". This is basically reverse polish notation. Best done with a stack.
__________________
"Science is constructed of facts as a house is of stones. But a collection of facts is no more a science than a heap of stones is a house." - Henri Poincare |
|
#3
|
|||
|
|||
|
Actually i'm supposed to submit an assignment. The question is:
build an expression tree without using a stack(LIFO) (you may use a queue(FIFO)). i've seen ur code that was posted earlier. it converts the expression into post fix using recursion( recursion is implemented internally thru stack). Is there a way we can do it using queue. |
|
#4
|
||||
|
||||
|
Quote:
Nope. Completely impossible. ![]() Ok, I'll help out a little. Not using a stack makes this a lot harder. That means you'll have to make several passes through the equation. Remember, FIFO is just the oposite of a FILO. Reverse the order of your operations. This means you'll have to store the values outside of the parentheses before you drill into them. Are you still allowed to use recursion? If not, you can still simulate it by adding a "level" variable that keeps track of how many levels deap you are in the equation. (ie: ((1+2)*3)-4, "1+2" is level 2, "*3" is level 1, etc.) That's all the help I'll give for an assignment. You need to write the code. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Software Design > algo to build expression tree |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|