Implement clone and invert
This commit is contained in:
@ -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);
|
||||
|
||||
|
@ -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
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user