2012-02-26 8 views
1

このプログラムをコード化しています。このプログラムは、中置表記式をとり、そこからバイナリツリーを構築します。現在、実際に動作する唯一の入力式は、2 + 2のようになります。((2 + 2)* 3)のように、かっこを処理してより長い式を取る方法を考えることはできません。私の出力では、私はそれを後置記号にして、表現を評価しようとしています。だから今私は括弧なしで2 + 2を接続すると私の出力は22+であり、それが4を出力するように評価します。Javaバイナリ式ツリー - 式の括弧を確認する

出力をそれぞれの間にスペースを入れて印刷する方法を理解する必要があります数と演算子を使用し、括弧で長い式を受け入れるようにします。私は今ここからどのように続けるのか分かりません。誰も助けてくれますか?

ありがとうございます!

これは私のコードです:

class ExpressionEvaluator<T> { 

    ExpressionEvaluator() { 
     Scanner reader = new Scanner(System.in); 

     System.out.println("Enter your expression: "); 
     String expression = reader.nextLine(); // Reads the expression and trims the spaces. 
     expression = expression.replaceAll(" ", ""); 
     ExpressTree<T> tree = new ExpressTree(expression); // Creates a new binary tree. 
    } 

    public static void main(String[] args) { 
     new ExpressionEvaluator(); 
    } 
} 


interface Tree<T> { 

    // This is the tree interface with its methods. 
    public Node<T> getRoot(); 

    public void setRoot(Node<T> value); 
} 


class ExpressTree<T> implements Tree<T> { 

    private Node<T> _root; 
    private String _value, _expression; 
    private Node<T> _treeNode; 
    private Stack storage1; 
    private Stack<String> storage2; 

    public ExpressTree(String expression) { 
     expressTreeBuilder(expression);  // Calls the expressTreeBuilder method on the expression. 
     postfixTraversal(this.getRoot()); 
     System.out.println(""); 
     System.out.println(this.storage2.pop()); 
    } 

    public Node<T> getRoot() {  // Gets the root of the tree. 
     return _root; 
    } 

    public void setRoot(Node<T> _treeNode2) {  // Sets the root which will always be an operator. 
     this._root = _treeNode2; 
    } 

    public boolean isDigit(char value) { // Method to check if a value is a digit or not. 
     if (Character.isDigit(value)) { 
      return true; 
     } else { 
      return false; 
     } 
    } 

    public void expressTreeBuilder(String expression) { 

     storage1 = new Stack(); // Stores the nodes. 
     storage2 = new Stack<String>();  // Stores the value of the nodes. 
     _treeNode = new Node();  // Creates a new node 
     _value = null; 
     String next = ""; 

     for (int i = 0; i < expression.length(); i++) { 
      char traverse = expression.charAt(i); 
      if (i + 1 < expression.length()) { 
       next = Character.toString(expression.charAt(i + 1)); 
      } 

      if (traverse == '+') {  // This checks to see the expression is an addition operator. 
       Node<T> pop1, pop2;  // It pops the first 2 values from the stack and stores them 
       String value1, value2; // in pop1 and pop2. Then it uses another stack to add them. 
       pop1 = (Node<T>) storage1.pop(); 
       value1 = storage2.pop(); // Stores the right side value. 
       value2 = next; // Stores the left side value. 
       _treeNode = new Node(); 
       _treeNode.setElement(Character.toString(traverse)); 
       pop2 = new Node(); 
       pop2.setElement(next); 
       pop1.setParent(_treeNode); 
       pop2.setParent(_treeNode); 
       _treeNode.setLeftSide(pop1);  // Sets the right side of the subtree. 
       _treeNode.setRightSide(pop2);  // Sets the left side of the subtree. 
       storage1.push(_treeNode); 
       storage1.push(pop2); 
       int add = (Integer.parseInt(value2) + Integer.parseInt(value1)); 
       storage2.push(Integer.toString(add)); 
       this.setRoot(_treeNode); 
       i++; 
      } else if (traverse == '-') {  // This checks to see the expression is a subtraction operator. 
       Node<T> pop1, pop2;  // It pops the first 2 values from the stack and stores them 
       String value1, value2; // in pop1 and pop2. Then it uses another stack to subtract them. 
       pop1 = (Node<T>) storage1.pop(); 
       value1 = storage2.pop(); // Stores the right side value. 
       value2 = next; // Stores the left side value. 
       _treeNode = new Node(); 
       _treeNode.setElement(Character.toString(traverse)); 
       pop2 = new Node(); 
       pop2.setElement(next); 
       pop1.setParent(_treeNode); 
       pop2.setParent(_treeNode); 
       _treeNode.setLeftSide(pop1);  // Sets the right side of the subtree. 
       _treeNode.setRightSide(pop2);  // Sets the left side of the subtree. 
       storage1.push(_treeNode); 
       storage1.push(pop2); 
       int subtract = (Integer.parseInt(value2) - Integer.parseInt(value1)); 
       storage2.push(Integer.toString(subtract)); 
       this.setRoot(_treeNode); 
       i++; 
      } else if (traverse == '*') { // This checks to see the expression is a multiplication operator. 
       Node<T> pop1, pop2; // It pops the first 2 values from the stack and stores them 
       String value1, value2; // in pop1 and pop2. Then it uses another stack to multiply them. 
       pop1 = (Node<T>) storage1.pop(); 
       value1 = storage2.pop(); // Stores the right side value. 
       value2 = next; // Stores the left side value. 
       _treeNode = new Node(); 
       _treeNode.setElement(Character.toString(traverse)); 
       pop2 = new Node(); 
       pop2.setElement(next); 
       pop1.setParent(_treeNode); 
       pop2.setParent(_treeNode); 
       _treeNode.setLeftSide(pop1);  // Sets the right side of the subtree. 
       _treeNode.setRightSide(pop2);  // Sets the left side of the subtree. 
       storage1.push(_treeNode); 
       storage1.push(pop2); 
       int multiply = (Integer.parseInt(value2) * Integer.parseInt(value1)); 
       storage2.push(Integer.toString(multiply)); 
       this.setRoot(_treeNode); 
       i++; 
      } else if (traverse == '(') { 

      } else { 
       _treeNode = new Node(); 
       String digits = ""; 
       while (i < expression.length() && isDigit(expression.charAt(i))) { 
        digits += expression.charAt(i); // Checks if the value found at the expression is digit 
        if (digits.length() == 1) { 
         break; 
        } 
        i++; 
       } 

       _treeNode.setElement(digits);  // If it is it will set the element of the node 
       storage1.push(_treeNode);  // The node will be pushed onto the stack. 
       storage2.push(digits);   // This node will store it's value. 
      } 
     } 
    } 

