I\'m currently learning about trees using java and i have some errors going on here in the insertion of items in a binary tree I don\'t why it doesn\'t work
this is the
root
is always null because you never assign value to it.
you can add to the beginning of your method check and assign it
public TNode insertNode(TNode item, int d){
if(item == null){
TNode node = new TNode(d);
if (root == null) {
root = node;
}
return node
}
... rest of method isn't changed...
Also when you are recursing you should make a call with a proper child node instead of always calling with item
, so for example first case would be:
item.left = insertNode(item.left, d);
For second case you would just use item.right
instead.