Results 1 to 9 of 9
  1. #1
    nellaf is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    Default Help creating expression tree

    Hi

    I need a expression tree class that conver infix expressions to postfix and prefix, and i already have this classes:

    Java Code:
    public class Stack<E>{
    
         private int d = -1;
         private int MAX = 100;
         private int size = 0;
         private E[] stack;
         
         @SuppressWarnings("unchecked")
         public Stack(int size){
              stack = (E[])new Object[size];
              this.size = size;
         }
         
         public Stack(){
              this(MAX);
         }
         
         public void push(E o) throws OverflowStackException{
              if (size == (d+1))
                   throw new OverflowStackException();
              
              d++;
              stack[d] = o;
         }
         
         public E top() throws EmptyException{
              if(d == -1)
                   throw new EmptyException();
              return stack[d];
         }
         
         public E pop() throws EmptyException{
              if(d == -1)
                   throw new EmptyException();
              d--;
              return stack[d+1];
         }
         
         public int size(){
              return d+1;
         }
         
         public boolean empty(){
              return d == -1;
         }
         
    }

    Java Code:
    public class BTree<E>{
         
         E element;
         BTree<E> dir;
         BTree<E> esq;
        
         public BTree(){
              this(null);
         }
         
         public BTree(E x){
              element = x;
              esq = null;
              dir = null;
         }
         
         public BTree(E x, BTree<E> d,  BTree<E> e){
              element = x;
              dir = d;
              esq = e;
         }
         
         public E element() throws InvalidNode{
              if(this == null)
                   throw new InvalidNode("Null node");
              return element;
         }
         
         public void setElement(E x){
              element = x;
         }
         
         public void setDir(BTree<E> d){
              dir = d;
         }
         
         public void setEsq(BTree<E> e){
              esq = e;
         }
         
         public BTree<E> getDir(){
              return dir;
         }
         
         public BTree<E> getEsq(){
              return esq;
         }
    
    }
    and now i need that class desesperatly i didnīt get anything yet and iīm getting out of time, someone please help me??
    I need to use the String Tokenizer and is all i can use from java.util.


    Thanks in advance

  2. #2
    nellaf is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    Default

    Somebody can help me??

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,001
    Blog Entries
    7
    Rep Power
    20

    Default

    If your BTree represents an expression the following simple methods will print the prefix and postfix form of the expression:

    Java Code:
    public void prefix(BTree expression) {
       if (expression != null) {
          System.out.print(expression.element); // prefix
          prefix(expression.getDir()); // assuming 'dir' is 'left'
          prefix(expression.getEsq()); // assuming 'esq' is 'right'
       }
    }

    ... and postfix:
    Java Code:
    public void postfix(BTree expression) {
       if (expression != null) {
          postfix(expression.getDir()); // assuming 'dir' is 'left'
          postfix(expression.getEsq()); // assuming 'esq' is 'right'
          System.out.print(expression.element); // postfix
       }
    }
    btw, your element() method is incorrect because 'this' can never be null.

    kind regards,

    Jos
    Last edited by JosAH; 12-03-2009 at 06:33 PM.

  4. #4
    nellaf is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    If your BTree represents an expression the following simple methods will print the prefix and postfix form of the expression:

    Java Code:
    public void prefix(BTree expression) {
       if (expression != null) {
          System.out.print(expression.element); // prefix
          prefix(expression.getDir()); // assuming 'dir' is 'left'
          prefix(expression.getEsq()); // assuming 'esq' is 'right'
       }
    }

    ... and postfix:
    Java Code:
    public void postfix(BTree expression) {
       if (expression != null) {
          prefix(expression.getDir()); // assuming 'dir' is 'left'
          prefix(expression.getEsq()); // assuming 'esq' is 'right'
          System.out.print(expression.element); // postfix
       }
    }
    btw, your element() method is incorrect because 'this' can never be null.

    kind regards,

    Jos

    The BTree is the binary tree so i need a full class that do all the process to convert to posfix and to prefix. I already have similar methods like that, but i think that i need a more complex one to work.
    The expression tree class must work with stacks like the code posted above.

    Thanks

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,001
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by nellaf View Post
    The BTree is the binary tree so i need a full class that do all the process to convert to posfix and to prefix. I already have similar methods like that, but i think that i need a more complex one to work.
    The expression tree class must work with stacks like the code posted above.
    You do understand that infix, prefix and postfix are just traversals of one and the same (expression) tree? You don't need more than the methods I posted and you don't need explicit stacks because the recursive nature of my methods takes care of that all.

    kind regards,

    Jos

  6. #6
    nellaf is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    Default

    Yes, but what was been requested is to use that 2 classes to use on a new one (expression tree), i will send you a pm with a code that my friend do.

  7. #7
    nellaf is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    Default

    Since i canīt send pm to you i will post here:

    deleted

    but i need something diferent and more simple if possible for me.
    If you can do this, i will appreciate very much.

    Best regards
    Last edited by nellaf; 12-03-2009 at 07:50 PM.

  8. #8
    jeevesgreen is offline Member
    Join Date
    Dec 2009
    Posts
    2
    Rep Power
    0

    Default

    I am also a student looking for help with the ExpressionTree.java. Just wondering if You finished the code yet. This is what I have so far but I keep having trouble with my "instanceof" method.

    Can Anyone Help?




    import java.util.*;
    public class ExpressionTree {


    BTNode<String> head = new BTNode<String>(null,null,null);
    BTNode<String> current = new BTNode<String>(null,null,null);
    Object temp;

    public ExpressionTree(String line){

    Scanner st = new Scanner(line);

    current = buildTree(st, head);

    }

    //helper method for ExpressionTree
    public BTNode buildTree(Scanner st,BTNode node){
    if(!st.hasNext())
    return null;
    temp = st.next();
    if(temp instanceof Integer){
    return new BTNode((String)temp, null,null);
    }
    else{
    node.setLeft(buildTree(st,node));
    node.setRight(buildTree(st,node));
    return new BTNode((String) temp, node.getLeft(),node.getRight());
    }

    }

    public void evaluateHelper(){

    }


    public void evaluate() {

    }

    public String toStringHelper(BTNode temp, String eval){
    System.out.println(temp.getData());
    // System.out.println(temp.getLeft().getData());
    //System.out.println(temp.getRight().getData());
    return eval;
    }

    public String toString(){

    return toStringHelper(current,"");
    }

    }

  9. #9
    jeevesgreen is offline Member
    Join Date
    Dec 2009
    Posts
    2
    Rep Power
    0

    Default

    Never Mind Guys. After some hours of hardcore coding I finally understood my mistakes. Just to let everyone know Using the "instanceof" method is a bad Idea... stay away from that method when creating ExpressionTrees (with Strings). Also, if people are using Chars or Ints rather than Strings then they should invest their time into learning about "Switch" methods. Totally worth the time.

    Best,
    JeevesGreen

Similar Threads

  1. Creating a Tree and then saving the Tree
    By jackmatt2 in forum New To Java
    Replies: 0
    Last Post: 08-22-2009, 12:51 PM
  2. Creating a tree of depth 5
    By roaan in forum New To Java
    Replies: 1
    Last Post: 07-13-2009, 09:26 AM
  3. illigel expression
    By ff123 in forum New To Java
    Replies: 6
    Last Post: 05-09-2009, 12:09 AM
  4. Quick reg. expression help
    By RR_QQ in forum New To Java
    Replies: 4
    Last Post: 02-10-2009, 07:51 PM
  5. Creating a tree
    By Preethi in forum AWT / Swing
    Replies: 0
    Last Post: 01-07-2008, 01:04 PM

Posting Permissions

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