Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java “我现在该怎么办?”;“建立”;用扫描仪扫描树?(代码后解释)_Java_Tree_Prefix_Postfix Notation_Infix Notation - Fatal编程技术网

Java “我现在该怎么办?”;“建立”;用扫描仪扫描树?(代码后解释)

Java “我现在该怎么办?”;“建立”;用扫描仪扫描树?(代码后解释),java,tree,prefix,postfix-notation,infix-notation,Java,Tree,Prefix,Postfix Notation,Infix Notation,本作业旨在帮助理解和实践树的概念和遍历。我应该使用树来计算前缀(波兰语)表示法中的表达式,以及以后缀(反向波兰语)表示法和中缀表示法打印表达式 下面是给出的评估类 import java.util.*; public class Evaluation { public static void main(String[] args) { System.out.print("Enter a prefix expression: "); Scanner scanner = ne

本作业旨在帮助理解和实践树的概念和遍历。我应该使用树来计算前缀(波兰语)表示法中的表达式,以及以后缀(反向波兰语)表示法和中缀表示法打印表达式

下面是给出的评估类

 import java.util.*;
 public class Evaluation
 {
public static void main(String[] args)
{
    System.out.print("Enter a prefix expression:  ");
    Scanner scanner = new Scanner(System.in);

    // build an expression tree
    Node root = buildTree(scanner);

    // print prefix expression
    System.out.println("prefix expression:");
    preorder(root);
    System.out.println();

    // print postfix expression
    System.out.println("postfix expression:");
    postorder(root);
    System.out.println();

    // print infix expression
    System.out.println("infix expression:");
    inorder(root);
    System.out.println();

    // evaluate the expression using postfix
    System.out.println("Result = " + evaluate(root));
}

// build an expression tree and return the root node of the tree
public static Node buildTree(Scanner sc)
{
    // your code here

}

// Postfix expression is the result of a post-order traversal
public static void postorder(Node node)
{
    // your code here

}

// Prefix expression is the result of a pre-order traversal
public static void preorder(Node node)
{
    // your code here

}

// Infix expression is the result of a in-order traversal
public static void inorder(Node node)
{
    // your code here

}

// Evaluate the expression tree using postorder traversal
public static int evaluate(Node node)
{
    // your code here

}
 }
编辑:这是我必须编写的Node类

 public class Node {
String value;
Node left, right;

Node(String value){
    this.value = value;
}

Node(String value, Node left, Node right){
    this.left = left;
    this.right = right;

}

public boolean isLeaf(){
    if()

    return true;
}

 }
作业说明是使用给定的方法buildTree构建一棵树,并以前缀表示法计算4个给定表达式:

1.-+10*2 8 3

2./*+31427

3.++4 2*3-15 1

4./-%+1236+23

编辑:在以前的作业中,我必须使用堆栈计算后缀表达式,但如何使用此树构建和遍历前缀表达式?
(我假设操作数和运算符将是节点的值,然后节点将以某种方式链接。)

好吧,让我们考虑一下。是什么使节点成为叶

我认为叶是一个没有子节点的节点。那么,对
if
检查使用以下布尔表达式怎么样:
if(left==null&&right==null)

稍后编辑:事实上,您甚至不需要进行
if
检查。你可以简单地说

public boolean isLeaf(){
    return left == null && right == null;
}
另外,请注意,您的重载构造函数

Node(String value, Node left, Node right){
    this.left = left;
    this.right = right;

}

未正确分配

如果它是叶节点,它将不会有任何对其他节点的引用,因此左侧节点和右侧节点将为空。您可以通过对这两个节点都应用检查来检查它是否是叶节点,如果两个节点都引用null,则返回true

if(left == null && right == null)
    return true;
else
    return false;