Implement max and secondMax
This commit is contained in:
@ -49,13 +49,45 @@ public class RecursiveOrderedBinaryTreeNodeProcessor<V> extends OrderedBinaryTre
|
||||
}
|
||||
|
||||
@Override
|
||||
public V max(BinaryTreeNode<V> tree, Comparator<V> cmp) {
|
||||
return null;
|
||||
public V max(BinaryTreeNode<V> tree) {
|
||||
if (tree == null) {
|
||||
throw new IllegalArgumentException("An empty tree has no max");
|
||||
}
|
||||
|
||||
if (tree.right == null) {
|
||||
return tree.key;
|
||||
}
|
||||
|
||||
return max(tree.right);
|
||||
}
|
||||
|
||||
@Override
|
||||
public V secondMax(BinaryTreeNode<V> tree, Comparator<V> cmp) {
|
||||
return null;
|
||||
public V secondMax(BinaryTreeNode<V> tree) {
|
||||
if (tree == null) {
|
||||
throw new IllegalArgumentException("An empty tree has no second max");
|
||||
}
|
||||
|
||||
if (tree.right != null) {
|
||||
return secondMaxRec(tree.right, tree.key);
|
||||
}
|
||||
|
||||
if (tree.left != null) {
|
||||
return max(tree.left);
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException("A tree with only one element has no second max");
|
||||
}
|
||||
|
||||
private V secondMaxRec(BinaryTreeNode<V> node, V prev) {
|
||||
if (node.right != null) {
|
||||
return secondMaxRec(node.right, node.key);
|
||||
}
|
||||
|
||||
if (node.left != null) {
|
||||
return max(node.left);
|
||||
}
|
||||
|
||||
return prev;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -1,7 +1,7 @@
|
||||
package aud.exam.prep.tree;
|
||||
|
||||
import aud.exam.prep.ListProvider;
|
||||
import aud.exam.prep.tree.OrderedTreeProcessor;
|
||||
import aud.exam.prep.Tests;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ArgumentsSource;
|
||||
@ -42,14 +42,49 @@ public abstract class OrderedTreeProcessorTest<T> {
|
||||
@ParameterizedTest
|
||||
@ArgumentsSource(ListProvider.class)
|
||||
void testThat_insertAndIterateWork(List<Integer> list) {
|
||||
T t = processor.newEmptyTree();
|
||||
|
||||
for (var n : list) {
|
||||
t = processor.insert(t, n, CMP);
|
||||
}
|
||||
T t = asTree(list);
|
||||
list.sort(CMP);
|
||||
|
||||
assertTrue(processor.check(t, CMP));
|
||||
assertIterableEquals(list, processor.iterate(t));
|
||||
}
|
||||
|
||||
private T asTree(List<Integer> list) {
|
||||
T t = processor.newEmptyTree();
|
||||
|
||||
for (var n : list) {
|
||||
t = processor.insert(t, n, CMP);
|
||||
}
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ArgumentsSource(ListProvider.class)
|
||||
void testThat_maxWorks(List<Integer> list) {
|
||||
list.add(-1);
|
||||
T t = asTree(list);
|
||||
|
||||
Integer max = Tests.getMax(list);
|
||||
assertEquals(max, processor.max(t));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ArgumentsSource(ListProvider.class)
|
||||
void testThat_secondMaxWorks(List<Integer> list) {
|
||||
list.add(-1);
|
||||
list.add(-2);
|
||||
T t = asTree(list);
|
||||
|
||||
Integer max = Tests.getSecondMax(list);
|
||||
assertEquals(max, processor.secondMax(t));
|
||||
}
|
||||
|
||||
|
||||
@ParameterizedTest
|
||||
@ArgumentsSource(ListProvider.class)
|
||||
void printATree(List<Integer> list) {
|
||||
T t = asTree(list);
|
||||
processor.print(t);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user