Java Reference
In-Depth Information
if
((leftTree !=
null
) && !leftTree.isEmpty())
root.setLeftChild(leftTree.root);
if
((rightTree !=
null
) && !rightTree.isEmpty())
{
if
(rightTree != leftTree)
root.setRightChild(rightTree.root);
else
root.setRightChild(rightTree.root.copy());
}
// end if
if
((leftTree !=
null
) && (leftTree !=
this
))
leftTree.clear();
if
((rightTree !=
null
) && (rightTree !=
this
))
rightTree.clear();
}
// end privateSetTree
Question 2
At the end of the implementation of
privateSetTree
, can you set
rightTree
to
null
instead of invoking
clear
? Explain.
The public methods
getRootData
,
isEmpty
, and
clear
are easy to implement. In addition
to these methods, we define several protected methods—
setRootData
,
setRootNode
, and
getRootNode
—that will be useful in the implementation of a subclass. The implementa-
tions of these methods follow:
public
T getRootData()
{
T rootData =
null
;
if
(root !=
null
)
rootData = root.getData();
return
rootData;
}
// end getRootData
public boolean
isEmpty()
{
VideoNote
Binary tree operations
return
root ==
null
;
}
// end isEmpty
public void
clear()
{
root =
null
;
}
// end clear
protected void
setRootData(T rootData)
{
root.setData(rootData);
}
// end setRootData
protected void
setRootNode(BinaryNodeInterface<T> rootNode)
{
root = rootNode;
}
// end setRootNode
protected
BinaryNodeInterface<T> getRootNode()
{
return
root;
}
// end getRootNode