• Post Reply Bookmark Topic Watch Topic
  • New Topic

i am not getting the results i am expecting in binary search tree.  RSS feed

 
john steward
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i don't see any nodes that i add.not sure why getting this error.
duplicate found
Exception in thread "main" java.lang.NullPointerException
at binarysearchtree.delete(binarysearchtree.java:111)
at binarysearchtree.main(binarysearchtree.java:196)
Java Result: 1

 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the ranch.

There is no guarantee that the left node won't be null, so when you do pc = pc.getLeft(), pc may be null. Then when you do c = pc.getData() you get the NullPointerException.

Also, don't override equals() without overriding getHash() too.
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Also, don't override equals() without overriding getHash() too.

You probably meant hashCode()
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, indeed! getHash() is something else altogether!
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A binary tree is guaranteed to have nulls in; I think you always have one more null than there are “real” values in it. So an inOrderTraversal method might look like thisI do not usually like things like if (xyz != null) … but in this instance it is unavoidable and must be used.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!