Implement find
This commit is contained in:
@ -8,7 +8,21 @@ public class RecursiveOrderedBinaryTreeNodeProcessor<V> extends OrderedBinaryTre
|
||||
|
||||
@Override
|
||||
public boolean find(BinaryTreeNode<V> tree, V v, Comparator<V> cmp) {
|
||||
return false;
|
||||
if (tree == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
var c = cmp.compare(v, tree.key);
|
||||
|
||||
if (c == 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (c < 0) {
|
||||
return find(tree.left, v, cmp);
|
||||
}
|
||||
|
||||
return find(tree.right, v, cmp);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -22,6 +22,23 @@ public abstract class OrderedTreeProcessorTest<T> {
|
||||
this.processor = processor;
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ArgumentsSource(ListProvider.class)
|
||||
void testThat_findOfNonExistsIsFalse(List<Integer> list) {
|
||||
T t = asTree(list);
|
||||
assertFalse(processor.find(t, -1, CMP));
|
||||
assertFalse(processor.find(t, 999, CMP));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ArgumentsSource(ListProvider.class)
|
||||
void testThat_findOfExistsIsTrue(List<Integer> list) {
|
||||
T t = asTree(list);
|
||||
for (var n : list) {
|
||||
assertTrue(processor.find(t, n, CMP));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testThat_newEmptyTreeWorks() {
|
||||
T t = processor.newEmptyTree();
|
||||
|
Reference in New Issue
Block a user