简体   繁体   中英

N-ary trees equals

I did this structure to represent an N-ary tree:

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:

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. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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