Java Reference
In-Depth Information
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 above 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
Many of these Programming Projects can be solved using AW's CodeMate.
To access these please go to:
www.aw-bc.com/codemate
.
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 suitors 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 (because of the three letters in her name), and the third
suitor would be eliminated from winning her hand and removed from the line.