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 aud.exam.prep.Pointer;
|
||||||
|
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instances of this interface can work with any "valid" tree of type <code>T</code>.
|
* 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);
|
boolean removeAll(Pointer<T> t, V v, Comparator<V> cmp);
|
||||||
|
|
||||||
|
T removeIf(T t, Predicate<V> pred);
|
||||||
|
|
||||||
V max(T t);
|
V max(T t);
|
||||||
|
|
||||||
V secondMax(T t);
|
V secondMax(T t);
|
@ -3,6 +3,7 @@ package aud.exam.prep.tree;
|
|||||||
import aud.exam.prep.Pointer;
|
import aud.exam.prep.Pointer;
|
||||||
|
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
public class RecursiveOrderedBinaryTreeNodeProcessor<V> extends OrderedBinaryTreeNodeProcessor<V> {
|
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);
|
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
|
@Override
|
||||||
public V max(BinaryTreeNode<V> tree) {
|
public V max(BinaryTreeNode<V> tree) {
|
||||||
if (tree == null) {
|
if (tree == null) {
|
||||||
@ -273,10 +310,7 @@ public class RecursiveOrderedBinaryTreeNodeProcessor<V> extends OrderedBinaryTre
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isBalanced(BinaryTreeNode<V> tree) {
|
public boolean isBalanced(BinaryTreeNode<V> tree) {
|
||||||
if (tree == null) {
|
return tree == null;
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
@ -10,7 +10,7 @@ import java.util.stream.Stream;
|
|||||||
public class DoubleLatinProvider implements ArgumentsProvider {
|
public class DoubleLatinProvider implements ArgumentsProvider {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Stream<? extends Arguments> provideArguments(ExtensionContext extensionContext) throws Exception {
|
public Stream<? extends Arguments> provideArguments(ExtensionContext extensionContext) {
|
||||||
return Stream
|
return Stream
|
||||||
.generate(() ->
|
.generate(() ->
|
||||||
Arguments.of(randomLatin(), randomLatin()))
|
Arguments.of(randomLatin(), randomLatin()))
|
@ -10,6 +10,9 @@ import org.junit.jupiter.params.provider.ArgumentsSource;
|
|||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
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 aud.exam.prep.Tests.CMP;
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
@ -69,7 +72,7 @@ public abstract class OrderedTreeProcessorTest<T> {
|
|||||||
assertIterableEquals(list, processor.iterate(t));
|
assertIterableEquals(list, processor.iterate(t));
|
||||||
}
|
}
|
||||||
|
|
||||||
private T asTree(List<Integer> list) {
|
protected T asTree(List<Integer> list) {
|
||||||
T t = processor.newEmptyTree();
|
T t = processor.newEmptyTree();
|
||||||
|
|
||||||
for (var n : list) {
|
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
|
@ParameterizedTest
|
||||||
@ArgumentsSource(ListProvider.class)
|
@ArgumentsSource(ListProvider.class)
|
||||||
void printATree(List<Integer> list) {
|
void printATree(List<Integer> list) {
|
Reference in New Issue
Block a user