I have problem with binary tree. I am trying to write insert method but i have problem with that. I don't know why,but this method is not working,i have some problem with reference it doesn't work how I think it should...
class BinaryTreeNode<T extends Comparable<T>>{ private BinaryTreeNode<T> leftNode; private BinaryTreeNode<T> rightNode; T data; BinaryTreeNode(){ leftNode=null; rightNode=null; data=null; } BinaryTreeNode(T data){ leftNode=null; rightNode=null; this.data=data; } BinaryTreeNode<T> getLeftNode() { return leftNode; } void setLeftNode(BinaryTreeNode<T> leftNode) { this.leftNode = leftNode; } BinaryTreeNode<T> getRightNode() { return rightNode; } void setRightNode(BinaryTreeNode<T> rightNode) { this.rightNode = rightNode; } T getData() { return data; } void setData(T data) { this.data = data; } } public class BinaryTree <T extends Comparable<T>> { private BinaryTreeNode<T> root=null; private int size; public BinaryTree(){ size=0; } public int Size() { return size; } public void insert(T data){ insert(data,root); } private void insert(T data,BinaryTreeNode<T> node){ if(node==null){ node=new BinaryTreeNode<T>(data); System.out.println(data+" inserted"); size++; } else{ if(data.compareTo(node.getData())==1){ System.out.println("right node"); insert(data,node.getRightNode()); } else{ System.out.println("left node"); insert(data,node.getLeftNode()); } } } }