Results 1 to 5 of 5
Thread: Problem with Binary Search Tree
 04262012, 03:37 AM #1Member
 Join Date
 Apr 2012
 Posts
 2
 Rep Power
 0
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;} }
Any idea why this is happening? Help would be much appreciated!
 04262012, 03:49 AM #2Senior Member
 Join Date
 Apr 2012
 Location
 New York State of Confusion, USA
 Posts
 137
 Blog Entries
 1
 Rep Power
 0
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.
 04262012, 04:05 AM #3Moderator
 Join Date
 Jul 2010
 Location
 California
 Posts
 1,641
 Rep Power
 8
Re: Problem with Binary Search Tree
Crossposted at Problem with Binary Search Tree
 04262012, 04:14 AM #4Senior Member
 Join Date
 Apr 2012
 Location
 New York State of Confusion, USA
 Posts
 137
 Blog Entries
 1
 Rep Power
 0
Re: Problem with Binary Search Tree
Thanks for pointing that out doWhile. Thanks for wasting my time Hawkflame.
 04262012, 06:30 AM #5Member
 Join Date
 Apr 2012
 Posts
 2
 Rep Power
 0
Re: Problem with Binary Search Tree
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

Binary search tree ?
By santa in forum New To JavaReplies: 5Last Post: 06072011, 03:22 PM 
Data Structures(Binary Search Tree to AVL Tree)ASAP pls
By jfAdik in forum Forum LobbyReplies: 0Last Post: 04042010, 07:40 AM 
Binary search tree search method
By chopo1980 in forum New To JavaReplies: 2Last Post: 12102009, 02:42 AM 
Binary Search Tree
By Goo in forum New To JavaReplies: 0Last Post: 03062009, 05:46 PM 
Binary Search Tree
By michael_mke in forum New To JavaReplies: 3Last Post: 12042008, 03:03 AM
Bookmarks