[英]Specific tree data structure in java
I'm trying to implement a specific tree data structure in Java; 我正在尝试用Java实现特定的树数据结构; I don't know exactly what type of tree is this.
我不确切知道这是什么类型的树。 Here's an example of what I'm trying to do:
这是我正在尝试做的一个例子:
-----------------------------
| Board |
|
| Node1 Node2 Node3 Node4 |
| _ _ _ _
| |_| |_| |_| |_| |
---+------+------+------+----
| | | |
/ | | \
-------------- --- --- -----------
| Board | - - | Board |
| |
| Node1 Node2 | | Node1 ... |
| _ _ | _
| |_| |_| | | |_| ... |
---+------+--- ---+------+
/ | |
. . .
. . .
. . .
So I created two classes: Board
and Node
. 所以我创建了两个类:
Board
和Node
。
Each Board
consists of an ArrayList
of Nodes
: 每个
Board
包含一个Nodes
的ArrayList
:
public class Board { ArrayList<Node> mContent; Board() { mContent = new ArrayList<Node>(); } Board(Board pBoard) { mContent = new ArrayList<Node>(pBoard.mContent); } void add(Node pNode) { mContent.add(pNode); } void add(String pString, Board pBoard) { Node tNode = new Node(pString, pBoard); mContent.add(tNode); } }
Each Node
consists of a String
and a reference to another Board
: 每个
Node
由一个String
和对另一个Board
的引用组成:
public class Node { String mLabel; Board mBoard; Node(){ mLabel = new String(); mBoard = null; } Node(String pLabel, Board pBoard){ mLabel = new String(pLabel); mBoard = new Board(Board); } void setBoard(Board pBoard){ mBoard = pBoard; } }
My questions are: 我的问题是:
On the add(Node pNode)
method, do I need to create a new Node
and then add it to the ArrayList
? 在
add(Node pNode)
方法中,我是否需要创建一个新Node
然后将其添加到ArrayList
?
On the setBoard(...)
method, do I need to create a new Board
and then pass it to mBoard
, or just doing mBoard = pBoard
is correct? 在
setBoard(...)
方法中,我是否需要创建一个新的Board
然后将其传递给mBoard
,或者只是做mBoard = pBoard
是否正确?
How do I indicate a leaf Node
? 如何指示叶
Node
? I tried to initialize it with null
, but I got a NullPointerException
我尝试用
null
初始化它,但是我得到了一个NullPointerException
Thank you! 谢谢!
To answer your question: 回答你的问题:
Node
passed as pNode
should be used; pNode
传递的Node
; you don't need to create a new one. Board
passed as pBoard
should be used; Board
通过,应该使用pBoard
; you don't need to create a new one. mBoard
. mBoard
。 Alternatively, it could have an mBoard
that has an empty mContent
array. mBoard
,它有一个空的mContent
数组。 The choice depends on information you have not posted about how you want your trees to bottom out. You might consider collapsing your structure. 您可以考虑折叠您的结构。 It's not clear why you don't just have a single
Node
class (with an ArrayList<Node> mContent
field in place of the mBoard field). 目前尚不清楚为什么你不只有一个
Node
类(使用ArrayList<Node> mContent
字段代替mBoard字段)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.