代码帮助,无法获取二叉树

时间:2015-12-01 16:19:15

标签: java binary-tree binary-search-tree postfix-notation

所以我的目标是将某种表达式转换为后缀表达式(check),然后将该表达式转换为二叉树。这是我的主要课程

public class ConvertIntoTree 
{
    public static void main(String[] args) 
    {
        // Create a new InfixToPostfix expression from input
        InfixtoPostfix mystack = new InfixtoPostfix(15);
        System.out.println("Type in an expression like (1+2)*(3+4)/(12-5) //No spaces ");
        Scanner scan = new Scanner(System.in);
        String str = scan.next();
        System.out.println("The Expression you have typed in infix form :\n"+str);
        System.out.println("The Expression in Postfix is :\n"+mystack.InToPost(str));

        ExpressionTree expressionTree = new ExpressionTree(str);
        expressionTree.createExpressionTree();
        //expressionTree.prefix();
        System.out.println("The Expression I want");
        expressionTree.infix();        
    }

}

这是我的表达式树类

class ExpressionTree {

    private final String postfix;
    private TreeNode root;

    /**
     * Takes in a valid postfix expression and later its used to construct the expression tree.
     * The posfix expression, if invalid, leads to invalid results 
     * 
     * @param postfix   the postfix expression.
     */
    public ExpressionTree(String postfix) {
        if (postfix == null) { throw new NullPointerException("The posfix should not be null"); }
        if (postfix.length() == 0)  { throw new IllegalArgumentException("The postfix should not be empty"); } 
        this.postfix = postfix;
    }

    private static class TreeNode {
        TreeNode left;
        char ch;
        TreeNode right;

        TreeNode(TreeNode left, char ch, TreeNode right) {
            this.left = left;
            this.ch = ch;
            this.right = right;
        }
    }


    private boolean isOperator(char c) {
        return c == '+' || c == '-' || c == '*' || c == '/';
    }


    /**
     * Constructs an expression tree, using the postfix expression
     */
    public void createExpressionTree() {
        final Stack<TreeNode> nodes = new Stack<TreeNode>();
        for (int i = 0; i < postfix.length(); i++) {
            char ch  = postfix.charAt(i);
            if (isOperator(ch)) {
               TreeNode rightNode = nodes.pop();
               TreeNode leftNode = nodes.pop();
               nodes.push(new TreeNode(leftNode, ch, rightNode));
            } else {
                nodes.add(new TreeNode(null, ch, null));
            }
        }
        root = nodes.pop();
    }


    /**
     * Returns the prefix notation
     * 
     * @return the prefix notation
     */
    public String prefix() {
        if (root == null) {
            throw new NoSuchElementException("The root is empty, the tree has not yet been constructed.");
        }

        final StringBuilder prefix = new StringBuilder();
        preOrder(root, prefix);
        return prefix.toString();
    }

    private void preOrder(TreeNode node, StringBuilder prefix) {
        if (node != null) {
            prefix.append(node.ch);
            preOrder(node.left, prefix);
            preOrder(node.right, prefix);
        }
    }

    /**
     * Returns the infix expression
     * 
     * @return  the string of infix.
     */
    public String infix() {
        if (root == null) {
            throw new NoSuchElementException("The root is empty, the tree has not yet been constructed.");
        }
        final StringBuilder infix = new StringBuilder();
        inOrder(root, infix);
        return infix.toString();
    }

    private void inOrder(TreeNode node, StringBuilder infix) {
        if (node != null) {
            inOrder(node.left, infix);
            infix.append(node.ch);
            inOrder(node.right, infix);
        }

    }

}

我运行expressionTree.infix();后,什么都没有。显然我做错了什么。任何想法或帮助,谢谢。

1 个答案:

答案 0 :(得分:0)

经过一点修改后,主要问题是使用expressionTree2.infix() 而不是System.out.println(expressionTree2.infix());