title: Java deep copy - wrong reference assignment
body: <p>Please consider the following code</p>
  
  <pre><code>public class Pair implements Cloneable, Comparable&lt;Para&gt; {
  
      ...
  
      public Pair(String key, double value) throws Exception {
          if (value &lt;= 0) throw new IllegalArgumentException();
  
          this.key = key;
          this.value = value;
      }
  
      ...
  
      @Override
      public Pair clone() throws CloneNotSupportedException {
          return (Pair) super.clone();
      }
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>public class BSTtree implements Dictionary, Cloneable {
  
      ...
  
      private class Node implements Cloneable, Comparable&lt;Node&gt; {
          Para elem;
          Node left = null;
          Node right = null;
          Node parent = null;
  
          Node () {
              this.elem = null;
          }
  
          Node (Pair elem) {
              this.elem = elem;
          }
  
          ...
  
          @Override
          public Node clone() throws CloneNotSupportedException {
              Node cloned = new Node();
  
              cloned.elem = elem.clone();
              if (left != null) cloned.left = left.clone();
              if (right != null) cloned.right = right.clone();
  
              return cloned;
          }
      }
  
      ...
  
      @Override
      public BSTtree clone() throws CloneNotSupportedException {
          BSTtree cloned = new BSTtree();
          cloned.root = root.clone();
  
          return cloned;
      }
  }
  </code></pre>
  
  <p>I'm trying to implement my own BST tree having <code>Cloneable</code> interface. Where I have a problem is</p>
  
  <pre><code>BSTtree alfa = new BSTtree();
  ...
  BSTtree beta = alfa.clone();
  </code></pre>
  
  <p>which I assume don't correctly set references to trees <code>alfa</code> and <code>beta</code>. Why do I think so? Take look at following</p>
  
  <pre><code>alfa.printTree(); //(VOID  1,00), (a  1,00), (b  2,00), (c  3,00), (e  5,00)
  beta.printTree(); //(VOID  1,00), (a  1,00), (b  2,00), (c  3,00), (e  5,00)
  
  beta.remove("a");
  
  alfa.printTree(); //(VOID  1,00), (b  2,00), (c  3,00), (e  5,00)
  beta.printTree(); //(VOID  1,00), (a  1,00), (b  2,00), (c  3,00), (e  5,00)
  
  alfa.remove("e");
  
  alfa.printTree(); //(VOID  1,00), (b  2,00), (c  3,00), (e  5,00)
  beta.printTree(); //(VOID  1,00), (a  1,00), (b  2,00), (c  3,00), (e  5,00)
  </code></pre>
  
  <p>So you can see after cloning any removing from <code>beta</code> in fact removes from <code>alfa</code>, and removing from <code>alfa</code> does not remove at all. Of course before calling <code>clone()</code> every operation on <code>alfa</code> worked correctly.</p>
  
  <p>This is for learning and the main task is to implement a working <code>clone()</code> method, so I don't want to use any other ways to perform a deep copy.</p>
  
  <p>Please advise what am I doing wrong, as self-debugging haven't helped with it yet.</p>