Source of 24.5.java


  1: /** Copies the subtree rooted at this node.
  2:     @return  The root of a copy of the subtree rooted at this node. */
  3: public BinaryNode<T> copy()
  4: {
  5:    BinaryNode<T> newRoot = new BinaryNode<>(data);
  6: 
  7:    if (leftChild != null)
  8:       newRoot.setLeftChild(leftChild.copy());
  9: 
 10:    if (rightChild != null)
 11:       newRoot.setRightChild(rightChild.copy());
 12: 
 13:    return newRoot;
 14: } // end copy
 15: private void privateSetTree(T rootData, BinaryTree<T> leftTree, BinaryTree<T> rightTree)
 16: {
 17:    root = new BinaryNode<>(rootData);
 18:    if ((leftTree != null) && !leftTree.isEmpty())
 19:       root.setLeftChild(leftTree.root.copy());
 20:    if ((rightTree != null) && !rightTree.isEmpty())
 21:       root.setRightChild(rightTree.root.copy());
 22: } // end privateSetTree
 23: 
 24: // Version 4.0