来自预序位串的二叉树

发布于 2024-11-05 01:06:21 字数 371 浏览 0 评论 0原文

我需要从预序位串(通过管道输送到流中的标准输入)构建二叉树,我想知道我对此的理解是否正确。

如果我有一个 11110001000 的预序位串(其中 1 表示内部节点,0 表示外部节点),会产生这样的二叉树吗?

<前><代码> 1 /\ 1 0 /\ 1 1 / \ / \ 1 00 0 /\ 0 0

从预序位串(通过输入给出)构建二叉树后,我还需要查找高度、路径长度以及二叉树是否完整。然而,我在进展到能够做到这一点时遇到了困难,因为我不知道如何开始实现预购位字符串 -> Java 中的二叉树转换。有人可以提示我如何开始从预序位字符串构建二叉树吗?

I need to build a binary tree from a preorder bitstring (which is piped into standard input in a stream) and I was wondering if my understanding of this was correct.

If I had a preorder bitstring of 11110001000 (where 1 indicates an internal node and 0 indicates an external node), would that result in a binary tree like this?

        1
       / \
      1   0
     / \
    1   1
   / \ / \
  1  00   0
 / \
0   0

After building the binary tree from the preorder bitstring (which is given through the input), I also need to find the height, path length, and whether the binary tree is complete or not. However I'm having trouble progressing to the point where I'm able to do this because I don't know how to get started on implementing the preorder bitstring -> binary tree conversion in Java. Could anyone please give hints on how I'd get started on building a binary tree from the preorder bitstring?

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

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

发布评论

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

评论(2

风吹雨成花 2024-11-12 01:06:21

您可以从我不久前编写的这个简单程序开始,并对其进行调整以接受二进制字符串作为输入而不是手动输入:

import javax.swing.JOptionPane;

class Node {
    int info;
    Node fs;
    Node fd;
}

class BinaryTree {

    public static void main(String[] args) {

        Node tree = null;
        tree = insertRecursivePreorder(tree);

    }

    static Node insertRecursivePreorder (Node n) {
      String input = JOptionPane.showInputDialog("Insert node, 0 to end: \n");
      int dato = Integer.parseInt(input);

      if (dato == 0) {
          n=null;
      } else {
          n=new Node();
          n.info=dato;
          n.fs=insertRecursivePreorder(n.fs);
          n.fd=insertRecursivePreorder(n.fd);
      }
      return n;
    }

}

You can start from this simple program I made some time ago and adapt it to accept a binary string as input instead of the manual input:

import javax.swing.JOptionPane;

class Node {
    int info;
    Node fs;
    Node fd;
}

class BinaryTree {

    public static void main(String[] args) {

        Node tree = null;
        tree = insertRecursivePreorder(tree);

    }

    static Node insertRecursivePreorder (Node n) {
      String input = JOptionPane.showInputDialog("Insert node, 0 to end: \n");
      int dato = Integer.parseInt(input);

      if (dato == 0) {
          n=null;
      } else {
          n=new Node();
          n.info=dato;
          n.fs=insertRecursivePreorder(n.fs);
          n.fd=insertRecursivePreorder(n.fd);
      }
      return n;
    }

}
阿楠 2024-11-12 01:06:21

您可以从此处开始。如果您了解 C++,请阅读这篇文章 也会很有用。

主要思想是有一个 Node 类,它具有对左节点和右节点的引用。然后,您需要做的就是浏览节点。

You can begin from here. And if you know c++, this article will be also useful.

The main idea is to have a Node class, which has references to the left and to the right nodes. Then, all you need to do, is navigate through the nodes.

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