gpt4 book ai didi

java - 两棵树中的 equals 方法

转载 作者:行者123 更新时间:2023-12-02 01:57:53 27 4
gpt4 key购买 nike

伙计们,我创建了两棵树。我写了一个方法,但它不能正常工作。我的方法不能递归工作,只打印“false”。一般来说,我需要递归树的元素,如果两棵树相似,那么它应该输出 - “true”。如果两个 rees 不相似,那么它应该输出 - false。请帮我在我的树中编写代码 equals 方法

我的代码:

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("NN");
picture02.addChild("Cartoon");
picture02.addChild("Comics");

Tree2<String> rootFolder1 = new Tree2<>("RootFolder");
printTree(rootFolder);
printTree(rootFolder1);


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

System.out.println(rootFolder.equals(rootFolder1));

}

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 boolean equals(Object obj) {
return isEquals(obj);
}

private boolean isEquals(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;
}
}
<小时/>
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));
}
}

最佳答案

问题是这一行

if (getClass() != obj.getClass()) {
return false;
}

因为一个对象属于 Tree 类,另一个对象属于 Tree2

关于java - 两棵树中的 equals 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52070319/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com