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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…