解析字符串向量中的 Java 树结构中的节点
我有一个看起来像这样的数据结构
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设您已经创建了这些节点,并且它们是与子节点一起创建的,但没有父节点?似乎有几个选项:
希望这有帮助,希望我的假设是正确的
-戴夫
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:
Hope this helps and hopefully I assumed correctly
-Dave
似乎在构建树时添加父节点会更容易,但是如果构建了树并且想要向每个节点添加父节点,则可以使用递归。
我会尝试类似的方法
注意:在发布之前我无法测试此代码。
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
NOTE: I was not able to test this code before posting.