简体   繁体   English

java中的特定树数据结构

[英]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 . 所以我创建了两个类: BoardNode

  • Each Board consists of an ArrayList of Nodes : 每个Board包含一个NodesArrayList

     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: 回答你的问题:

  • The Node passed as pNode should be used; 应该使用作为pNode传递的Node ; you don't need to create a new one. 你不需要创建一个新的。
  • The Board passed as pBoard should be used; Board通过,应该使用pBoard ; you don't need to create a new one. 你不需要创建一个新的。
  • A leaf node has a null 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM