Java Reference
In-Depth Information
public
BinaryTree(T rootData, BinaryTree<T> leftTree,
BinaryTree<T> rightTree)
{
privateSetTree(rootData, leftTree, rightTree);
}
// end constructor
public void
setTree(T rootData)
{
root =
new
BinaryNode<T>(rootData);
}
// end setTree
public void
setTree(T rootData, BinaryTreeInterface<T> leftTree,
BinaryTreeInterface<T> rightTree)
{
privateSetTree(rootData, (BinaryTree<T>)leftTree,
(BinaryTree<T>)rightTree);
}
// end setTree
private void
privateSetTree(T rootData, BinaryTree<T> leftTree,
BinaryTree<T> rightTree)
{
< FIRST DRAFT - See Segments 24.5 - 24.8 for improvements. >
root =
new
BinaryNode<T>(rootData);
if
(leftTree !=
null
)
root.setLeftChild(leftTree.root);
if
(rightTree !=
null
)
root.setRightChild(rightTree.root);
}
// end privateSetTree
<
Implementations of
getRootData
,
getHeight
,
getNumberOfNodes
,
isEmpty
,
clear
,
and the methods specified in
TreeIteratorInterface
are here.
>
. . .
}
// end BinaryTree
Programming Tip:
No cast is needed when you pass an instance of
BinaryTree
to a
method whose parameter has the type
BinaryTreeInterface
. The converse, however,
requires a cast.
24.5
A problem.
The implementation of
privateSetTree
just given is really not sufficient to han-
dle all possible uses of the method. Suppose that the client defines three distinct instances of
BinaryTree
—
treeA
,
treeB
, and
treeC
—and executes the statement
treeA.setTree(a, treeB, treeC);