相等方法比较两棵树

时间:2018-08-28 10:23:25

标签: java

我最近一直在学习Java。我创造了两棵树。我需要编写代码(等于方法)来比较两棵树,如果它们相同,则输出true或false。

我的代码:

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

第一棵树:

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

第二棵树:

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

2 个答案:

答案 0 :(得分:1)

将以下方法添加到您的Node类。由于树也是一个节点-您应该能够比较两棵树。仅供参考,这是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;
}

答案 1 :(得分:0)

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

然后将这种方法用于两棵树的根节点。