Login
Search
Welcome to the iTechForums.
If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.
To register now click here.
Latest Threads
Advertisements
Forum Statistics
Threads:
Posts: 4
Members:
Number of Users Online:
Welcome to our newest member,
Reply
 
Thread Tools Display Modes
  #1  
Old 05-15-2008, 02:36 PM
Daneey?h Daneey?h is offline
Junior Member
 
Join Date: May 2008
Posts: 2
Default

Recursive Method (Java)?


I am asked to write a recursive method named evaluate which computes the value of any non-null expression tree.
I am given the following class:

public class ExpTN {
public String op; // stores "+","-","*","/", or "value"
public int value; // significant only if op is "value"
public ExpTN left, right; // significant only if op ISN'T a value;

Below is generally what I was thinking of doing.
Am I on the right path? Any ideas/suggestions or other ways of writing such a method?

Thank you! =)
--------------
static double evaluate( ExpNode node ) {
if ( node.kind == NUMBER ) {
// The value of a NUMBER node is the # it holds.
return node.number; }
else {
double leftVal = getValue( node.left );
double rightVal = getValue( node.right );
switch ( node.op ) {
case '+': return leftVal + rightVal;
case '-': return leftVal - rightVal;
case '*': return leftVal * rightVal;
case '/': return leftVal / rightVal;
default: return Double.NaN;
} } }
Would the complexity class of that be O(N)?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
The considerations involved in choosing between a recursive and iterative approach... mssrtoothpick Software Design & Algorithms 0 05-13-2008 10:12 PM
how can i update java in my LG KE770 mobile to access bluetooth from java? atul k Java, Java Servlets & JSP 0 05-06-2008 05:00 PM
How agile method works in the software development? Ems Development Software 1 05-06-2008 01:15 PM
i need to write a recursive definition for any valid java identfier.? anonymous Java, Java Servlets & JSP 0 05-06-2008 09:48 AM

Your Ad Here

All times are GMT. The time now is 10:53 PM.