The Set.contains method returns true if and only if the element exists in the Se
ID: 3709455 • Letter: T
Question
The Set.contains method returns true if and only if the element exists in the Set. a) The BSTSetTest.java file has test cases for BSTSet. Devise three different tests for the contain:s method (put them in testContainsA,B, and C) so that you are confident that contains) works. Your tests for this part should be "black box", that is, they don't depend on the implementation: they only call public methods of BSTSet (in this case, the constructor, add(), and contains()). Your 3 tests need to be different: that is, your add methods should be such that they cause different underlying tree structures and there should be cases where contains() returns each true and false. Implement the BSTSet.contains method. b)Explanation / Answer
b)
public Node contains(E key, Node root)
{
Node current = root;
while (current != null)
{
if (current.data.compareTo(key) == 0)
{
return current;
}
else if (current.data.compareTo(key) > 0)
{
current = current.left;
}
else
{
current = current.right;
}
}
return null;
}
a)
@Test
public void testContainsA() {
BSTSet<Integer> t = new BSTSet<>();
t.add(new Integer(10));
//t.contains() will retrun node
assertEquals(10, t.contains(10, t.root).data);
}
@Test
public void testContainsB() {
BSTSet<Integer> t = new BSTSet<>();
t.add(new Integer(11));
//t.contains() will retrun node
assertEquals(null, t.contains(11, t.root));
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.