Java binary search tree | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Java binary search tree

I am trying to write an insertion method for a bst As follows https://code.sololearn.com/c71yy56zsCoz/?ref=app But it keeps throwing NullPointerExeptions Any idea how to fix

18th Nov 2021, 3:20 PM
JOXs 🍁
JOXs 🍁 - avatar
2 Answers
0
You didn't assign any left or right and you are trying to access it. Pass the node as an argument to the insert function and try to insert there. https://www.sololearn.com/learn/664/?ref=app Check the java code there for reference.
18th Nov 2021, 4:10 PM
Arun Ruban SJ
Arun Ruban SJ - avatar
0
solve root separately and check null before use r.somerhing if (root == null) { // solve root case separately return; } Node r = root; while (true) { if (data < r.data) { // r is node now if (r.left != null) { // ... } else { // ... break; } } else { // same as left } } // while
19th Nov 2021, 1:22 PM
zemiak