Java 用于相互比较树的树结构
我正在寻找一棵最适合我的用例的树。树应包含硬件类型的元素,包括变量ElementType: 节点应以硬件元素的ElementType命名: 一旦我建立了几棵树(如上面的一棵),我想将它们相互比较,并检查一棵树是否是另一棵树的一部分。例如,应该将上面的树与下面的树进行比较,结果应该会告诉我,第一棵树是第二棵树的一部分。应通过其节点名(ElementType)对树进行比较,因为每个树的基础对象不同:Java 用于相互比较树的树结构,java,data-structures,tree,comparison,treenode,Java,Data Structures,Tree,Comparison,Treenode,我正在寻找一棵最适合我的用例的树。树应包含硬件类型的元素,包括变量ElementType: 节点应以硬件元素的ElementType命名: 一旦我建立了几棵树(如上面的一棵),我想将它们相互比较,并检查一棵树是否是另一棵树的一部分。例如,应该将上面的树与下面的树进行比较,结果应该会告诉我,第一棵树是第二棵树的一部分。应通过其节点名(ElementType)对树进行比较,因为每个树的基础对象不同: java中有适合我的需求的树结构吗?据我所知,没有结构,但很容易实现 public class
java中有适合我的需求的树结构吗?据我所知,没有结构,但很容易实现
public class Tree {
private Node root;
public boolean containsSubTree(Tree other) {
return root.containsSubTree(other.root);
}
}
public class Node {
private Node parent;
private ElementType elementType;
private List<Node> children = new ArrayList<Node>();
public Node(Node parent, ElementType elementType) {
...
}
public void addChild(Node child) {
children.add(child);
}
protected boolean equalsIgnoreParent(Node other) {
if (elementType != other.elementType) return false;
if (children.size() != other.children.size()) return false;
for (int i = 0; i < children.size(); ++ i) {
// recursive step
if (!children.get(i).equalsIgnoreParent(other.children.get(i)) {
return false;
}
}
return true;
}
public boolean containsSubTree(Node other) {
if (equalsIgnoreParent(other)) return true;
for (Node child : children) {
// recursive step
if (child.containsSubTree(other)) return true;
}
return false;
}
}
公共类树{
私有节点根;
公共布尔containsSubTree(树其他){
返回root.containsSubTree(other.root);
}
}
公共类节点{
私有节点父节点;
私有元素类型元素类型;
private List children=new ArrayList();
公共节点(节点父节点,ElementType ElementType){
...
}
公共void addChild(节点子节点){
添加(child);
}
受保护的布尔equalsIgnoreParent(节点其他){
if(elementType!=other.elementType)返回false;
if(children.size()!=other.children.size())返回false;
for(int i=0;i
然后调用tree1.containsSubTree(tree2)进行检查
如果您想忽略子项的顺序,您可能会将子项存储为,这将需要一个适当的
我的解决方案是用可以产生深度的方法来实现的。我确信它可以在没有递归的情况下实现……我只是不介意:)据我所知,没有结构,但它很容易实现
public class Tree {
private Node root;
public boolean containsSubTree(Tree other) {
return root.containsSubTree(other.root);
}
}
public class Node {
private Node parent;
private ElementType elementType;
private List<Node> children = new ArrayList<Node>();
public Node(Node parent, ElementType elementType) {
...
}
public void addChild(Node child) {
children.add(child);
}
protected boolean equalsIgnoreParent(Node other) {
if (elementType != other.elementType) return false;
if (children.size() != other.children.size()) return false;
for (int i = 0; i < children.size(); ++ i) {
// recursive step
if (!children.get(i).equalsIgnoreParent(other.children.get(i)) {
return false;
}
}
return true;
}
public boolean containsSubTree(Node other) {
if (equalsIgnoreParent(other)) return true;
for (Node child : children) {
// recursive step
if (child.containsSubTree(other)) return true;
}
return false;
}
}
公共类树{
私有节点根;
公共布尔containsSubTree(树其他){
返回root.containsSubTree(other.root);
}
}
公共类节点{
私有节点父节点;
私有元素类型元素类型;
private List children=new ArrayList();
公共节点(节点父节点,ElementType ElementType){
...
}
公共void addChild(节点子节点){
添加(child);
}
受保护的布尔equalsIgnoreParent(节点其他){
if(elementType!=other.elementType)返回false;
if(children.size()!=other.children.size())返回false;
for(int i=0;i
然后调用tree1.containsSubTree(tree2)进行检查
如果您想忽略子项的顺序,您可能会将子项存储为,这将需要一个适当的
我的解决方案是用可以产生深度的方法实现的。我确信它可以在没有递归的情况下实现……我只是不觉得麻烦:)我在另一个答案中的想法是错误的。下面是另一种尝试:
public class Node {
// LinkedHashMap preserves order in which nodes were added
private Map<ElementType, Node> childMap = new LinkedHashMap<ElementType, Node>();
private ElementType elementType;
public void addChild(Node node) {
if (childMap.containsKey(node.elementType)) {
throw new IllegalStateException("Duplicates not allowed");
}
childMap.put(node.elementType, node);
}
public Collection<Node> getChildren() {
return childMap.values();
}
/**
* Ensure all nodes in other are in this node.
* Ignore any extra elements in this that are not in other
*/
protected boolean isSameIgnoreExtras(Node other) {
if (this.elementType != other.elementType) return false;
if (this.childMap.size() < other.childMap.size()) return false;
for (Node otherChild : other.getChildren()) {
Node thisChild = childMap.get(otherChild.elementType);
if (thisChild == null) {
return false;
}
// recursive step
if (!thisChild.isSameIgnoreExtras(otherChild)) {
return false;
}
}
return true;
}
public boolean containsSubTree(Node other) {
if (isSameIgnoreExtras(other)) {
return true;
}
for (Node child : getChildren()) {
// recursive step
if (child.containsSubTree(other)) {
return true;
}
}
return false;
}
}
公共类节点{
//LinkedHashMap保留添加节点的顺序
private Map childMap=新建LinkedHashMap();
私有元素类型元素类型;
公共void addChild(节点){
if(childMap.containsKey(node.elementType)){
抛出新的非法状态异常(“不允许重复”);
}
childMap.put(node.elementType,node);
}
公共集合getChildren(){
返回childMap.values();
}
/**
*确保其他节点中的所有节点都在此节点中。
*忽略此文件中不在其他文件中的任何额外元素
*/
受保护的布尔值isSameIgnoreExtras(节点其他){
如果(this.elementType!=other.elementType)返回false;
if(this.childMap.size()
我在另一个答案中的想法是错误的。下面是另一种尝试:
public class Node {
// LinkedHashMap preserves order in which nodes were added
private Map<ElementType, Node> childMap = new LinkedHashMap<ElementType, Node>();
private ElementType elementType;
public void addChild(Node node) {
if (childMap.containsKey(node.elementType)) {
throw new IllegalStateException("Duplicates not allowed");
}
childMap.put(node.elementType, node);
}
public Collection<Node> getChildren() {
return childMap.values();
}
/**
* Ensure all nodes in other are in this node.
* Ignore any extra elements in this that are not in other
*/
protected boolean isSameIgnoreExtras(Node other) {
if (this.elementType != other.elementType) return false;
if (this.childMap.size() < other.childMap.size()) return false;
for (Node otherChild : other.getChildren()) {
Node thisChild = childMap.get(otherChild.elementType);
if (thisChild == null) {
return false;
}
// recursive step
if (!thisChild.isSameIgnoreExtras(otherChild)) {
return false;
}
}
return true;
}
public boolean containsSubTree(Node other) {
if (isSameIgnoreExtras(other)) {
return true;
}
for (Node child : getChildren()) {
// recursive step
if (child.containsSubTree(other)) {
return true;
}
}
return false;
}
}
公共类节点{
//LinkedHashMap保留添加节点的顺序
private Map childMap=新建LinkedHashMap();
私有元素类型元素类型;
公共void addChild(节点){
if(childMap.containsKey(node.elementType)){
抛出新的非法状态异常(“不允许重复”);
}
childMap.put(node.elementType,node);
}
公共集合getChildren(){
返回childMap.values();
}
/**
*确保其他节点中的所有节点都在此节点中。
*忽略此文件中不在其他文件中的任何额外元素
*/
受保护的布尔值isSameIgnoreExtras(节点其他){