NOTE: This is not a BinarySearchTree
public class BinaryTree {
BinaryTree left, right;
int data;
public BinaryTree(int data) {
this.data=data;
this.left=null;
this.right=null;
}
public void insert(int data) {
BinaryTree T = this;
Queue<BinaryTree> Q = new LinkedList<BinaryTree>();
Q.add(T);
while(Q.size()>0) {
BinaryTree Current = Q.remove();
if(Current.left==null) {
Current.left=new BinaryTree(data);
break;
}
else if(Current.right==null) {
Current.right=new BinaryTree(data);
break;
}
else {
Q.add(Current.left);
Q.add(Current.right);
}
}
}
}
No comments:
Post a Comment