N-ary树等于

时间:2017-03-26 23:22:16

标签: java algorithm tree

我做了这个结构来代表N-ary树:

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) { }
}

我也做了一些其他方法,但它们不是这种方法的核心,所以我没有向你展示。但是,让我们谈谈方法等于: 如何检查两棵树在结构和值上是否相等?我的意思是他们只有在以下情况下才是平等的:

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


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

所以我的想法是同时进行两次递归(一次使用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;
}

但是我不知道如何同时探索两棵树,例如我做了dfs预订,但是在这种方法中你必须同时探索两棵树。你能给我一个算法来同时探索两棵树吗? 我的测试:

        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));

1 个答案:

答案 0 :(得分:0)

递归将如下:只有当相应的数据等于,子项大小等于且每个子项等于

时,树才是等于的
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建议

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);
}
}