Java Reference
In-Depth Information
22.
public void
outputHashTable( )
{
for
(
int
i=0; i< SIZE; i++)
{
if
(hashArray[i].size( ) > 0)
hashArray[i].outputList( );
}
}
23. This code is similar to
intersection
, but adds elements if they are not in
otherSet
:
public
Set<T> difference(Set<T> otherSet)
{
Set<T> diffSet =
new
Set<T>( );
// Copy only items in this set but not otherSet
Node<T> position = head;
while
(position !=
null
)
{
if
(!otherSet.contains(position.data))
diffSet.add(position.data);
position = position.link;
}
return
diffSet;
}
24. As implemented in Answer 23, the complexity is identical to the
intersection
method. For every element in the set, we invoke the
contains
method of
otherSet
.
This requires
O
(
nm
) steps, where
n
is the number of items in the calling object's set
and
m
is the number of items in
otherSet
's set.
25. No.
26. Change
showElementsInSubtree(subTreeRoot.leftLink);
System.out.print(subTreeRoot.data + " ");
showElementsInSubtree(subTreeRoot.rightLink);
to
showElementsInSubtree(subTreeRoot.rightLink);
System.out.print(subTreeRoot.data + " ");
showElementsInSubtree(subTreeRoot.leftLink);
Programming Projects
Visit
www.myprogramminglab.com
to complete select exercises online and get instant
feedback.
1. In an ancient land, the beautiful princess Eve had many suitors. She decided on the
following procedure to determine which suitor she would marry. First, all of the suit-
ors would be lined up one after the other and assigned numbers. The first suitor would
be number 1, the second number 2, and so on up to the last suitor, number
n
. Starting
at the suitor in the first position, she would then count three suitors down the line
VideoNote
Solution to
Programming
Project 15.1