Java Reference
In-Depth Information
/** Copies the subtree rooted at this node.
@return the root of a copy of the subtree rooted at this node */
public
BinaryNodeInterface<T> copy();
}
// end BinaryNodeInterface
These nodes have more responsibilities than the nodes in a linked chain. Soon you will see
how the last three methods in this interface simplify the implementation of the binary tree. But first
we will implement this interface as the class
BinaryNode
.
Since we want to hide the node from clients of the binary tree, we place
BinaryNode
within
TreePackage
, along with
BinaryNodeInterface
, and omit its access modifier. We present a portion
of the implementation of
BinaryNode
in Listing 24-1.
LISTING 24-2
The class
BinaryNode
package
TreePackage;
class
BinaryNode<T>
implements
BinaryNodeInterface<T>
{
private
T data;
private
BinaryNode<T> left;
private
BinaryNode<T> right;
public
BinaryNode()
{
this
(
null
);
// call next constructor
}
// end default constructor
public
BinaryNode(T dataPortion)
{
this
(dataPortion,
null
,
null
);
// call next constructor
}
// end constructor
public
BinaryNode(T dataPortion, BinaryNode<T> leftChild,
BinaryNode<T> rightChild)
{
data = dataPortion;
left = leftChild;
right = rightChild;
}
// end constructor
public
T getData()
{
return
data;
}
// end getData