Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
264 views
in Technique[技术] by (71.8m points)

java - 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));
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...