如何解决如何在java中添加一个可以比较两个树结构的函数?
我创建了一个树数据结构,但我无法添加两种方法来获取不同的节点和相同的节点。
** 是相似节点,* 是不同节点。
树结构 1
>23720634 (Root)
**24751368**
**24751324**
**24751324**
**23726962**
**24751382**
**23728776**
**23724832**
**23727632**
**23728875**
**23728813**
*23722966*
**24751324**
**17712706**
树结构 2
>25764379 (Root)
**24751368**
**24751324**
**24751324**
**23726962**
**24751382**
**23728776**
**23724832**
**23727632**
**23728875**
**23728813**
*24742015*
*24763621*
**24751324**
**17712706**
我对树的实现
import org.jetbrains.annotations.NotNull;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
/**
* @param <T>
*/
class TreeNode<T> implements Iterable<TreeNode<T>> {
private final List<TreeNode<T>> elementsIndex;
public T data;
public TreeNode<T> parent;
public List<TreeNode<T>> children;
/**
* @param data =--> data type holder
*/
public TreeNode(T data) {
this.data = data;
this.children = new LinkedList<>();
this.elementsIndex = new LinkedList<>();
this.elementsIndex.add(this);
}
/**
* @param depth --> tree depth
* @return Indentation
*/
private static String createIndent(int depth) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < depth; i++) {
sb.append('-');
}
sb.append(">");
return sb.toString();
}
/**
* @param <T> ---> tree data type
* @param node --> tree leaves
* @param appender --> add tree values
*/
public static <T> void printTree(TreeNode<T> node,String appender) {
//System.out.println(appender + node.getData());
node.getChildren().forEach(each -> printTree(each,appender + appender));
}
/**
* @param child --> tree children
* @return child
*/
public TreeNode<T> addChild(TreeNode<T> child) {
child.setParent(this);
this.children.add(child);
return child;
}
/**
* @param children --> tree children
*/
public void addChildren(List<TreeNode<T>> children) {
children.forEach(each -> each.setParent(this));
this.children.addAll(children);
}
/**
* @return data
*/
public T getData() {
return data;
}
/**
* @param data --> tree data type
*/
public void setData(T data) {
this.data = data;
}
/**
* @return parent
*/
public TreeNode<T> getParent() {
return parent;
}
/**
* @param parent --> tree parent
*/
private void setParent(TreeNode<T> parent) {
this.parent = parent;
}
/**
* @return children list
*/
public List<TreeNode<T>> getChildren() {
return children;
}
/**
* @return parent
*/
public boolean isRoot() {
return parent == null;
}
/**
* @return check the size of child
*/
public boolean isLeaf() {
return children.size() == 0;
}
/**
* @param child --> tree children
* @return child node
*/
public TreeNode<T> addChild(T child) {
TreeNode<T> childNode = new TreeNode<>(child);
childNode.parent = this;
this.children.add(childNode);
this.registerChildForSearch(childNode);
return childNode;
}
/**
* @return the depth of the level
*/
public int getLevel() {
if (this.isRoot())
return 0;
else
return parent.getLevel() + 1;
}
/**
* @param node --> tree leaves
*/
private void registerChildForSearch(TreeNode<T> node) {
elementsIndex.add(node);
if (parent != null)
parent.registerChildForSearch(node);
}
public void deleteNode() {
if (parent != null) {
int index = this.parent.getChildren().indexOf(this);
this.parent.getChildren().remove(this);
for (TreeNode<T> each : getChildren()) {
each.setParent(this.parent);
}
this.parent.getChildren().addAll(index,this.getChildren());
} else {
deleteRootNode();
}
this.getChildren().clear();
}
/**
* @return parent after deleting
*/
public TreeNode<T> deleteRootNode() {
if (parent != null) {
throw new IllegalStateException("deleteRootNode not called on root");
}
TreeNode<T> newParent = null;
if (!getChildren().isEmpty()) {
newParent = getChildren().get(0);
newParent.setParent(null);
getChildren().remove(0);
for (TreeNode<T> each : getChildren()) {
each.setParent(newParent);
}
newParent.getChildren().addAll(getChildren());
}
this.getChildren().clear();
return newParent;
}
/**
* @return root
*/
public TreeNode getRoot() {
if (parent == null) {
return this;
}
return parent.getRoot();
}
/**
* @param cmp --> comparing the tree variable
* @return finds the element
*/
public TreeNode<T> findTreeNode(Comparable<T> cmp) {
for (TreeNode<T> element : this.elementsIndex) {
T elData = element.data;
if (cmp.compareto(elData) == 0)
return element;
}
return null;
}
/**
* @return the root
*/
@Override
public String toString() {
return data != null ? data.toString() : "[data null]";
}
/**
* @return iterator
*/
@Override
public @NotNull Iterator<TreeNode<T>> iterator() {
return new TreeNodeIterator<>(this);
}
/**
* @param treeRoot --> tree root
* @param data --> tree data type
* @return the data else null if not found
*/
public TreeNode<T> search(TreeNode<T> treeRoot,T data) {
Comparable<T> searchCriteria = treeData -> {
boolean nodeOk = treeData.equals(data);
return nodeOk ? 0 : 1;
};
return treeRoot.findTreeNode(searchCriteria);
}
/**
* @param treeRoot --> tree root
* @return the whole tree
*/
public StringBuilder display(TreeNode<T> treeRoot) {
StringBuilder print = new StringBuilder();
print.append('\n');
for (TreeNode<T> node : treeRoot) {
String indent = createIndent(node.getLevel());
print.append(indent).append(node.data);
print.append("\n|");
}
if (print.length() > 0)
print.deleteCharat(print.length() - 1);
return print;
}
}
/**
* @param <T>
*/
class TreeNodeIterator<T> implements Iterator<TreeNode<T>> {
private final TreeNode<T> treeNode;
private final Iterator<TreeNode<T>> childrenCurNodeIterator;
private Processstages doNext;
private TreeNode<T> next;
private Iterator<TreeNode<T>> childrenSubNodeIterator;
/**
* @param treeNode --> tree children
*/
public TreeNodeIterator(TreeNode<T> treeNode) {
this.treeNode = treeNode;
this.doNext = Processstages.Processparent;
this.childrenCurNodeIterator = treeNode.children.iterator();
}
/**
* @return true if there is nay element
*/
@Override
public boolean hasNext() {
if (this.doNext == Processstages.Processparent) {
this.next = this.treeNode;
this.doNext = Processstages.ProcessChildCurNode;
return true;
}
if (this.doNext == Processstages.ProcessChildCurNode) {
if (childrenCurNodeIterator.hasNext()) {
TreeNode<T> childDirect = childrenCurNodeIterator.next();
childrenSubNodeIterator = childDirect.iterator();
this.doNext = Processstages.ProcessChildSubNode;
return hasNext();
} else {
this.doNext = null;
return false;
}
}
if (this.doNext == Processstages.ProcessChildSubNode) {
if (childrenSubNodeIterator.hasNext()) {
this.next = childrenSubNodeIterator.next();
return true;
} else {
this.next = null;
this.doNext = Processstages.ProcessChildCurNode;
return hasNext();
}
}
return false;
}
/**
* @return the next element
*/
@Override
public TreeNode<T> next() {
return this.next;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
enum Processstages {
Processparent,ProcessChildCurNode,ProcessChildSubNode
}
}
**24751368**
**24751324**
**24751324**
**23726962**
**24751382**
**23728776**
**23724832**
**23727632**
**23728875**
**23728813**
**24751324**
**17712706**
Nodes in Structure 1
*23722966*
Nodes in Structure 2
*24742015*
*24763621*
解决方法
请告诉我更好的方法?
public static void main(String[] args) throws Exception{
Path p1 = Path.of("23720634","24751368","24751324","23726962","24751382","23728776","23724832","23727632","23728875","23728813","23722966","17712706");
Path p2 = Path.of("25764379","24742015","24763621","17712706");
comprec(p1,p2);
}
private static void comprec(Path p1,Path p2){
for(int x1 = 1,x2 = 1,t = 0; ; t++){
try{
if(p1.getName(x1).equals(p2.getName(x2))){
System.out.println("Same Nodes: " + p1.getName(x1++));
x2++;
}else if(p1.getName(x1).equals(p2.getName(x2 + t))){
System.out.println("Nodes in Structure 2: " + p2.getName(x2++));
}else if(p1.getName(x1 + t).equals(p2.getName(x2))){
System.out.println("Nodes in Structure 1: " + p1.getName(x1++));
}else if(p1.getName(x1 + t).equals(p2.getName(x2 + t))){
throw new IllegalArgumentException();
}else continue;
}catch(IllegalArgumentException e){
if(p1.getNameCount() == x1 || p2.getNameCount() == x2){
for(int i = x1; i < p1.getNameCount(); i++)
System.out.println("Nodes in Structure 1: " + p1.getName(i));
for(int i = x2; i < p2.getNameCount(); i++)
System.out.println("Nodes in Structure 2: " + p2.getName(i));
break;
}else{
System.out.println("Nodes in Structure 1: " + p1.getName(x1++));
System.out.println("Nodes in Structure 2: " + p2.getName(x2++));
}
}
t = -1;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。