I\'m doing a small Java work on Binary Search Tree but when I\'m implementing the recursive insert of a node into the tree and display it, I don\'t get anything. I\'ve been on i
Your left, right element of the BSTNodes are null. You need to create them before inserting into it. Otherwise they create an empty hanging BSTNode() and insert it, without connecting to the rest of the tree.
You can change the lines,
if ( elem.compareTo( root.element ) < 0 ) {
insertR( root.left, elem );
} else {
insertR( root.right, elem );
}
to
if ( elem.compareTo( root.element ) < 0 ) {
if ( root.left == null )
root.left = new BSTNode( elem );
else
insertR( root.left, elem );
} else {
if ( root.right == null )
root.right = new BSTNode( elem );
else
insertR( root.right, elem );
}
This sets the BST root and allow the class to work properly, change
if ( root == null ) {
root = new BSTNode( elem );
}
to
if ( root == null ) {
root = new BSTNode( elem );
if ( root.element != null ) {
this.root = root;
}
}
Though recursive insertion into BST seems trivial, it's very easy to make an argument passing mistake that'd nullify your attempts. To recursively call the insertion function, you must pass the object BSTNode root
as one of the argument AND make changes to its content instead of what it points to.
In the following code, root = new BSTNode( elem );
only changes the address that the local variable root
is pointing at, and the change is not visible to this.root
. Before the assignment, root
has a value of null
, which was value-copied from this.root
when the BST was empty. After the assignment, root
points at some newly created BSTNode
with data from elem
. Intention of the code is to make this.root
point to the same new BSTNode
, yet there is no way to achieve that by making the local root
point to it. When insertR
returns, this.root
still points to null
!!
Same mistakes happen to insertR(root.left, elem);
and insertR(root.right, elem);
. Arguments are passed, but since no content changes are made, the function call would just return with no information updated to the BST.
public void insert(Comparable elem) {
insertR(this.root, elem);
}
public void insertR( BSTNode root, Comparable elem ) {
if ( root == null ) {
root = new BSTNode( elem );
}
else {
if ( elem.compareTo( root.element ) < 0 ) {
insertR( root.left, elem );
} else {
insertR( root.right, elem );
}
}
}
To recursively change a BST, changes shall be made towards the variable(s) an object contains instead of what the object variable points at. Thus when this.root
is null
, you should handle the first insertion somewhere else instead of inside insertR
, like the following:
public void insert(Comparable elem) {
if (this.root == null) {
this.root = new BSTNode(elem);
} else {
insertR(this.root, elem);
}
}
The code by Arcturus for additional insertions was correct because the changes made to the contents of root
immediately spreads to this.root
, as they are point at the same BSTNode
object. Of course here root
is required not to be null
, which is satisfied because null
case has been handled in the code above.
if ( elem.compareTo( root.element ) < 0 ) {
if ( root.left == null )
root.left = new BSTNode( elem );
else
insertR( root.left, elem );
} else {
if ( root.right == null )
root.right = new BSTNode( elem );
else
insertR( root.right, elem );
}