From 5df44cbc444addbc0e7857f579a3e3ef4054d4a3 Mon Sep 17 00:00:00 2001 From: Oshgnacknak Date: Sat, 4 Sep 2021 16:00:54 +0200 Subject: [PATCH] Implement override and overrideAll --- ...cursiveOrderedBinaryTreeNodeProcessor.java | 44 ++++++++---- .../prep/tree/OrderedTreeProcessorTest.java | 70 +++++++++++++++++++ 2 files changed, 102 insertions(+), 12 deletions(-) diff --git a/src/aud/exam/prep/tree/RecursiveOrderedBinaryTreeNodeProcessor.java b/src/aud/exam/prep/tree/RecursiveOrderedBinaryTreeNodeProcessor.java index 70d9ee4..ecb9536 100644 --- a/src/aud/exam/prep/tree/RecursiveOrderedBinaryTreeNodeProcessor.java +++ b/src/aud/exam/prep/tree/RecursiveOrderedBinaryTreeNodeProcessor.java @@ -13,12 +13,30 @@ public class RecursiveOrderedBinaryTreeNodeProcessor extends OrderedBinaryTre @Override public boolean override(Pointer> pointer, V from, V to, Comparator cmp) { + if (remove(pointer, from, cmp)) { + pointer.deref = insert(pointer.deref, to, cmp); + return true; + } return false; } @Override public boolean overrideAll(Pointer> pointer, V from, V to, Comparator cmp) { - return false; + var count = removeAllWithCount(pointer, from, cmp); + if (count == 0) { + return false; + } + + pointer.deref = insertNTimes(pointer.deref, to, cmp, count); + return true; + } + + private BinaryTreeNode insertNTimes(BinaryTreeNode tree, V v, Comparator cmp, int count) { + if (count == 0) { + return tree; + } + tree = insert(tree, v, cmp); + return insertNTimes(tree, v, cmp, count-1); } @Override @@ -118,10 +136,14 @@ public class RecursiveOrderedBinaryTreeNodeProcessor extends OrderedBinaryTre @Override public boolean removeAll(Pointer> pointer, V v, Comparator cmp) { + return removeAllWithCount(pointer, v, cmp) > 0; + } + + private int removeAllWithCount(Pointer> pointer, V v, Comparator cmp) { var tree = pointer.deref; if (tree == null) { - return false; + return 0; } var c = cmp.compare(v, tree.key); @@ -135,20 +157,19 @@ public class RecursiveOrderedBinaryTreeNodeProcessor extends OrderedBinaryTre tree.key = getReplacementForRemoval(tree.left, tree, false); } - removeAll(pointer, v, cmp); - return true; + return 1 + removeAllWithCount(pointer, v, cmp); } if (c < 0) { - return removeAllRec(tree.left, v, cmp, tree, false); + return removeAllWithCountRec(tree.left, v, cmp, tree, false); } - return removeAllRec(tree.right, v, cmp, tree, true); + return removeAllWithCountRec(tree.right, v, cmp, tree, true); } - private boolean removeAllRec(BinaryTreeNode node, V v, Comparator cmp, BinaryTreeNode prev, boolean right) { + private int removeAllWithCountRec(BinaryTreeNode node, V v, Comparator cmp, BinaryTreeNode prev, boolean right) { if (node == null) { - return false; + return 0; } var c = cmp.compare(v, node.key); @@ -176,15 +197,14 @@ public class RecursiveOrderedBinaryTreeNodeProcessor extends OrderedBinaryTre node = prev.left; } - removeAllRec(node, v, cmp, prev, right); - return true; + return 1 + removeAllWithCountRec(node, v, cmp, prev, right); } if (c < 0) { - return removeAllRec(node.left, v, cmp, node, false); + return removeAllWithCountRec(node.left, v, cmp, node, false); } - return removeAllRec(node.right, v, cmp, node, true); + return removeAllWithCountRec(node.right, v, cmp, node, true); } @Override diff --git a/test/aud/exam/prep/tree/OrderedTreeProcessorTest.java b/test/aud/exam/prep/tree/OrderedTreeProcessorTest.java index 7cdaa2a..27902e8 100644 --- a/test/aud/exam/prep/tree/OrderedTreeProcessorTest.java +++ b/test/aud/exam/prep/tree/OrderedTreeProcessorTest.java @@ -9,6 +9,7 @@ import org.junit.jupiter.params.provider.ArgumentsSource; import java.util.Collections; import java.util.List; +import java.util.Objects; import static aud.exam.prep.Tests.CMP; import static org.junit.jupiter.api.Assertions.*; @@ -156,4 +157,73 @@ public abstract class OrderedTreeProcessorTest { T t = asTree(list); processor.print(t); } + + @ParameterizedTest + @ArgumentsSource(ListProvider.class) + void testThat_overrideOfNotExistsIsFalseAndDoesNotModify(List list) { + T t = asTree(list); + list.sort(CMP); + + var p = new Pointer<>(t); + assertFalse(processor.override(p, -1, -5, CMP)); + assertFalse(processor.override(p, 999, -5, CMP)); + t = p.deref; + + assertTrue(processor.check(t, CMP)); + assertIterableEquals(list, processor.iterate(t)); + } + + @ParameterizedTest + @ArgumentsSource(ListProvider.class) + void testThat_overrideOfExistsIsTrueAndDoesModify(List list) { + T t = asTree(list); + for (int i = 0; i < list.size(); i++) { + var to = -(i*i) - 5; + + var p = new Pointer<>(t); + assertTrue(processor.override(p, list.get(i), to, CMP)); + list.set(i, to); + t = p.deref; + + assertTrue(processor.check(t, CMP)); + list.sort(CMP); + assertIterableEquals(list, processor.iterate(t)); + } + } + + + @ParameterizedTest + @ArgumentsSource(ListProvider.class) + void testThat_overrideAllOfNotExistsIsFalseAndDoesNotModify(List list) { + T t = asTree(list); + list.sort(CMP); + + var p = new Pointer<>(t); + assertFalse(processor.overrideAll(p, -1, -5, CMP)); + assertFalse(processor.overrideAll(p, 999, -5, CMP)); + t = p.deref; + + assertTrue(processor.check(t, CMP)); + assertIterableEquals(list, processor.iterate(t)); + } + + @ParameterizedTest + @ArgumentsSource(ListProvider.class) + void testThat_overrideAllOfExistsIsTrueAndDoesModify(List list) { + T t = asTree(list); + for (int i = 0; i < list.size(); i++) { + var from = list.get(i); + var to = -(i*i) - 5; + + var p = new Pointer<>(t); + assertTrue(processor.overrideAll(p, from, to, CMP)); + t = p.deref; + list.replaceAll(k -> + Objects.equals(k, from) ? to : k); + + assertTrue(processor.check(t, CMP)); + list.sort(CMP); + assertIterableEquals(list, processor.iterate(t)); + } + } } \ No newline at end of file