HI there, please help out with a bug i encountered during my binary tree assignment.
i searched the net for a binary tree code and thanks to CHINMAY LOKESH, i found one with a tree displays in it. i altered the code a bit to compare "string" rather than "integer". tree displaying, Inserting, deleting, and finding was doing fine when i encounter a bug when finding a value that is not inside the tree. the description are as follows...
this is the number i entered into the tree.
das
dd
5g
3213
ds6
asd
o56o
hj23
0ghj
asd
1
the Binary Tree was Created Successfully! i check the displays of the tree.
when i find the number "2"
it was doing what it supposed to by saying cannot find the number "2"
but when i try to find the number "22"
nothing occurs and this error message was displayed...
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Tree.find(Tree.java:14)
this is the code in the applet...
public Node find(String val) { Node current = root; line 14: while(val.equals(current.item) == false) { if(val.length()>current.item.length()) { current = current.rightChild; } else if(val.length()<current.item.length()) { current = current.leftChild; } else { if(val.compareToIgnoreCase(current.item)<0) current = current.leftChild; else current = current.rightChild; if(current == null) return null; } } return current; }
the odd thing is.... the value i use for finding in the range of 10 till 59 gave the same errors, but finding for 9 or 60 displays cannot find which is correct by the way...
another thing i noticed which is also weird...
if i did the find method for 10 till 60 after i inserted a number of "30"
the programs work fine and gave a message that those numbers cannot be found...
??? i have no ideas what's happened... any ideas from the community here?
this my first time posting a thread here, if you need extra info do pm here.
thanks in advance and have a nice day.
ps. tetkun.