import java.util.*;

public class DigitalForrest {
// this code was written by Andreas van Cranenburgh, 0440949
// I dislike Java, that's why the code sucks.
	public static void main (String[] args) {	
		//make a tree with stupid data
		LinkedBinaryTree tree = new LinkedBinaryTree();
		tree.addRoot("h");
		Position root = tree.root();
		Position left = tree.insertLeft(root, "e");
		Position right = tree.insertRight(root, "l");
		Position left1 = tree.insertLeft(left, "l");
		Position right1 = tree.insertRight(right, "!");
		Position left2 = tree.insertRight(left, "o");
		Position right2 = tree.insertLeft(right, ".");
		//      h
		//   e     l
		// l  o   .   !
		
		//do the magic
		PrintTree(tree);
		
		//first print the root:
		//print half the spaces of the height, to align to center:
	}
	public static LinkedBinaryTree ParseTree(LinkedBinaryTree tree) {
		// parse a tree with padding so that there are null-nodes in place of non existing nodes
		
	}	
	public static void PrintTree(LinkedBinaryTree tree) {
		//first print the root:
		//print half the spaces of the height, to align to center:
		Iterator positions = new PositionIterator();
		positions = tree.positions();
		BTNode node;
		for (int a=0; a < tree.size(); a++) 
			if (positions.hasNext()) {
				node = (BTNode)positions.next();
				for (int b=0; b<(int)Height(tree, node)/2; b++)
					System.out.print("\t");
				System.out.print(node.element());
			}	
	}

        public static int depth (Tree tree, Position v) {
		if (tree.isRoot(v))
			return 0;
		else
			return 1 + depth(tree, tree.parent(v));
		}
	
	// this is height2 as seen on page 239 of Goodrich & Tamassia	
	private static int Height(LinkedBinaryTree T, Position v) {
		if (T.isExternal(v))
			return 0;
		else {
			int h = 0;
			try {
				PositionIterator children = (PositionIterator)T.children(v); 
				while (children.hasNext()) {
					h = Math.max(h, Height(T, (Position)children.next()));
				}
				return h;
			} catch (ExceptionEmptyList e){
				//System.out.print("empty list\n");
			}
			return 0;
		}
	}
	
}
