// Herbruikbare package...
package be.hogent.h20;

class H20_TreeNode {
	
	H20_TreeNode leftNode;
	int data;
	H20_TreeNode rightNode;
	
	public H20_TreeNode (int nodeData) {
		data = nodeData;
		leftNode = rightNode = null;
	}
	
	public synchronized void insert (int x) {
		if (x < data) {
			if (leftNode == null)
				leftNode = new H20_TreeNode(x);
			else
				leftNode.insert(x);
		}
		else if (x > data) {
			if (rightNode == null)
				rightNode = new H20_TreeNode(x);
			else
				rightNode.insert(x);
		}
	}
}

public class H20_Tree {
	
	private H20_TreeNode root;
	
	public H20_Tree() {
		root = null;		
	}
	
	public synchronized void insertNode(int x) {
		if (root == null)
			root = new H20_TreeNode(x);
		else
			root.insert(x);
	}
	
	public synchronized void preorderTraversal() {
		preorderHelper(root);
	}
	
	private void preorderHelper(H20_TreeNode node) {
		if (node == null)
			return;
		
		System.out.print(node.data + " ");
		preorderHelper(node.leftNode);
		preorderHelper(node.rightNode);
	}
	
	public synchronized void inorderTraversel() {
		inorderHelper(root);
	}
	
	private void inorderHelper(H20_TreeNode node) {
		if (node == null)
			return;
		
		inorderHelper(node.leftNode);
		System.out.print(node.data + " ");
		inorderHelper(node.rightNode);
	}
	
	public synchronized void postorderTraversal() {
		postorderHelper(root);
	}
	
	private void postorderHelper(H20_TreeNode node) {
		if (node == null)
			return;
		
		postorderHelper(node.leftNode);
		postorderHelper(node.rightNode);
		System.out.print(node.data + " ");
	}

}
