如何在java中创建自己的树? [英] How to create own tree in java?
本文介绍了如何在java中创建自己的树?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道如何在java中创建自己的树,它由八个子节点组成,并且在每个子节点中它有许多子节点.如何创建这个.请帮我.我是 Java 新手.
I want to know how to create own tree in java, it consists of eight sub-nodes and in each sub-node it having many sub-nodes. How to create this. please help me. I am newer to java.
推荐答案
您可能需要创建某种 Node 类来表示树中的节点:
You'll probably need to create some sort of Node class to represent the nodes in the tree:
public class Node
{
private List<Node> children = null;
private String value;
public Node(String value)
{
this.children = new ArrayList<>();
this.value = value;
}
public void addChild(Node child)
{
children.add(child);
}
}
然后填充您的树:
public static void main(String [] args)
{
Node root = new Node("root");
root.addChild(new Node("child1"));
root.addChild(new Node("child2")); //etc.
}
您必须修改它以适合您自己的目的,此代码只是为了让您了解结构.
You'll have to modify this to suit your own purposes, this code is just to give you an idea of the structure.
这篇关于如何在java中创建自己的树?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文