解析字符串向量中的 Java 树结构中的节点

发布于 2024-10-01 09:50:54 字数 1777 浏览 0 评论 0原文

我有一个看起来像这样的数据结构

private String name;
private ArrayList<Node> children;
private String parent="";

public Node(String name) {
setName(name);
children = new ArrayList<Node>();
}

在我的程序的其他地方,我有一个名为“root”的节点,其中包含整个树数据结构。

从概念上讲,它看起来像这样

                                       root
                                      /     \
                                     /       \
                                  node1     node2
                                   /           \
                                  /             \
                                node2          node3
                                / 
                               /
                             node3

正如您所看到的,节点可以具有相同的名称。这是有意的。我想为每个节点创建一个字符串,其中包含其自己的名称及其沿袭,并将它们存储在向量中。

因此左侧的节点 3 为 "root|node1|node2|node3" 右侧的节点 3 为 "root|node2|node3" node1 为“root|node1”等。

我有一种方法可以迭代节点结构来打印每个节点,但我发现很难设置每个父节点,因为我无法找到一种方法来做到这一点。任何帮助都会很棒,因为到目前为止我尝试过的一切都失败了。一个重要的注意事项是,该树不一定是二叉树,我只是用它作为示例。

这是我用来打印树的每个节点的代码。希望它很容易调整。

public void print() {
        LinkedList<Node> open = new LinkedList<Node>();
        LinkedList<Node> closed = new LinkedList<Node>();

        open.add(this);

        while(!open.isEmpty()) {
            Node currentNode = open.removeFirst();
            System.out.println(currentNode.getName());

            ArrayList<Node> children = currentNode.getChildren();
            closed.add(currentNode);

            for(int i = 0; i < children.size(); i++) {
                Node current = children.get(i);
                open.addLast(current);
            }
        }
    }

谢谢你们。

I have a data structure that looks like this

private String name;
private ArrayList<Node> children;
private String parent="";

public Node(String name) {
setName(name);
children = new ArrayList<Node>();
}

Elsewhere in my program, I have a Node called "root" that contains an entire tree data structure.

Conceptually it looks like this

                                       root
                                      /     \
                                     /       \
                                  node1     node2
                                   /           \
                                  /             \
                                node2          node3
                                / 
                               /
                             node3

As you can see nodes can have the same name. That's intended. I want to create a string for each node that contains its own name, plus it's lineage and store them in a Vector.

so node 3 on the left hand side would be "root|node1|node2|node3" the node3 on the rhs would be "root|node2|node3" node1 would be "root|node1"etc.

I have a way to iterate through the node structure to print each node, but I'm finding it difficult to set every parent, as in, I can't figure out a way to do it. Any help would be fantastic as everything I've tried so far has failed. One important note is that the tree may not necessarily be a Binary tree, I'm just using it for an example.

Here's the code I use for printing every node of the tree. Hopefully it will be easy to tweak.

public void print() {
        LinkedList<Node> open = new LinkedList<Node>();
        LinkedList<Node> closed = new LinkedList<Node>();

        open.add(this);

        while(!open.isEmpty()) {
            Node currentNode = open.removeFirst();
            System.out.println(currentNode.getName());

            ArrayList<Node> children = currentNode.getChildren();
            closed.add(currentNode);

            for(int i = 0; i < children.size(); i++) {
                Node current = children.get(i);
                open.addLast(current);
            }
        }
    }

Thanks guys.

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

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

发布评论

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

评论(2

笑叹一世浮沉 2024-10-08 09:50:54

我假设您已经创建了这些节点,并且它们是与子节点一起创建的,但没有父节点?似乎有几个选项:

  1. 创建子项时,设置父项(我认为您实际上无法控制它,因为您在问这个问题)所以......
  2. 可以使用某种类型的 Map 而不是 Vector您可以在其中将密钥设置为沿袭。然后,当您迭代节点时,进行一些字符串调整以删除当前节点的名称,并留下父谱系。
  3. 与 #2 相关,不要使用 Map(并保留 Vector)并仍然执行字符串操作,但您必须迭代 Vector 中的每个节点以按其谱系查找父节点。

希望这有帮助,希望我的假设是正确的
-戴夫

I'm assuming that you already have these nodes created and they were created with children but without the parent? Seems like there are a few options:

  1. When creating the children, set the parent (I would think you don't really have control over this because you are asking this question) so...
  2. Instead of a Vector maybe use some type of Map where you can set the key as the lineage. Then when you iterate over the nodes do some string tweaking to remove the current node's name and you are left with the parent lineage.
  3. Related to #2, don't use a Map (and keep the Vector) and still do the string manipulation but you would then have to iterate every node in the Vector to look for the parent by its lineage.

Hope this helps and hopefully I assumed correctly
-Dave

无声静候 2024-10-08 09:50:54

似乎在构建树时添加父节点会更容易,但是如果构建了树并且想要向每个节点添加父节点,则可以使用递归。
我会尝试类似的方法

addParent(root, "");

public void addParent(Node node, String parent) {
  node.setParent(parent);

  // if this node has children iterate through them
  // and call addParent with current node name.
  for(Node childNode : node.getChildren()) {
        addParent(childNode, node.getName());
  }
}

注意:在发布之前我无法测试此代码。

it seems that it would be easier to add the parent as you are building the tree but if you have the tree built and want to add a parent to each node you can use recursion.
I'd try something like

addParent(root, "");

public void addParent(Node node, String parent) {
  node.setParent(parent);

  // if this node has children iterate through them
  // and call addParent with current node name.
  for(Node childNode : node.getChildren()) {
        addParent(childNode, node.getName());
  }
}

NOTE: I was not able to test this code before posting.

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