Results 1 to 5 of 5
  1. #1
    Hawkflame is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default Problem with Binary Search Tree

    Hi, I'm new to the forum and am not sure if this is the right place to post this, but I'm getting a problem with the following Binary Search Tree:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    import java.util.Comparator;
    class BST{
    private BSTNode t=null;
    private BSTNode l = null;
    private BSTNode r = null;
    private BSTNode node;
    private StringComparator comp;// how to compare nodes
    public BST(Comparator c){
    
    
    }
    
    // constructor
    public BSTNode insert(Object newData){
    BSTNode temp=t;
    if (t == null)
    {
    	t=new BSTNode(newData);
    	t.left= new BSTNode(null);
    	t.right= new BSTNode(null);
    	return t;
    }
    else
    {
    
    	while (temp.data != null)
    	{
    		if (comp.compare(temp.data, newData)<0)
    			{
    				System.out.println("going left");
    				temp=temp.left;
    			}
    			else
    			{
    				System.out.println("going right");
    				temp=temp.right;
    			}
    		
    	}
    	temp.data=newData;
    	temp.left= new BSTNode(null);
    	temp.right= new BSTNode(null);
    	return temp;
    }
    //	if (comp.compare(t.data, newData)<0)
    //	return t.left = insert(newData);
    //else return t.right = insert(newData);
    }
    
    
    // can check if comp is null before insert
    public void setComparator(StringComparator c){
    if (t == null)
    	c.compare(t.data, node.data);
    }
    // only allow if tree empty
    public void clear(){
    	
    	t = null;
    }// empty tree
    public BSTNode find(String key)
    {
     if (t == null)
     return null;
    if (key == t.data)
     return t;
    if (comp.compare(key, t.data)<0)
    return find(key);
    else
    	return find(key);
    }
    public void getText(JTextField t, String key)
    {
    t.setText(key);
    }
    public String toString()
    {
    String fail= "The object was not found.";	
    return fail;}
    }
    In particular I'm getting a null pointer exception on line 33 (if (comp.compare(temp.data, newData)<0)).

    Any idea why this is happening? Help would be much appreciated!

  2. #2
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: Problem with Binary Search Tree

    Having the stack trace from the exception would be helpful.

    Either comp is null or temp is null, based on what you've described. Now, your BST class' constructor takes a comparator as input, but it never does anything with it. Is that really what you intended. Also, your code's commentary indicates that the insert method is the constructor, which is not possible.

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Problem with Binary Search Tree


  4. #4
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: Problem with Binary Search Tree

    Thanks for pointing that out doWhile. Thanks for wasting my time Hawkflame.

  5. #5
    Hawkflame is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default Re: Problem with Binary Search Tree

    Quote Originally Posted by jlczuk View Post
    Thanks for pointing that out doWhile. Thanks for wasting my time Hawkflame.
    I apologize for the lack of nettiquette; usually I feel my way around the dos and don'ts of a forum first, but I was in a bit of a panic, and I could see why crossposting like that could be rude.

    Anyway, I actually figured it out myself; the problem indeed was with the StringComparator comp, so I just scrapped it completely, replacing

    if (comp.compare(temp.data, newData)<0)

    with

    if ((temp.data.toString()).compareTo(newData.toString ())>0)

    I'll try to be more patient in the future. :)

Similar Threads

  1. Binary search tree ?
    By santa in forum New To Java
    Replies: 5
    Last Post: 06-07-2011, 03:22 PM
  2. Replies: 0
    Last Post: 04-04-2010, 07:40 AM
  3. Binary search tree search method
    By chopo1980 in forum New To Java
    Replies: 2
    Last Post: 12-10-2009, 01:42 AM
  4. Binary Search Tree
    By Goo in forum New To Java
    Replies: 0
    Last Post: 03-06-2009, 04:46 PM
  5. Binary Search Tree
    By michael_mke in forum New To Java
    Replies: 3
    Last Post: 12-04-2008, 02:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •