Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package hw3.q04;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.LinkedList;
- import java.util.List;
- public class ExpressionTree
- {
- public ExpressionTreeNode root;
- public boolean isanumber(ExpressionTreeNode node)
- {
- if((node.data.equals("0")) || (node.data.equals("1")) || (node.data.equals("2"))|| (node.data.equals("3"))||(node.data.equals("4"))||(node.data.equals("5"))||(node.data.equals("6"))||(node.data.equals("7"))||(node.data.equals("8"))||(node.data.equals("9")))
- {
- return true;
- }
- return false;
- }
- public List<String> tostringlist(String prefixExpression) // returns a list of SINGLE strings given a string.
- {
- List<String> listofstrings = new LinkedList<String>();
- for (int i = 0; i < prefixExpression.length(); i++) {
- char charat = prefixExpression.charAt(i);
- String stringat = String.valueOf(charat);
- if(!stringat.equals(" "))
- {
- listofstrings.add(stringat);
- }
- }
- return listofstrings;
- }
- public List<String> removefirst(List<String> prefixExpression) // returns a list of SINGLE strings given a string.
- {
- List<String> listofstrings = new LinkedList<String>();
- for (int i = 1; i < prefixExpression.size(); i++) {
- String string = prefixExpression.get(i);
- listofstrings.add(string);
- }
- return listofstrings;
- }
- public void addtothisnode(ExpressionTreeNode thenodetoputin) // only adds to the root
- {
- if(root.left == null)
- {
- root.left = thenodetoputin;
- }
- if(root.right == null)
- {
- root.right = thenodetoputin;
- }
- }
- public ExpressionTree(String prefixExpression)
- {
- List<String> listofstrings = tostringlist(prefixExpression);
- for (String currentstring : listofstrings) {
- insert( root, currentstring);
- }
- //createExpressionTree(List<String> listofstrings, ExpressionTreeNode parentroot)
- // root = createExpressionTree(listofstrings, null);
- // ExpressionTreeNode newnode = new ExpressionTreeNode(listofstrings.get(0));
- // root = newnode;
- // listofstrings = removefirst(listofstrings);
- // ExpressionTreeNode newnodel = new ExpressionTreeNode(listofstrings.get(0));
- // ExpressionTreeNode newnoder = new ExpressionTreeNode(listofstrings.get(1));
- //
- // ////
- // root.left = createExpressionTree(listofstrings, root);
- // root.right = createExpressionTree(listofstrings, root);
- //
- //
- //
- // if(newnodel.isanumber(newnodel))
- // {
- // listofstrings = removefirst(listofstrings);
- // root.left = newnodel;
- // }
- //
- // if(!newnodel.isanumber(newnodel))
- // {
- // root.left = newnodel;
- // //????
- // listofstrings = removefirst(listofstrings);
- // root.left.left = createExpressionTree(listofstrings, root.left);
- // root.right.right = createExpressionTree(listofstrings, root.left);
- // }
- //
- //
- //
- // if(newnoder.isanumber(newnoder))
- // {
- // root.right = newnoder;
- // }
- //
- //
- // if(!newnoder.isanumber(newnoder))
- // { root.right = newnoder;
- // //????
- // listofstrings = removefirst(listofstrings);
- // root.left = createExpressionTree(listofstrings, root.right);
- // root.right = createExpressionTree(listofstrings, root.right);
- // }
- //
- //
- }
- //for each STRING in STRINGLIST, run this code ... potentially :(
- public ExpressionTreeNode lastInsertedNode = new ExpressionTreeNode();
- public boolean isLastTokenOperator = false;
- public void insert(ExpressionTreeNode rootnode, String token) //test + 2 + 1 1
- {
- ExpressionTreeNode newnode = new ExpressionTreeNode(token);
- if(root == null)
- {
- root = newnode;
- }
- if(isLastTokenOperator) // case 1?
- {
- //insert into the last inserted left child
- rootnode.left = newnode;
- }
- else // case 2
- {
- //backtrack: get node with null right child
- rootnode.right = newnode;
- //insert
- }
- //maintain
- lastInsertedNode = newnode;
- if((token.equals("+")) || (token.equals("-")) || (token.equals("*")) || (token.equals("/")))
- {
- isLastTokenOperator = true;
- }
- else
- {
- isLastTokenOperator = false;
- }
- }
- private ExpressionTreeNode createExpressionTree(List<String> listofstrings, ExpressionTreeNode parentroot)
- {
- ExpressionTreeNode newnode = new ExpressionTreeNode(listofstrings.get(0));
- listofstrings = removefirst(listofstrings);
- if(parentroot == null)
- {
- root = newnode;
- parentroot = root;
- createExpressionTree(listofstrings, parentroot);
- }
- if(newnode.isanumber(newnode))
- {
- parentroot.left = newnode;
- }
- else
- {
- parentroot.right = newnode;
- }
- // ExpressionTreeNode newnoder = new ExpressionTreeNode(listofstrings.get(1));
- //
- // if(newnodel.isanumber(newnodel))
- // {
- // listofstrings = removefirst(listofstrings);
- // parentroot.left = newnodel;
- // }
- //
- //
- // if(!newnodel.isanumber(newnodel))
- // {
- // listofstrings = removefirst(listofstrings);
- // parentroot.left = newnodel;
- // parentroot.left = createExpressionTree(listofstrings, parentroot.left);
- // parentroot.right = createExpressionTree(listofstrings, parentroot.left);
- // }
- //
- //
- // //// RIGHT SIDE ///
- // if(newnoder.isanumber(newnoder))
- // {
- // listofstrings = removefirst(listofstrings);
- // parentroot.right = newnoder;
- // }
- //
- // if(!newnoder.isanumber(newnoder))
- // {
- // listofstrings = removefirst(listofstrings);
- // parentroot.right = newnoder;
- // parentroot.left = createExpressionTree(listofstrings, parentroot.right);
- // parentroot.right = createExpressionTree(listofstrings, parentroot.right);
- // }
- //
- return newnode;
- }
- //
- // public String getExpressionAsInfix()
- // {
- //
- // }
- //
- // public String getExpressionAsPostfix()
- // {
- //
- // }
- //
- // public String getExpressionAsPrefix()
- // {
- //
- // }
- //
- // public String getStructure()
- // {
- //
- // }
- //
- // public float getValue()
- // {
- //
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement