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
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.
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