首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >构建二叉树

构建二叉树
EN

Stack Overflow用户
提问于 2016-04-09 21:29:29
回答 1查看 352关注 0票数 1

每当二叉树在循环中滚动时,我就将表达式构建为二叉树,在")“的每个结尾创建一个新树,并将这些运算符/操作数推到堆栈中,然后弹出回一个完整的二叉树。

我的构建方法:

代码语言:javascript
复制
package lab5;

import net.datastructures.*;

public class Expression<T> {

/** Contain Linked Tree and Linked Stack instance variables **/
LinkedBinaryTree<T> tree;
LinkedStack<LinkedBinaryTree<T>> stack;


public Expression () {
    tree = new LinkedBinaryTree<T> ();
    stack = new LinkedStack<LinkedBinaryTree<T>>();

} // end constructor

public LinkedBinaryTree<T> buildExpression (String expression) {// LinkedBinaryTree<T> is a type of LinkedBinaryTree
    // major TODO to implement the algorithm]
    LinkedBinaryTree<T> operand, op1, op2;
    LinkedStack<LinkedBinaryTree<T>> newStack = new LinkedStack<LinkedBinaryTree<T>>();
    String symbol;

    int i = 0;
    int len = expression.length();

    for (i = 0; i < len; i++) {
        symbol = expression.substring(i, i+1);

        if ((!symbol.equals ("(")) && (!symbol.equals (")"))) {
            operand = new LinkedBinaryTree<T> ();
            operand.addRoot((T)symbol);
            newStack.push(operand);
        } else if (symbol.equals ("(")){
            continue;
        } else {
            op2 = newStack.pop();
            operand = newStack.pop();
            op1 = newStack.pop();
        tree.attach(operand.root(), op1, op2);
        newStack.push(tree);
        }
    }
    tree = newStack.pop();
    return tree;

}  // end method buildExpression

}

我的测试:

代码语言:javascript
复制
package lab5;
import net.datastructures.*;

public class ExpressionTest {

/**
 * @param args
 * @throws EmptyTreeException
 */

/** Paranthesize is code fragment 8.26 pg. 346 **/

/** evaluateExpression method apart of LinkedBinaryTree class in net.datastructures **/

public static void main(String[] args) {
    // declare local variables/objects
            String s = new String ();
            String exp = "((((3+1)x3)/((9-5)+2))-((3x(7-4))+6))"; //-13
            Expression<String> expression = new Expression<String> ();

            // print the expression string
            System.out.printf ("The original Expression String generated via printf: %s", exp);

            // create the tree using the 'stub' method
            // i.e. it does nothing
            LinkedBinaryTree<String> tree = expression.buildExpression (exp);

            // use Object.toString simply to print its reference
            System.out.printf ("\n\nThe Tree: %s\n", tree);

    }

}

我得到了一个NullPointerException,我不知道为什么。我需要“尝试”然后让错误滚过去吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-09 22:42:11

错误可能与检查'(' (单引号)有关,而不是symbol.equals ('(')中的"(" (双引号)。您可以将一个字符串与一个字符进行比较,这个字符永远都不相等。

也许stack也不是初始化的?它应该是buildExpression()的本地。

请注意,您的代码片段不会编译:

  • 未定义stack
  • 未定义symbol

顺便说一句:buildExpression()可以是静态的,这样可以避免main中空的未使用的表达式分配。

首先检查"("将避免检查"("两次。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36522929

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档