1

I have implemented the LinkedBinaryTree structure and I want to make that tree cloneable, yet I couldn't figure out how to insert the Positions into the new tree properly.

enter image description here

Here is my Position interface:

public interface Position<E> extends Cloneable{

  E getElement() throws IllegalStateException;

  Position<E> clone() throws CloneNotSupportedException;
}

This is Node class and its clone method in LinkedBinaryTree class.(Node class also has setters and getters and other stuff. I didn't put them cause they are relevant to topic.).

protected static class Node<E> implements Position<E>, Cloneable {
    public Position<E> clone(){
        try {
            return (Position<E>)super.clone();
        }catch(CloneNotSupportedException e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }
}

These are the methods for traversal in AbstractBinaryTree class:

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

And this is the clone method of LinkedBinaryTree(I know it is not finished yet):

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;

}

I can get the Positions with inorder method and clone every single Position but I don't know how to insert them into the new tree with the same order.

M.Soyturk
  • 340
  • 3
  • 14
  • Basically your clone method should take care of inserting elements into proper positions, right ? Can you post the code for clone method as well here – zenwraight Jan 05 '18 at 22:18
  • I have already put all my clone methods. I think I need to do that in the clone method of LinkedBinaryTree. – M.Soyturk Jan 05 '18 at 22:33
  • No my point was where the this clone method - super.clone() .. the parent clone method which gets called... so a clone method should be recursive in nature.... – zenwraight Jan 05 '18 at 22:35
  • Well I don’t have that. Apperantly, I’m missing something about cloneable logic. – M.Soyturk Jan 06 '18 at 07:19
  • My LinkedBinaryTree class extends AbstractBinaryTree class but AbstractBinaryTree doesn't have any clone method and it doesn't implement Cloneable interface, yet when i write (LinkedBinaryTree)super.clone() it doesn't give any error. Conversly, it deep copies the LinkedBinary tree but it doesn't copy its nodes. I'm really confused now. – M.Soyturk Jan 06 '18 at 07:58

1 Answers1

1

I solved the problem and I'm putting it here for those of you who are wondering. I added two methods which basically gets the old tree's root and elements and adds them to the new tree's root and returns the new root.

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

}
M.Soyturk
  • 340
  • 3
  • 14