move source to gradle source dir
This commit is contained in:
11
src/main/java/aud/exam/prep/Main.java
Normal file
11
src/main/java/aud/exam/prep/Main.java
Normal file
@ -0,0 +1,11 @@
|
||||
/*
|
||||
* This Java source file was generated by the Gradle 'init' task.
|
||||
*/
|
||||
package aud.exam.prep;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello World!");
|
||||
}
|
||||
}
|
@ -3,6 +3,7 @@ package aud.exam.prep.tree;
|
||||
import aud.exam.prep.Pointer;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* Instances of this interface can work with any "valid" tree of type <code>T</code>.
|
||||
@ -27,6 +28,8 @@ public interface OrderedTreeProcessor<V, T> {
|
||||
|
||||
boolean removeAll(Pointer<T> t, V v, Comparator<V> cmp);
|
||||
|
||||
T removeIf(T t, Predicate<V> pred);
|
||||
|
||||
V max(T t);
|
||||
|
||||
V secondMax(T t);
|
@ -3,6 +3,7 @@ package aud.exam.prep.tree;
|
||||
import aud.exam.prep.Pointer;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class RecursiveOrderedBinaryTreeNodeProcessor<V> extends OrderedBinaryTreeNodeProcessor<V> {
|
||||
|
||||
@ -221,6 +222,42 @@ public class RecursiveOrderedBinaryTreeNodeProcessor<V> extends OrderedBinaryTre
|
||||
return removeAllWithCountRec(node.right, v, cmp, node, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BinaryTreeNode<V> removeIf(BinaryTreeNode<V> root, Predicate<V> pred) {
|
||||
if (root == null) {
|
||||
return null;
|
||||
}
|
||||
root.left = removeIf(root.left, pred);
|
||||
root.right = removeIf(root.right, pred);
|
||||
|
||||
if (pred.test(root.key)) {
|
||||
|
||||
if (root.left == null) {
|
||||
return root.right;
|
||||
} else if (root.right == null) {
|
||||
return root.left;
|
||||
} else {
|
||||
root.key = removeRightMostNode(root.left, root, false);
|
||||
}
|
||||
}
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
V removeRightMostNode(BinaryTreeNode<V> node, BinaryTreeNode<V> prev, boolean right) {
|
||||
if (node.right != null) {
|
||||
return removeRightMostNode(node.right, node, true);
|
||||
}
|
||||
|
||||
if (right) {
|
||||
prev.right = node.left;
|
||||
} else {
|
||||
prev.left = node.left;
|
||||
}
|
||||
|
||||
return node.key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public V max(BinaryTreeNode<V> tree) {
|
||||
if (tree == null) {
|
||||
@ -273,10 +310,7 @@ public class RecursiveOrderedBinaryTreeNodeProcessor<V> extends OrderedBinaryTre
|
||||
|
||||
@Override
|
||||
public boolean isBalanced(BinaryTreeNode<V> tree) {
|
||||
if (tree == null) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
return tree == null;
|
||||
}
|
||||
|
||||
@Override
|
@ -10,7 +10,7 @@ import java.util.stream.Stream;
|
||||
public class DoubleLatinProvider implements ArgumentsProvider {
|
||||
|
||||
@Override
|
||||
public Stream<? extends Arguments> provideArguments(ExtensionContext extensionContext) throws Exception {
|
||||
public Stream<? extends Arguments> provideArguments(ExtensionContext extensionContext) {
|
||||
return Stream
|
||||
.generate(() ->
|
||||
Arguments.of(randomLatin(), randomLatin()))
|
@ -10,6 +10,9 @@ import org.junit.jupiter.params.provider.ArgumentsSource;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import static aud.exam.prep.Tests.CMP;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
@ -69,7 +72,7 @@ public abstract class OrderedTreeProcessorTest<T> {
|
||||
assertIterableEquals(list, processor.iterate(t));
|
||||
}
|
||||
|
||||
private T asTree(List<Integer> list) {
|
||||
protected T asTree(List<Integer> list) {
|
||||
T t = processor.newEmptyTree();
|
||||
|
||||
for (var n : list) {
|
||||
@ -168,6 +171,46 @@ public abstract class OrderedTreeProcessorTest<T> {
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ArgumentsSource(ListProvider.class)
|
||||
void testThat_removeIfWorks(List<Integer> list) {
|
||||
T t = asTree(list);
|
||||
list.sort(CMP);
|
||||
|
||||
for (var toRemove : getRemovals(list)) {
|
||||
Predicate<Integer> pred = n ->
|
||||
Objects.equals(n, toRemove);
|
||||
|
||||
list.removeIf(pred);
|
||||
|
||||
t = processor.removeIf(t, pred);
|
||||
|
||||
assertTrue(processor.check(t, CMP));
|
||||
assertIterableEquals(list, processor.iterate(t));
|
||||
}
|
||||
}
|
||||
|
||||
List<Integer> getRemovals(List<Integer> list) {
|
||||
var start = -100 + list
|
||||
.stream()
|
||||
.min(CMP)
|
||||
.orElse(0);
|
||||
|
||||
var end = 100 + list
|
||||
.stream()
|
||||
.max(CMP)
|
||||
.orElse(0);
|
||||
|
||||
var removals = IntStream
|
||||
.rangeClosed(start, end)
|
||||
.boxed()
|
||||
.collect(Collectors.toList());
|
||||
|
||||
Collections.shuffle(removals, Tests.RANDOM);
|
||||
|
||||
return removals;
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ArgumentsSource(ListProvider.class)
|
||||
void printATree(List<Integer> list) {
|
Reference in New Issue
Block a user