001 package brs.visitor;
002
003 import brs.*;
004
005 /**
006 * Print all the numbers in a tree in in-order, line by line
007 * @author Mathias Ricken - Copyright 2008 - All rights reserved.
008 */
009 public class InOrderPrint<T> implements IVisitor<T,Void,Void> {
010 public Void emptyCase(BiTree<T> host, Void... nu) {
011 return null;
012 }
013
014 public Void nonEmptyCase(BiTree<T> host, Void... nu) {
015 host.getLeftSubTree().execute(this);
016 System.err.println(host.getRootDat());
017 host.getRightSubTree().execute(this);
018 return null;
019 }
020
021 public static void main(String[] args) {
022 BiTree<Integer> bt = new BiTree<Integer>();
023 InOrderPrint<Integer> inOrderPrint = new InOrderPrint<Integer>();
024
025 System.out.println(bt);
026 bt.execute(inOrderPrint);
027 System.out.println("==============================");
028
029 bt.insertRoot(5);
030 System.out.println(bt);
031 bt.execute(inOrderPrint);
032 System.out.println("==============================");
033
034 bt.getLeftSubTree().insertRoot(-2);
035 System.out.println(bt);
036 bt.execute(inOrderPrint);
037 System.out.println("==============================");
038
039 bt.getRightSubTree().insertRoot(10);
040 System.out.println(bt);
041 bt.execute(inOrderPrint);
042 System.out.println("==============================");
043
044 bt.getRightSubTree().getLeftSubTree().insertRoot(-9);
045 System.out.println(bt);
046 bt.execute(inOrderPrint);
047 }
048 }