public interface CommonIterator<E> {
E first();
E next();
Boolean isDone();
}
public interface Tree {
CommonIterator createFrontIndexIterator();
CommonIterator createMiddleIndexIterator();
CommonIterator createBackIndexIterIterator();
}
public class Node implements Tree{
public Node left;
public Node right;
public String name;
public Node(Node left, Node right, String name) {
this.left = left;
this.right = right;
this.name = name;
}
public Node(String name) {
this.name = name;
}
public CommonIterator<Node> createFrontIndexIterator(){
return new FrontIndexIter(this);
}
public CommonIterator<Node> createMiddleIndexIterator(){
return new MiddleIndexIter(this);
}
public CommonIterator<Node> createBackIndexIterIterator(){
return new BackIndexIter(this);
}
}
public class FrontIndexIter implements CommonIterator<Node> {
private final Node root;
private Node cursor = null;
private Boolean isDone = false;
private Stack<Node> stack;
public FrontIndexIter(Node root) {
this.root = root;
stack = new Stack<>();
cursor = root;
stack.push(cursor);
}
@Override
public Node first() {
return root;
}
@Override
public Node next() {
if (stack.isEmpty()) {
isDone = true;
return null;
}
Node top=stack.pop();
cursor = top.right;
if (cursor!=null){
stack.push(cursor);
}
cursor = top.left;
if (cursor!=null){
stack.push(cursor);
}
return top;
}
@Override
public Boolean isDone() {
return isDone;
}
}
public class MiddleIndexIter implements CommonIterator<Node> {
private final Node root;
private Node cursor = null;
private Boolean isDone = false;
private Stack<Node> stack;
public MiddleIndexIter(Node root) {
this.root = root;
stack = new Stack<>();
cursor = root;
while (cursor != null) {
stack.push(cursor);
cursor = cursor.left;
}
}
@Override
public Node first() {
Node res = root;
while ((res = res.left) != null) {
}
return res;
}
@Override
public Node next() {
if (stack.isEmpty()) {
isDone = true;
return null;
}
Node top = stack.peek();
stack.pop();
cursor = top.right;
while (cursor != null) {
stack.push(cursor);
cursor = cursor.left;
}
return top;
}
@Override
public Boolean isDone() {
return isDone;
}
}
public class BackIndexIter implements CommonIterator<Node> {
private final Node root;
private Node cursor = null;
private Boolean isDone = false;
private Stack<Node> stack;
public BackIndexIter(Node root) {
this.root = root;
stack = new Stack<>();
cursor = root;
while (cursor != null) {
stack.push(cursor);
cursor = cursor.left;
}
}
@Override
public Node first() {
Node res = root;
while ((res = res.left) != null) {
}
return res;
}
@Override
public Node next() {
if (stack.isEmpty()) {
isDone = true;
return null;
}
Node top = stack.peek();
stack.pop();
if (!stack.isEmpty()) {
cursor = stack.peek();
if ((cursor = cursor.right) != null && cursor != top) {
while (cursor != null) {
stack.push(cursor);
cursor = cursor.left;
}
}
}
return top;
}
@Override
public Boolean isDone() {
return isDone;
}
}
public class App {
public static void main(String[] args) {
Node one = new Node(
new Node(new Node(new Node("8"),null,"3"), new Node(new Node("6"), new Node("7"), "4"), "2"),
new Node(new Node("9"),null,"5"), "1");
CommonIterator<Node> iter = one.createFrontIndexIterator();
Node d;
while (!iter.isDone() && (d = iter.next()) != null) {
System.out.print(d.name+" ");
}
System.out.println();
iter = one.createMiddleIndexIterator();
while (!iter.isDone() && (d = iter.next()) != null) {
System.out.print(d.name+" ");
}
System.out.println();
iter = one.createBackIndexIterIterator();
while (!iter.isDone() && (d = iter.next()) != null) {
System.out.print(d.name+" ");
}
}
}