从预序位串构建二叉树

发布于 2024-11-05 08:41:17 字数 2182 浏览 0 评论 0原文

我正在尝试做一项作业,但第一步遇到了麻烦。下面的链接是上下文的分配:

https://docs.google.com/viewer?a=v&pid=explorer&chrome=true&srcid=0B1DkmkmuB-leNDVmMDU0MDgtYmQzNC00OTdkLTgxMDETZTkxZWQyYjM4OTI1&hl=en

示例输入为:

a0
0
a00
ab000

给出的输出为:

树 1:
无效!
树 2:
高度:-1
路径长度:0
完整:是
邮购:
树 3:
高度:0
路径长度:0
完整:是
后序:a
树 4:
高度:1
路径长度:1
完整:是
后序:ba

我无法继续完成作业,因为我一直坚持从输入实际构建二叉树。到目前为止我能够提出的代码如下:

public class btsmall {
    int k = 0;
    char[] cArray;

    public static void main(String[] args) throws IOException {
        new btsmall().run();
    }

    static class Node {
        Node left;
        Node right;
        char value;

        public Node(char value) {
            this.value = value;
        }
    }

    public void run() throws IOException {
        String preorder;
        InputStreamReader input = new InputStreamReader(System.in);
        BufferedReader reader = new BufferedReader(input);

        while ((preorder = reader.readLine()) != null) {
            cArray = preorder.toCharArray();
            Node tree = null;
            insert(tree);
            preorder(tree);
            k = 0;
        }
    }

    public void insert(Node node) {
        if (cArray[k] == (char) 0) {
            node = new Node((char) 0);
            node.left = node.right = null;
            k++;
        } else {
            node = new Node(cArray[k]);
            k++;
            insert(node.left);
            insert(node.right);
        }
    }

    public void preorder(Node node) {
        if (node != null) {
            System.out.println(node.value + " ");
            preorder(node.left);
            preorder(node.right);
        }
    }
}

我正在尝试测试我是否使用预排序方法正确构建了二叉树,但是每当我运行该程序时,它似乎陷入了某个地方的无限循环中。谁能帮忙指出是什么原因造成的?我真的走在正确的道路上吗?有人对我应该如何构建这个特定的二叉树有任何提示吗?

谢谢。

I am trying to do an assignment but I'm having trouble with the first step. The link below is the assignment for context:

https://docs.google.com/viewer?a=v&pid=explorer&chrome=true&srcid=0B1DkmkmuB-leNDVmMDU0MDgtYmQzNC00OTdkLTgxMDEtZTkxZWQyYjM4OTI1&hl=en

A sample input is:

a0
0
a00
ab000

Which gives an output of:

Tree 1:
Invalid!
Tree 2:
height: -1
path length: 0
complete: yes
postorder:
Tree 3:
height: 0
path length: 0
complete: yes
postorder: a
Tree 4:
height: 1
path length: 1
complete: yes
postorder: ba

I am unable to progress on the assignment because I am stuck on actually building the binary tree from the input. The code I have been able to come up with so far is below:

public class btsmall {
    int k = 0;
    char[] cArray;

    public static void main(String[] args) throws IOException {
        new btsmall().run();
    }

    static class Node {
        Node left;
        Node right;
        char value;

        public Node(char value) {
            this.value = value;
        }
    }

    public void run() throws IOException {
        String preorder;
        InputStreamReader input = new InputStreamReader(System.in);
        BufferedReader reader = new BufferedReader(input);

        while ((preorder = reader.readLine()) != null) {
            cArray = preorder.toCharArray();
            Node tree = null;
            insert(tree);
            preorder(tree);
            k = 0;
        }
    }

    public void insert(Node node) {
        if (cArray[k] == (char) 0) {
            node = new Node((char) 0);
            node.left = node.right = null;
            k++;
        } else {
            node = new Node(cArray[k]);
            k++;
            insert(node.left);
            insert(node.right);
        }
    }

    public void preorder(Node node) {
        if (node != null) {
            System.out.println(node.value + " ");
            preorder(node.left);
            preorder(node.right);
        }
    }
}

I am trying to test that I'm building the binary tree correctly with the preorder method, but whenever I run the program it seems to be stuck in an infinite loop somewhere. Can anyone help point out what is causing it? And am I actually on the right track with this? Does anyone have any hints on how I should go about building this particular binary tree?

Thanks.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

┼── 2024-11-12 08:41:17

它不是无限循环。它只是等待来自 System.in 的输入

it's not in an infinite loop. its just waiting for input from System.in

春花秋月 2024-11-12 08:41:17

(char) 0 是由 Unicode U+0000 (NUL) 表示的字符。您希望在测试中使用'0' (U+0030)。

顺便说一句,问题设置者没有说明给定的预序是深度优先还是广度优先(或其他),因此无法确定如何正确重建树。

(char) 0 is the character which is represented by Unicode U+0000 (NUL). You want to use '0' (U+0030) in your test.

As an aside, the problem setter has not stated whether the given preorder is depth-first or breadth-first (or something else), so one cannot be certain how to rebuild the tree correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文