Java 使LinkedBinaryTree可克隆

Java 使LinkedBinaryTree可克隆,java,algorithm,data-structures,cloning,cloneable,Java,Algorithm,Data Structures,Cloning,Cloneable,我已经实现了LinkedBinaryTree结构,我想使该树可克隆,但我无法弄清楚如何将位置正确地插入到新树中 这是我的职位界面: public interface Position<E> extends Cloneable{ E getElement() throws IllegalStateException; Position<E> clone() throws CloneNotSupportedException; } 公共接口位置扩展可克隆性{

我已经实现了LinkedBinaryTree结构,我想使该树可克隆,但我无法弄清楚如何将位置正确地插入到新树中

这是我的职位界面:

public interface Position<E> extends Cloneable{

  E getElement() throws IllegalStateException;

  Position<E> clone() throws CloneNotSupportedException;
}
公共接口位置扩展可克隆性{
E getElement()抛出IllegalStateException;
Position clone()抛出CloneNotSupportedException;
}
这是LinkedBinaryTree类中的Node类及其克隆方法。(Node类也有setter和getter以及其他东西。我没有放它们,因为它们与主题相关。)

受保护的静态类节点实现位置,可克隆{
公共位置克隆(){
试一试{
返回(位置)super.clone();
}捕获(CloneNotSupportedException e){
e、 printStackTrace();
抛出新的RuntimeException();
}
}
}
以下是AbstractBinaryTree类中的遍历方法:

private void inorderSubtree(Position<E> p, List<Position<E>> snapshot) {
  if (left(p) != null) {
      inorderSubtree(left(p), snapshot);
  }
  snapshot.add(p);
  if (right(p) != null) {
      inorderSubtree(right(p), snapshot);
  }
}

public Iterable<Position<E>> inorder() {
  List<Position<E>> snapshot = new ArrayList<>();
  if (!isEmpty())
    inorderSubtree(root(), snapshot);   // fill the snapshot recursively
return snapshot;
}
订单子树中的私有void(位置p,列表快照){ 如果(左(p)!=null){ 索引子树(左(p),快照); } snapshot.add(p); 如果(右(p)!=null){ 索引子树(右(p),快照); } } 公共Iterable索引(){ 列表快照=新建ArrayList(); 如果(!isEmpty()) inorderSubtree(root(),snapshot);//递归地填充快照 返回快照; } 这是LinkedBinaryTree的克隆方法(我知道它还没有完成):

public LinkedBinaryTree clone()引发CloneNotSupportedException{
LinkedBinaryTree clonedTree=新建LinkedBinaryTree();
ArrayList a=(ArrayList)顺序();
Position clonePosition=a.get(0.clone();
返回克隆树;
}

我可以用inoorder方法获得位置并克隆每个位置,但我不知道如何将它们以相同的顺序插入到新树中

我解决了这个问题,我把它放在这里是为了那些想知道的人。我添加了两个方法,基本上获取旧树的根和元素,然后将它们添加到新树的根并返回新根

    Node<E> cloneTree(Position<E> root) {
        Node<E> validatedRoot = validate(root);
        Node<E> n1 = new Node<E>(null,null,null,null);
        n1.setElement(validatedRoot.getElement());
        cloneTrack++;
        cloneTree(validatedRoot, n1);
        return n1;
}

void cloneTree(Node<E> root, Node<E> newRoot) {
        if (root == null) {
            return;
        }
        if (root.left != null) {
            newRoot.setLeft(new Node<E>(null,null,null,null));
            newRoot.getLeft().setElement(root.getLeft().getElement());
            cloneTrack++;
            cloneTree(root.left, newRoot.left);
        }
        if (root.right != null) {
            newRoot.setRight(new Node<E>(null,null,null,null));
            newRoot.getRight().setElement(root.getRight().getElement());
            cloneTrack++;
            cloneTree(root.right, newRoot.right);
        }

}
节点克隆树(位置根){
节点validatedRoot=验证(根);
节点n1=新节点(null,null,null,null);
n1.setElement(validatedRoot.getElement());
cloneTrack++;
克隆树(validatedRoot,n1);
返回n1;
}
void cloneTree(节点根、节点新根){
if(root==null){
返回;
}
if(root.left!=null){
setLeft(新节点(null,null,null,null));
newRoot.getLeft().setElement(root.getLeft().getElement());
cloneTrack++;
克隆树(root.left,newRoot.left);
}
if(root.right!=null){
setRight(新节点(null,null,null,null));
newRoot.getRight().setElement(root.getRight().getElement());
cloneTrack++;
克隆树(root.right,newRoot.right);
}
}

基本上,克隆方法应该注意将元素插入正确的位置,对吗?你能把克隆方法的代码也贴在这里吗?我已经把我所有的克隆方法都放在这里了。我想我需要在LinkedBinaryTree的克隆方法中这样做。。被调用的父克隆方法。。。所以克隆方法本质上应该是递归的……我没有。显然,我缺少了一些关于可克隆逻辑的东西。我的LinkedBinaryTree类扩展了AbstractBinaryTree类,但AbstractBinaryTree没有任何克隆方法,也没有实现可克隆接口,但是当我编写(LinkedBinaryTree)super.clone()时,它没有给出任何错误。相反,它深度复制LinkedBinary树,但不复制其节点。我现在真的很困惑。
public LinkedBinaryTree<E> clone() throws CloneNotSupportedException{

  LinkedBinaryTree<E> clonedTree = new LinkedBinaryTree<E>();

  ArrayList<Position<E>> a = (ArrayList<Position<E>>) inorder();

  Position<E> clonePosition = a.get(0).clone();

return clonedTree;

}
    Node<E> cloneTree(Position<E> root) {
        Node<E> validatedRoot = validate(root);
        Node<E> n1 = new Node<E>(null,null,null,null);
        n1.setElement(validatedRoot.getElement());
        cloneTrack++;
        cloneTree(validatedRoot, n1);
        return n1;
}

void cloneTree(Node<E> root, Node<E> newRoot) {
        if (root == null) {
            return;
        }
        if (root.left != null) {
            newRoot.setLeft(new Node<E>(null,null,null,null));
            newRoot.getLeft().setElement(root.getLeft().getElement());
            cloneTrack++;
            cloneTree(root.left, newRoot.left);
        }
        if (root.right != null) {
            newRoot.setRight(new Node<E>(null,null,null,null));
            newRoot.getRight().setElement(root.getRight().getElement());
            cloneTrack++;
            cloneTree(root.right, newRoot.right);
        }

}