二进制树未清除每个环路迭代
我正在使用二进制树将前缀表达式转换为infix,然后将其求解。 该部分正常工作。
我的问题是我设置的输入循环是将新表达式添加到旧树上,导致错误。 我已经尝试清除链接列表,并手动创建了一种清空树的方法。 但是,每次我的输出都显示出附加到上一个的新表达式,而不是创建一个全新的树。
public static void main(final String[] args) {
Scanner listener = new Scanner(System.in);
System.out.println();
System.out.println("Enter a prefix expression Separated by space!! (Enter 'done' to end): ");
String line = listener.nextLine();
while(!line.equalsIgnoreCase("done")){
System.out.println(line);
if(line.length()<=1){ System.out.println("Invalid expression"); listener.close(); return;}
instances(line);
line = new String();
line = listener.nextLine();
}
listener.close();
}
public static void instances(String input){
System.out.println("The input expression was \n\t"+ input +"\n");
String[] in = input.split(" ");
LinkedList<String> queue = new LinkedList<>(Arrays.asList(in));
TreeNode root = new TreeNode(queue, null);
System.out.println("Expression as infix: ");
inOrder(root);
System.out.println(infix_string);
String[] infix = infix_string.split(" ");
Stack infix_stack = new Stack(infix);
System.out.print("\t" + infix_stack + " = ");
infix_stack.answer();
System.out.println();
System.out.println("Enter a prefix expression Separated by space!! (Enter 'done' to end): ");
queue.clear();
clearTree(root);
}
public static void clearTree(TreeNode root){
if(root != null){
clearTree(root.left);
clearTree(root.right);
root=null;
}
}
}
I am using a binary tree to convert prefix expression into infix, and then solving them.
That part works fully.
My issue is the input loop I set up is adding on the new expression to the old tree, resulting in an error.
I have tried clearing the linked list, and manually created a method to empty out the tree.
But every time, the output I have is showing the new expression appended to the previous one, rather than creating a whole new tree.
public static void main(final String[] args) {
Scanner listener = new Scanner(System.in);
System.out.println();
System.out.println("Enter a prefix expression Separated by space!! (Enter 'done' to end): ");
String line = listener.nextLine();
while(!line.equalsIgnoreCase("done")){
System.out.println(line);
if(line.length()<=1){ System.out.println("Invalid expression"); listener.close(); return;}
instances(line);
line = new String();
line = listener.nextLine();
}
listener.close();
}
public static void instances(String input){
System.out.println("The input expression was \n\t"+ input +"\n");
String[] in = input.split(" ");
LinkedList<String> queue = new LinkedList<>(Arrays.asList(in));
TreeNode root = new TreeNode(queue, null);
System.out.println("Expression as infix: ");
inOrder(root);
System.out.println(infix_string);
String[] infix = infix_string.split(" ");
Stack infix_stack = new Stack(infix);
System.out.print("\t" + infix_stack + " = ");
infix_stack.answer();
System.out.println();
System.out.println("Enter a prefix expression Separated by space!! (Enter 'done' to end): ");
queue.clear();
clearTree(root);
}
public static void clearTree(TreeNode root){
if(root != null){
clearTree(root.left);
clearTree(root.right);
root=null;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论