将中缀字符串转换为二叉树的代码,我收到:无法编译的源代码 - 错误的符号类型
Code to convert an infix string to a binary tree and I received: Uncompilable source code - Erroneous sym type
我正在编写一个代码,通过使用 2 个数组列表和一个链表将中缀语句转换为二叉树。我收到一条错误消息:
线程 "main" java.lang.RuntimeException 中的异常:无法编译的源代码 - 错误的符号类型:prog5.InFixToBinaryTreeConverter.precedence
在 prog5.InFixToBinaryTreeConverter.createBinaryTree(InFixToBinaryTreeConverter.java:56)
在 prog5.InFixToBinaryTreeConverter.run(InFixToBinaryTreeConverter.java:31)
在 prog5.Prog5.main(Prog5.java:13)
我很茫然,我该如何解决这些错误?
我的主要方法:
public class Prog5 {
public static void main(String[] args) {
InFixToBinaryTreeConverter fp = new InFixToBinaryTreeConverter();
fp.run("( ( 6 + 2 ) - 5 ) * 8 / 2");
}
}
节点class:
public class Node<String> {
protected String element;
protected Node<String> left;
protected Node<String> right;
int x;
public Node(String e, Node left, Node right){
element = e; //data = element
this.left = this.right = null;
}
public void displayNode(){
System.out.print(element);
}
}
二叉树转换器的中缀class:
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import static jdk.nashorn.internal.runtime.JSType.isNumber;
public class InFixToBinaryTreeConverter{
List<String> stack = new ArrayList<>(); //stack
List<String> inFix= new LinkedList<>(); //queue
List<Node> btstack = new ArrayList<>(); //stack
Node root = null;
//create a no-arg consutrctor that initializes the inFix, stack , &
btstack lists
String expression;
public void run(String s){ // run method is driver for program
this.expression = s;
createInFix();
createBinaryTree();
}
public void createInFix(){
String[] temporary = expression.split("\s+"); //temp = a
for (int i = 0; i < temporary.length; i++ ){
inFix.add(temporary[i]);
}
}
public Node createBinaryTree(){
this.stack.add("(");
this.inFix.add(")");
while(!this.inFix.isEmpty()){
String variable = this.inFix.remove(0);
if(isNumber(variable)){
Node nodeNew = new Node(variable);
this.btstack.add(nodeNew);
}
if(isLeftParentheses(variable)){
this.stack.add(variable);
}
if(isOperator(variable)){
while(precedence(this.stack.get((this.stack.size()-1, variable)))){
Node right = this.btstack.remove((this.btstack.size()-1));
Node left = this.btstack.remove((this.btstack.size()-1));
Node nodeNew = new Node(this.stack.remove(this.stack.size()-1), left, right);
this.btstack.add(nodeNew);
}
}
this.stack.add(variable);
}
if(isRightParentheses(variable)){
if(this.stack.get(this.stack.size()-1) != null){
while(!isLeftParentheses(this.stack.get(this.stack.size()-1))){
Node right = this.btstack.remove((this.btstack.size()-1));
Node left = this.btstack.remove((this.btstack.size()-1));
Node nodeNew = new Node(this.stack.remove(this.stack.size()-1), left, right);
this.btstack.add(nodeNew);
}
}
root = this.btstack.get((this.btstack.size()-1));
}
return root;
}
void process(Node node){
System.out.print(node.element+ " ");
}
/* Given a binary tree, print its nodes in inorder*/
private void printInorder(Node node){
if (node != null){
printInorder(node.left); // first recur on left child
process(node); // then print the data of node
printInorder(node.right); // now recur on right child
}
}
/* Given a binary tree, print its nodes in preorder*/
private void printPreorder(Node node){
if (node != null){
process(node); // first print data of node
printPreorder(node.left); // then recur on left sutree
printPreorder(node.right); // now recur on right subtree
}
}
private void printPostorder(Node node){
if (node != null){
printPreorder(node.left); // then recur on left sutree
printPreorder(node.right); // now recur on right subtree
process(node); // first print data of node
}
}
void printPostorder(){
printPostorder(root);
}
void printInorder(){
printInorder(root);
}
void printPreorder(){
printPreorder(root);
}
private static boolean isOperator(String str){ //check to see if c is an operator
if("+".equals(str) || "-".equals(str) || "*".equals(str) || "/".equals(str) || "^".equals(str)){
return true;
}else {
return false;
}
}
private static boolean precendence(String operator1, String operator2){
if("^".equals(operator1)){
return true;
} else if ("^".equals(operator2)){
return false;
}else if ("*".equals(operator1) || "/".equals(operator1)){
return true;
}else if("+".equals(operator1) || "-".equals(operator1)){
if("*".equals(operator2) || "/".equals(operator2)){
return false;
}else{
return true;
}
}return false;
}
private boolean isLeftParentheses(String x) {
if("(".equals(x)){
return true;
}
else {
return false;
}
}
private boolean isRightParentheses(String x) {
if(")".equals(x)){
return true;
}
else {
return false;
}
}
}
您在 while 循环条件中拼写错误 precedence
,您在下面定义的方法名为 precendence
。
我不确定你使用的是什么编辑器,但在 Eclipse 中这会显示为错误,当你将鼠标悬停在它上面时,它会说该方法不存在。
而 this.stack.get
方法只接受一个 int
参数,但您传递的是 (this.stack.size()-1, variable)
。你的意思可能是这样的:
precedence(this.stack.get(this.stack.size() - 1), variable)
我正在编写一个代码,通过使用 2 个数组列表和一个链表将中缀语句转换为二叉树。我收到一条错误消息:
线程 "main" java.lang.RuntimeException 中的异常:无法编译的源代码 - 错误的符号类型:prog5.InFixToBinaryTreeConverter.precedence 在 prog5.InFixToBinaryTreeConverter.createBinaryTree(InFixToBinaryTreeConverter.java:56) 在 prog5.InFixToBinaryTreeConverter.run(InFixToBinaryTreeConverter.java:31) 在 prog5.Prog5.main(Prog5.java:13)
我很茫然,我该如何解决这些错误?
我的主要方法:
public class Prog5 {
public static void main(String[] args) {
InFixToBinaryTreeConverter fp = new InFixToBinaryTreeConverter();
fp.run("( ( 6 + 2 ) - 5 ) * 8 / 2");
}
}
节点class:
public class Node<String> {
protected String element;
protected Node<String> left;
protected Node<String> right;
int x;
public Node(String e, Node left, Node right){
element = e; //data = element
this.left = this.right = null;
}
public void displayNode(){
System.out.print(element);
}
}
二叉树转换器的中缀class:
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import static jdk.nashorn.internal.runtime.JSType.isNumber;
public class InFixToBinaryTreeConverter{
List<String> stack = new ArrayList<>(); //stack
List<String> inFix= new LinkedList<>(); //queue
List<Node> btstack = new ArrayList<>(); //stack
Node root = null;
//create a no-arg consutrctor that initializes the inFix, stack , &
btstack lists
String expression;
public void run(String s){ // run method is driver for program
this.expression = s;
createInFix();
createBinaryTree();
}
public void createInFix(){
String[] temporary = expression.split("\s+"); //temp = a
for (int i = 0; i < temporary.length; i++ ){
inFix.add(temporary[i]);
}
}
public Node createBinaryTree(){
this.stack.add("(");
this.inFix.add(")");
while(!this.inFix.isEmpty()){
String variable = this.inFix.remove(0);
if(isNumber(variable)){
Node nodeNew = new Node(variable);
this.btstack.add(nodeNew);
}
if(isLeftParentheses(variable)){
this.stack.add(variable);
}
if(isOperator(variable)){
while(precedence(this.stack.get((this.stack.size()-1, variable)))){
Node right = this.btstack.remove((this.btstack.size()-1));
Node left = this.btstack.remove((this.btstack.size()-1));
Node nodeNew = new Node(this.stack.remove(this.stack.size()-1), left, right);
this.btstack.add(nodeNew);
}
}
this.stack.add(variable);
}
if(isRightParentheses(variable)){
if(this.stack.get(this.stack.size()-1) != null){
while(!isLeftParentheses(this.stack.get(this.stack.size()-1))){
Node right = this.btstack.remove((this.btstack.size()-1));
Node left = this.btstack.remove((this.btstack.size()-1));
Node nodeNew = new Node(this.stack.remove(this.stack.size()-1), left, right);
this.btstack.add(nodeNew);
}
}
root = this.btstack.get((this.btstack.size()-1));
}
return root;
}
void process(Node node){
System.out.print(node.element+ " ");
}
/* Given a binary tree, print its nodes in inorder*/
private void printInorder(Node node){
if (node != null){
printInorder(node.left); // first recur on left child
process(node); // then print the data of node
printInorder(node.right); // now recur on right child
}
}
/* Given a binary tree, print its nodes in preorder*/
private void printPreorder(Node node){
if (node != null){
process(node); // first print data of node
printPreorder(node.left); // then recur on left sutree
printPreorder(node.right); // now recur on right subtree
}
}
private void printPostorder(Node node){
if (node != null){
printPreorder(node.left); // then recur on left sutree
printPreorder(node.right); // now recur on right subtree
process(node); // first print data of node
}
}
void printPostorder(){
printPostorder(root);
}
void printInorder(){
printInorder(root);
}
void printPreorder(){
printPreorder(root);
}
private static boolean isOperator(String str){ //check to see if c is an operator
if("+".equals(str) || "-".equals(str) || "*".equals(str) || "/".equals(str) || "^".equals(str)){
return true;
}else {
return false;
}
}
private static boolean precendence(String operator1, String operator2){
if("^".equals(operator1)){
return true;
} else if ("^".equals(operator2)){
return false;
}else if ("*".equals(operator1) || "/".equals(operator1)){
return true;
}else if("+".equals(operator1) || "-".equals(operator1)){
if("*".equals(operator2) || "/".equals(operator2)){
return false;
}else{
return true;
}
}return false;
}
private boolean isLeftParentheses(String x) {
if("(".equals(x)){
return true;
}
else {
return false;
}
}
private boolean isRightParentheses(String x) {
if(")".equals(x)){
return true;
}
else {
return false;
}
}
}
您在 while 循环条件中拼写错误 precedence
,您在下面定义的方法名为 precendence
。
我不确定你使用的是什么编辑器,但在 Eclipse 中这会显示为错误,当你将鼠标悬停在它上面时,它会说该方法不存在。
而 this.stack.get
方法只接受一个 int
参数,但您传递的是 (this.stack.size()-1, variable)
。你的意思可能是这样的:
precedence(this.stack.get(this.stack.size() - 1), variable)