From 4e98be1c07ad8b46470ec01bdf7a0f67eb074159 Mon Sep 17 00:00:00 2001 From: Oshgnacknak Date: Fri, 3 Sep 2021 11:52:57 +0200 Subject: [PATCH] Implement max and secondMax --- ...cursiveOrderedBinaryTreeNodeProcessor.java | 40 ++++++++++++++-- .../prep/tree/OrderedTreeProcessorTest.java | 47 ++++++++++++++++--- 2 files changed, 77 insertions(+), 10 deletions(-) diff --git a/src/aud/exam/prep/tree/RecursiveOrderedBinaryTreeNodeProcessor.java b/src/aud/exam/prep/tree/RecursiveOrderedBinaryTreeNodeProcessor.java index 4fe012f..a6f643e 100644 --- a/src/aud/exam/prep/tree/RecursiveOrderedBinaryTreeNodeProcessor.java +++ b/src/aud/exam/prep/tree/RecursiveOrderedBinaryTreeNodeProcessor.java @@ -49,13 +49,45 @@ public class RecursiveOrderedBinaryTreeNodeProcessor extends OrderedBinaryTre } @Override - public V max(BinaryTreeNode tree, Comparator cmp) { - return null; + public V max(BinaryTreeNode 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 tree, Comparator cmp) { - return null; + public V secondMax(BinaryTreeNode 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 node, V prev) { + if (node.right != null) { + return secondMaxRec(node.right, node.key); + } + + if (node.left != null) { + return max(node.left); + } + + return prev; } @Override diff --git a/test/aud/exam/prep/tree/OrderedTreeProcessorTest.java b/test/aud/exam/prep/tree/OrderedTreeProcessorTest.java index 5504803..bd1ea35 100644 --- a/test/aud/exam/prep/tree/OrderedTreeProcessorTest.java +++ b/test/aud/exam/prep/tree/OrderedTreeProcessorTest.java @@ -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 { @ParameterizedTest @ArgumentsSource(ListProvider.class) void testThat_insertAndIterateWork(List 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 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 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 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 list) { + T t = asTree(list); + processor.print(t); + } } \ No newline at end of file