    public void infixTraversal(Node<T> n) { 
     if (n != null) { 
      infixTraversal(n.getLeftSide()); 
      System.out.print(n.element() + ""); 
      infixTraversal(n.getRightSide()); 
     } 
    } 

    public void postfixTraversal(Node<T> n) { 
     if (n != null) { 
      postfixTraversal(n.getLeftSide()); 
      postfixTraversal(n.getRightSide()); 
      System.out.print(n.element()); 
     } 
    } 
} 


class Node<T> { 

    public Node<T> _left, _right, _parent; 
    private String _element; 

    public Node() { 
     this._element = null; 
     this._left = null; 
     this._right = null; 
     this._parent = null; 
    } 

    public String element() {     // Gets the value of the node. 
     return _element; 
    } 

    public Node<T> getLeftSide() {    // Gets the left side of the node. 
     return _left; 
    } 

    public Node<T> getRightSide() {    // Gets the right side of the node. 
     return _right; 
    } 

    public Node<T> getParent() {    // Gets the parent of the node. 
     return _parent; 
    } 

    public void setElement(String e) {   // Sets the value of the node. 
     _element = e; 
    } 

    public void setLeftSide(Node<T> n) {  // Sets the left side of the node. 
     _left = n; 
    } 

    public void setRightSide(Node<T> n) {  // Sets the right side of the node. 
     _right = n; 
    } 

    public void setParent(Node<T> n) {   // Sets the parent of the node. 
     _parent = n; 
    } 
} 

答えて

3

あなたは後置式にあなたの中置式を変換するための適切なアルゴリズムが必要になります。後置式で式を取得したら、構文解析ツリーを構築するか、式を完全に評価するのはかなり簡単です。古典的なアルゴリズムはShunting-yard algorithmです。これについては、ウィキペディアで詳しく説明しています。

後置アルゴリズムが完了したら、もはやパラテシスや演算子の優先順位について心配する必要はありません。それは単なる値のスタックであり、結果をポップしてプッシュします。

0

あなたの質問にはいくつかの異なる問題があります。

1)の発現を解析し、ツリーに分解が。これにはレクサー(またはトークナイザー)が必要です。優先度、かっこなどのシンプルなものさえ必要です。

2)ツリーの変換/評価。これにはいくつかの古典的なアルゴリズムがあります。

3)アウトカムを印刷する(パーツ2を完成したらかなり簡単だと思われますが、正しい場所に正しい空白と括弧を追加するだけです)。

私はこれのすべてについて、いくつかの既存の記事を読むことをお勧めします

は、のは、例えばとしましょう: