Thursday, 8 August 2013

Specific tree data structure in java

Specific tree data structure in java

I'm trying to implement a specific tree data structure in 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.
Each Board consists of an ArrayList of Nodes:
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:
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?
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?
How do I indicate a leaf Node? I tried to initialize it with null, but I
got a NullPointerException
Thank you!

No comments:

Post a Comment