Software Design
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreSoftware Design

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 22nd, 2004, 03:20 AM
Jaspreet Singh Jaspreet Singh is offline
Getting my *facts* right
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Location: Amritsar, India
Posts: 177 Jaspreet Singh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 17 h 40 m
Reputation Power: 5
Red face algo to build expression tree

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.

Reply With Quote
  #2  
Old April 22nd, 2004, 03:56 PM
dog135's Avatar
dog135 dog135 is offline
Doggie
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jul 2003
Location: Seattle, WA
Posts: 751 dog135 User rank is Corporal (100 - 500 Reputation Level)dog135 User rank is Corporal (100 - 500 Reputation Level)dog135 User rank is Corporal (100 - 500 Reputation Level)dog135 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 10 h 38 m 25 sec
Reputation Power: 7
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

Reply With Quote
  #3  
Old April 23rd, 2004, 04:11 AM
Jaspreet Singh Jaspreet Singh is offline
Getting my *facts* right
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Location: Amritsar, India
Posts: 177 Jaspreet Singh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 17 h 40 m
Reputation Power: 5
Unhappy

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.

Reply With Quote
  #4  
Old April 23rd, 2004, 02:09 PM
dog135's Avatar
dog135 dog135 is offline
Doggie
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jul 2003
Location: Seattle, WA
Posts: 751 dog135 User rank is Corporal (100 - 500 Reputation Level)dog135 User rank is Corporal (100 - 500 Reputation Level)dog135 User rank is Corporal (100 - 500 Reputation Level)dog135 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 10 h 38 m 25 sec
Reputation Power: 7
Quote:
Originally Posted by Jaspreet Singh
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.


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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreSoftware Design > algo to build expression tree


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT