简体   繁体   English

N元树等于

[英]N-ary trees equals

I did this structure to represent an N-ary tree: 我做了这个结构来表示一个N元树:

public class MyTree {
    public int data;
    LinkedList<MyTree> children;


    public MyTree(int data) {
        this.data = data;
        this.children = new LinkedList<>();
    }

    public void addChild(MyTree child) {
        this.children.addFirst(child);
    }

    public boolean equals(MyTree root) { }
}

I also did some other methods,but they aren't the core of this method so I don't show it to you. 我也做了其他一些方法,但是它们不是该方法的核心,因此我不会向您展示。 However let's talk about the method equals: how to check if two tree are equal in both structure and value? 但是,让我们谈谈方法是否相等:如何检查两棵树的结构和值是否相等? I mean they are equal only if: 我的意思是,只有在以下情况下它们才相等:

  8             
 / | \20
9  10     
|
20
 |
 30
 / | \
40 50 70


  8             
 / |\20
9  10     
|
20
 |
 30
/ | \
40 50 70

So my idea is to do two recursion at the same time(one with this.tree and one with the tree in input),when the fuction explore the first node compare it with the first of the second tree and so on(they have to respect the same order and value!) like this: 所以我的想法是同时进行两个递归(一个使用this.tree,另一个使用输入的树),当功能探索第一个节点时,将其与第二个树中的第一个进行比较,依此类推(必须尊重相同的顺序和价值!)像这样:

public boolean equals(MyTree t) {

    boolean result = true;
    if (this == null && t == null) {
        return false;
    }
    if (this == null || t == null) {
        return false;
    }
    if (this.getValue() != t.getValue()) {
        return false;
    }
    if (this.getChildren().size() != t.getChildren().size() ) {
        return false;
    }

    if (this.getChildren().size() == t.getChildren().size() ) {
        for (int i = 0; i < getChildren().size(); i++) {
            MyTree object = t.getChildren().get(i);
            MyTree object1 = this.getChildren().get(i);
            result = object1.equals(object);
            if (result == false) {
                return false;
            }
        }
    }

    return result;
}

But I don't know how to explore at the same time two tree,I did a dfs pre-order for example,but in this method you have to explore two tree at the same time. 但是我不知道如何同时探索两棵树,例如我做了一个dfs的预订,但是在这种方法中,您必须同时探索两棵树。 Can you give me an algorithm to explore two tree at the same time? 您能给我一个算法同时探索两棵树吗? My test: 我的测试:

        MyTree t1 = new MyTree(8);
        t1.addChild(new MyTree(9));
        t1.addChild(new MyTree(10));
        MyTree t2 = new MyTree(20);
        t2.addChild(new MyTree(40));
        t2.addChild(new MyTree(30));
        MyTree t3 = new MyTree(25);
        t3.addChild(new MyTree(80));
        t3.addChild(new MyTree(70));
        t3.addChild(new MyTree(95));
        t2.addChild(t3);
        t1.addChild(t2);

        MyTree t4 = new MyTree(8);
        t4.addChild(new MyTree(9));
        t4.addChild(new MyTree(10));
        MyTree t5 = new MyTree(20);
        t5.addChild(new MyTree(40));
        t5.addChild(new MyTree(30));
        MyTree t6 = new MyTree(25);
        t6.addChild(new MyTree(80));
        t6.addChild(new MyTree(70));
        t6.addChild(new MyTree(95));
        t5.addChild(t6);
        t4.addChild(t5);
        System.out.print(t1.equals(t4));

Recursion is going to be as follow: trees are equals only if corresponding data equals, children size are equals and each children are equals 递归将如下所示:仅当对应数据相等,子项大小相等且每个子项相等时,树才相等

public class MyTree {
public int data;
LinkedList<MyTree> children;


public MyTree(int data) {
    this.data = data;
    this.children = new LinkedList<>();
}

public void addChild(MyTree child) {
    this.children.addFirst(child);
}

public boolean equals(MyTree root) {
    if (root == null || root.children.size() != children.size() || data != root.data) return false;

    Iterator<MyTree> myTreeIterator = children.iterator();
    Iterator<MyTree> rootTreeIterator = root.children.iterator();
    while (myTreeIterator.hasNext() && rootTreeIterator.hasNext()) {
        if (!myTreeIterator.next().equals(rootTreeIterator.next())) return false;
    }
    return true;
}
}

UPD: @Gene advice UPD:@Gene建议

public class MyTree {
public int data;
LinkedList<MyTree> children;


public MyTree(int data) {
    this.data = data;
    this.children = new LinkedList<>();
}

public void addChild(MyTree child) {
    this.children.addFirst(child);
}

@Override
public boolean equals(Object o) {
    return this == o || !(o == null || getClass() != o.getClass()) && equals((MyTree) o);
}

public boolean equals(MyTree root) {
    return !(root == null || data != root.data) && children.equals(root.children);
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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