package remember;

public class Tree {
	private Tree left;
	private Tree right;
	private int key;

	public Tree(int key) {
		super();
		this.key = key;
	}

	public Tree getLeft() {
		return left;
	}

	public void setLeft(Tree left) {
		this.left = left;
	}

	public Tree getRight() {
		return right;
	}

	public void setRight(Tree right) {
		this.right = right;
	}

	public int getKey() {
		return key;
	}

	public void setKey(int key) {
		this.key = key;
	}

	public void insert(Tree aTree) {
		if (this.getKey() > aTree.getKey()) {
			if (this.getLeft() != null) {
				this.getLeft().insert(aTree);
			} else {
				this.setLeft(aTree);
			}
		} else {
			if (this.getRight() != null) {
				this.getRight().insert(aTree);
			} else {
				this.setRight(aTree);
			}
		}
	}

	public void traverse(TreeVisitor visitor) {
		if (this.getLeft() != null) {
			this.getLeft().traverse(visitor);
		}
		visitor.visit(this);
		if (this.getRight() != null) {
			this.getRight().traverse(visitor);
		}
	}
}
