Implement clone and invert

This commit is contained in:
2021-09-04 16:23:38 +02:00
parent b68ff6c72e
commit 3b6c546cd4
3 changed files with 46 additions and 4 deletions

View File

@ -43,7 +43,7 @@ public interface OrderedTreeProcessor<V, T> {
T leftmostNodeInRightSubtree(T t);
T invert(T t);
void invert(T t);
T clone(T t);

View File

@ -300,13 +300,31 @@ public class RecursiveOrderedBinaryTreeNodeProcessor<V> extends OrderedBinaryTre
}
@Override
public BinaryTreeNode<V> invert(BinaryTreeNode<V> tree) {
return null;
public void invert(BinaryTreeNode<V> tree) {
if (tree == null) {
return;
}
invert(tree.left);
invert(tree.right);
var t = tree.left;
tree.left = tree.right;
tree.right = t;
}
@Override
public BinaryTreeNode<V> clone(BinaryTreeNode<V> tree) {
return null;
if (tree == null) {
return null;
}
var t = new BinaryTreeNode<V>();
t.left = clone(tree.left);
t.right = clone(tree.right);
t.key = tree.key;
return t;
}
@Override

View File

@ -243,4 +243,28 @@ public abstract class OrderedTreeProcessorTest<T> {
assertIterableEquals(list, processor.iterate(t));
}
}
@ParameterizedTest
@ArgumentsSource(ListProvider.class)
void testThat_invertWorks(List<Integer> list) {
T t = asTree(list);
processor.invert(t);
var cmp = CMP.reversed();
assertTrue(processor.check(t, cmp));
list.sort(cmp);
assertIterableEquals(list, processor.iterate(t));
}
@ParameterizedTest
@ArgumentsSource(ListProvider.class)
void testThat_cloneWorks(List<Integer> list) {
T t = asTree(list);
t = processor.clone(t);
assertTrue(processor.check(t, CMP));
list.sort(CMP);
assertIterableEquals(list, processor.iterate(t));
}
}