Move print to right place

This commit is contained in:
2021-09-04 16:15:01 +02:00
parent 5566f8f3f9
commit b68ff6c72e
2 changed files with 24 additions and 23 deletions

View File

@ -1,6 +1,7 @@
package aud.exam.prep.tree; package aud.exam.prep.tree;
abstract class OrderedBinaryTreeNodeProcessor<V> implements OrderedTreeProcessor<V, BinaryTreeNode<V>> { abstract class OrderedBinaryTreeNodeProcessor<V> implements OrderedTreeProcessor<V, BinaryTreeNode<V>> {
@Override @Override
public BinaryTreeNode<V> newEmptyTree() { public BinaryTreeNode<V> newEmptyTree() {
return null; return null;
@ -9,28 +10,6 @@ abstract class OrderedBinaryTreeNodeProcessor<V> implements OrderedTreeProcessor
@Override @Override
public Iterable<V> iterate(BinaryTreeNode<V> tree) { public Iterable<V> iterate(BinaryTreeNode<V> tree) {
return () -> return () ->
new BinaryTreeIterator<>(tree); new BinaryTreeIterator<>(tree);
}
@Override
public void print(BinaryTreeNode<V> tree) {
printRec(tree, 0);
}
private void printRec(BinaryTreeNode<V> tree, int indent) {
if (tree == null) {
System.out.println("- *");
return;
}
System.out.println("-> " + tree.key);
System.out.print(" |".repeat(indent));
System.out.print(" L");
printRec(tree.left, indent + 1);
System.out.print(" |".repeat(indent));
System.out.print(" L");
printRec(tree.right, indent + 1);
} }
} }

View File

@ -331,4 +331,26 @@ public class RecursiveOrderedBinaryTreeNodeProcessor<V> extends OrderedBinaryTre
return true; return true;
} }
@Override
public void print(BinaryTreeNode<V> tree) {
printRec(tree, 0);
}
private void printRec(BinaryTreeNode<V> tree, int indent) {
if (tree == null) {
System.out.println("- *");
return;
}
System.out.println("-> " + tree.key);
System.out.print(" |".repeat(indent));
System.out.print(" L");
printRec(tree.left, indent + 1);
System.out.print(" |".repeat(indent));
System.out.print(" L");
printRec(tree.right, indent + 1);
}
} }