Results 1 to 5 of 5
Thread: Prefix to Expression tree
- 10-14-2011, 07:00 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 10
- Rep Power
- 0
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
- 10-14-2011, 10:12 PM #2
Member
- Join Date
- Mar 2011
- Posts
- 10
- Rep Power
- 0
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;} } } } }
- 10-15-2011, 10:32 AM #3
Member
- Join Date
- May 2011
- Posts
- 21
- Rep Power
- 0
Re: Prefix to Expression tree
Bump, need help with this too.
- 10-15-2011, 11:56 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,605
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-16-2011, 04:52 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
Looking for pseudocode to convert Prefix Expression & Infix Expression to expression
By dragstang86 in forum New To JavaReplies: 2Last Post: 07-18-2011, 07:11 AM -
Preorder Binary Expression Tree Help
By gixxer05 in forum New To JavaReplies: 3Last Post: 05-12-2011, 06:33 PM -
Create program to evaluate prefix expression
By Debonairj in forum New To JavaReplies: 1Last Post: 08-12-2010, 02:35 AM -
Data Structures(Binary Search Tree to AVL Tree)ASAP pls
By jfAdik in forum Forum LobbyReplies: 0Last Post: 04-04-2010, 07:40 AM -
Help creating expression tree
By nellaf in forum New To JavaReplies: 8Last Post: 12-04-2009, 04:17 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks