二进制树未清除每个环路迭代

发布于 2025-01-26 22:32:40 字数 1935 浏览 3 评论 0原文

我正在使用二进制树将前缀表达式转换为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;
        }
    }
}

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文