Results 1 to 5 of 5
  1. #1
    scottmulla is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Lightbulb Prefix to Expression tree

    I am trying to make a basic java program that takes an infix expression as the argument (i.e. "+ * 2 - 7 3 / 20 5") and outputs it as an arithmetic expression tree and evaluates the tree.

    i have the attached classes to work with...am really struggling on this one so any help would be appreciated.

    files.zip

  2. #2
    scottmulla is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default Re: Prefix to Expression tree

    so far i have this

    Java Code:
    public class ArithmeticTreeTest {
    	
    	public static void main(String[] args) throws IOException {
    		BinaryTreeNode tree = null;
    		String preFix = args[0];
    		StringTokenizer st = new StringTokenizer(preFix);	
    
    
    		while (st.hasMoreTokens()){
    			String tokens = st.nextToken();
    			System.out.println(tokens);
    			try{
    				int num = Integer.parseInt(tokens);
    				if (tree.left() == null){
    					tree.setLeft(new BinaryTreeNode(new Integer(num)));
    				} else {
    					tree.setRight(new BinaryTreeNode(new Integer(num)));
    				}
    			} catch (NumberFormatException e){
    				char operator = tokens.charAt(0);
    				tree = new BinaryTreeNode(operator, null, null);
    			}
    		}
    
    		System.out.println();
    		System.out.println("The tree representation is:");
    		System.out.println(tree.toString());
    		System.out.println();
    		System.out.println("The tree evaluates to " + evaluate(tree));
    		System.out.println();
    	}
    	
    	protected static int evaluate(BinaryTreeNode node){
    		if (BinaryTreeNode.size(node)==1) 
    			return ((Integer) node.value()).intValue();
    		else {
    			int left=evaluate(node.left());
    			int right=evaluate(node.right());
    			char c=((Character) node.value()).charValue();
    			switch (c){
    				case '+': return (left+right);
    				case '*': return (left*right);
    				case '-': return (left-right);
    				case '/': return (left/right);
    				case '%': return (left%right);
    				default: { System.out.println("Unknown operator!"); 
    							System.exit(-1); return 0;}
    			}
    		}
    	}	
    	
    	
    }

  3. #3
    david522 is offline Member
    Join Date
    May 2011
    Posts
    21
    Rep Power
    0

    Default Re: Prefix to Expression tree

    Bump, need help with this too.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,373
    Blog Entries
    7
    Rep Power
    25

    Default Re: Prefix to Expression tree

    Maybe you'll find one of my blog articles useful; it babbles about expression compilation and the code can do what you want (and some more).

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  5. #5
    scottmulla is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default Re: Prefix to Expression tree

    Thanks for the help, that's all a bit advanced for me i don't really understand most of it...

Similar Threads

  1. Replies: 2
    Last Post: 07-18-2011, 07:11 AM
  2. Preorder Binary Expression Tree Help
    By gixxer05 in forum New To Java
    Replies: 3
    Last Post: 05-12-2011, 06:33 PM
  3. Create program to evaluate prefix expression
    By Debonairj in forum New To Java
    Replies: 1
    Last Post: 08-12-2010, 02:35 AM
  4. Replies: 0
    Last Post: 04-04-2010, 07:40 AM
  5. Help creating expression tree
    By nellaf in forum New To Java
    Replies: 8
    Last Post: 12-04-2009, 05:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •