Java structure for mathematical tree with int nodes
In Java, is Tree
the best structure to represent a tree with such properties:
int
s; int d > 0
Operations I need todo:
Operations I do not need:
Properties are just perfect for a Tree
, so maybe there is some super implementation available in terms of performance. XMLTree
or whatever.
Currently I'm using an array of arrays to store elements, but I find it not subtle.
Here is a basic example of a Node
class that could be used to form your tree structure. There is some complexity in writing methods to iterate over all child nodes in a depth-first or breadth-first fashion.
As an alternative you could consider using DefaultMutableTreeNode
which provides these methods for free ( depthFirstEnumeration()
, breadthFirstEnumeration()
). This node implementation also allows you to attach a user object by calling setUserObject(Object)
. The drawback is that the implementation may not be as compact as writing your own structure, so it really depends on the size of your tree.
public class Node {
private final int value;
private final List<Node> children;
public Node(int value) {
this.value = value;
this.children = new LinkedList<Node>();
}
public int getValue() {
return value;
}
public List<? extends Node> getChildren() {
return Collections.unmodifiableList(children);
}
public void addChild(Node child) {
children.add(child);
}
}
链接地址: http://www.djcxy.com/p/39876.html
上一篇: 如何在Java中创建动态树数据结构
下一篇: 具有int节点的数学树的Java结构