Implement find

This commit is contained in:
2021-09-04 16:04:35 +02:00
parent 5df44cbc44
commit 5566f8f3f9
2 changed files with 32 additions and 1 deletions

View File

@ -8,9 +8,23 @@ public class RecursiveOrderedBinaryTreeNodeProcessor<V> extends OrderedBinaryTre
@Override
public boolean find(BinaryTreeNode<V> tree, V v, Comparator<V> cmp) {
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
public boolean override(Pointer<BinaryTreeNode<V>> pointer, V from, V to, Comparator<V> cmp) {
if (remove(pointer, from, cmp)) {

View File

@ -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();