Skip to content

Instantly share code, notes, and snippets.

@calindotgabriel
Created June 30, 2015 17:25
Show Gist options
  • Save calindotgabriel/3bf9bee69e2d58160287 to your computer and use it in GitHub Desktop.
Save calindotgabriel/3bf9bee69e2d58160287 to your computer and use it in GitHub Desktop.
package tree;
import java.util.Iterator;
import java.util.Stack;
/**
* BinaryTree implementation.
*/
public class BinaryTree<T> implements Iterable<BinaryTree<T>>{
private TreeNode<T> root;
public BinaryTree() {}
public BinaryTree(T data) {
createLeaf(data);
}
public BinaryTree(BinaryTree<T> left, T data, BinaryTree<T> right) {
createLeaf(data);
addSubLeft(left);
addSubRight(right);
}
public void createLeaf(T data) {
root = new TreeNode<T>(data);
}
public void addSubLeft(BinaryTree<T> tree) {
root.leftTree = tree;
}
public void addSubRight(BinaryTree<T> tree) {
root.rightTree = tree;
}
public T element() {
return root.data;
}
public BinaryTree<T> left() {
return root.leftTree;
}
public BinaryTree<T> right() {
return root.rightTree;
}
public boolean empty() {
return root == null;
}
@Override
public Iterator<BinaryTree<T> > iterator() {
return new PreOrderIterator(this);
}
private class PreOrderIterator implements Iterator<BinaryTree<T> > {
Stack<BinaryTree<T>> stack = new Stack<BinaryTree<T>>();
public PreOrderIterator(BinaryTree<T> binaryTree) {
stack.push(binaryTree);
}
@Override
public boolean hasNext() {
return !stack.isEmpty();
}
@Override
public BinaryTree<T> next() {
BinaryTree<T> tree = stack.pop();
TreeNode<T> node = tree.root;
if (node.rightTree != null) {
stack.push(node.rightTree);
}
if (node.leftTree != null) {
stack.push(node.leftTree);
}
return tree;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment