简体   繁体   中英

Equals method to compare two trees

I have been learning java recently. And I created two trees. I need to write code(equals method) to compare two trees and if they are the same, then output true or false.

My Code:

public class TreePrint {

    public static void main(String[] args) {

        Tree<String> rootFolder = new Tree<>("RootFolder");

        Node<String> video = rootFolder.addChild("Video");
        Node<String> music = rootFolder.addChild("Music");
        Node<String> picture = rootFolder.addChild("Picture");

        video.addChild("Terminator");
        video.addChild("Die Hard");
        video.addChild("Rocky");
        music.addChild("Eminem");
        Node<String> picture01 = picture.addChild("Picasso");
        picture01.addChild("Do Vinci");
        Node<String> picture02 = picture01.addChild("Adil");
        picture02.addChild("Cartoon");
        picture02.addChild("Comics");

         Tree2<String> rootFolder1 = new Tree2<>("RootFolder1");

        Node<String> video1 = rootFolder1.addChild("Video");
        Node<String> music1 = rootFolder1.addChild("Music");
        Node<String> picture1 = rootFolder1.addChild("Picture");

        video1.addChild("Terminator");
        video1.addChild("Die Hard");
        video1.addChild("Rocky");
        music1.addChild("Eminem");
        Node<String> picture001 = picture1.addChild("Picasso");
        picture001.addChild("Do Vinci");
        Node<String> picture002 = picture001.addChild("Adil");
        picture002.addChild("Cartoon");
        picture002.addChild("Comics");


        printTree(rootFolder);
        printTree(rootFolder1);


        boolean b1 = rootFolder.contains("P0");
        System.out.println(b1);
        boolean b2 = rootFolder1.contains("Eminem");
        System.out.println(b2);
    }

    private static <T> void printTree(Node<T> node) {
        printTree(node, 0);
    }

    private static <T> void printTree(Node<T> node, int level) {
        printNode(node, level);
        if (node.getChildren() != null) {
            for (Node childNode : node.getChildren()) {
                printTree(childNode, level + 1);
            }
        }
    }

    private static <T> void printNode(Node<T> kid, int level) {

        for (int i = 0; i < level; i++) {
            System.out.print("  ");
        }

        System.out.println(kid.getData());
    }
}

first tree:

public class Tree<T> extends Node<T> {

    public Tree(T data) {

        super(data, null);
    }
    public boolean contains(T value) {
        return recurse(iterate(), value);
    }

    private boolean recurse(List<Node<T>> children, T value) {
        return children.stream()
                .anyMatch(item -> item.getData().equals(value) || item.iterate().size() > 0 && recurse(item.iterate(), value));
    }


}

public class Node<T> {
    private T data;
    private final List<Node<T>> children = new ArrayList<>();
    private final Node<T> parent;


    public Node(T data, Node<T> parent) {
        this.data = data;
        this.parent = parent;
    }

    public void addChild(Node<T> node) {
        children.add(node);
    }


    public Node<T> addChild(T nodeData) {

        Node<T> newNode = new Node<T>( nodeData, this ); 
        children.add( newNode );
        return newNode;
    }


    public List<Node<T>> iterate() {
        return children;
    }

    public void remove(Node<T> node) {
        children.remove(node);
    }

    public List<Node<T>> getChildren() {
        return children;
    }

    public Node getParent() {
        return parent;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

Second Tree:

public class Tree2<T> extends Node<T> {

    public Tree2(T data) {

        super(data, null);
    }
    public boolean contains(T value) {
        return recurse(iterate(), value);
    }

    private boolean recurse(List<Node<T>> children, T value) {
        return children.stream()
                .anyMatch(item -> item.getData().equals(value) || item.iterate().size() > 0 && recurse(item.iterate(), value));
    }
}

Add the following method to your Node class. Since your tree is also a node - you should be able to compare two trees. FYI, This is autogenerated by Eclipse.

public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Node other = (Node) obj;
    if (children == null) {
        if (other.children != null)
            return false;
    } else if (!children.equals(other.children))
        return false;
    if (data == null) {
        if (other.data != null)
            return false;
    } else if (!data.equals(other.data))
        return false;
    return true;
}
/* Given two trees, return true if they are
   structurally identical */
boolean identicalTrees(Node a, Node b) 
{
    /*1. both empty */
    if (a == null && b == null)
        return true;

    /* 2. both non-empty -> compare them */
    if (a != null && b != null) 
        return (a.data == b.data
                && identicalTrees(a.left, b.left)
                && identicalTrees(a.right, b.right));

    /* 3. one empty, one not -> false */
    return false;
}

then just use this method for the root nodes of your two trees.

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