Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Binary Tree Insert

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Binary Tree Insert

    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());
    			}
    		}
    	}
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Binary Tree Insert

    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...
    Describe your problem, define "not working," "some problem with reference," "it doesn't work," and what evidence you have that "it doesn't work how (you) think it should." We can't read your mind or see your computer screen (but don't post screenshots).

Similar Threads

  1. Binary Tree insert(int value) method
    By geeksutopia in forum What's Wrong With My Code?
    Replies: 10
    Last Post: March 28th, 2014, 09:37 AM
  2. (Binary Tree) Family tree - help with my addChild method
    By Pip_Squeak in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 26th, 2014, 07:52 AM
  3. Binary Tree ( Family Tree)
    By tommyacton in forum Algorithms & Recursion
    Replies: 0
    Last Post: March 23rd, 2013, 10:40 PM
  4. Binary Search Tree inorder tree traversal
    By Maukkv in forum What's Wrong With My Code?
    Replies: 17
    Last Post: January 26th, 2013, 05:28 PM
  5. Data Structures(Binary Search Tree to AVL Tree)ASAP
    By jfAdik in forum Algorithms & Recursion
    Replies: 2
    Last Post: April 5th, 2010, 03:58 AM

Tags for this Thread