From 5566f8f3f947f05f3f81631ded66a35a9fda60f4 Mon Sep 17 00:00:00 2001 From: Oshgnacknak Date: Sat, 4 Sep 2021 16:04:35 +0200 Subject: [PATCH] Implement find --- ...RecursiveOrderedBinaryTreeNodeProcessor.java | 16 +++++++++++++++- .../prep/tree/OrderedTreeProcessorTest.java | 17 +++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/aud/exam/prep/tree/RecursiveOrderedBinaryTreeNodeProcessor.java b/src/aud/exam/prep/tree/RecursiveOrderedBinaryTreeNodeProcessor.java index ecb9536..876d56e 100644 --- a/src/aud/exam/prep/tree/RecursiveOrderedBinaryTreeNodeProcessor.java +++ b/src/aud/exam/prep/tree/RecursiveOrderedBinaryTreeNodeProcessor.java @@ -8,7 +8,21 @@ public class RecursiveOrderedBinaryTreeNodeProcessor extends OrderedBinaryTre @Override public boolean find(BinaryTreeNode tree, V v, Comparator 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 diff --git a/test/aud/exam/prep/tree/OrderedTreeProcessorTest.java b/test/aud/exam/prep/tree/OrderedTreeProcessorTest.java index 27902e8..80c0ba1 100644 --- a/test/aud/exam/prep/tree/OrderedTreeProcessorTest.java +++ b/test/aud/exam/prep/tree/OrderedTreeProcessorTest.java @@ -22,6 +22,23 @@ public abstract class OrderedTreeProcessorTest { this.processor = processor; } + @ParameterizedTest + @ArgumentsSource(ListProvider.class) + void testThat_findOfNonExistsIsFalse(List 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 list) { + T t = asTree(list); + for (var n : list) { + assertTrue(processor.find(t, n, CMP)); + } + } + @Test void testThat_newEmptyTreeWorks() { T t = processor.newEmptyTree();