private
TreeNode attach( TreeNode newptr, TreeNode ptr) {
if (
ptr==null ) return newptr; // Attach new node
else
If (
(newptr.data).compareTo(ptr.data) < 0 )
// which
subtree should it go into?
ptr.left
= attach( newptr, ptr.left); // attach to left subtree
else
ptr.right = attach( newptr,
ptr.right); // attach to right subtree
return
ptr;
}
void
insert( TreeData item ) {
TreeNode
newptr = new TreeNode(); // create a new node & initialize it newptr.data
= item; // copy of item into data field
root =
attach( newptr, root ); // attach it to the tree
}
The public method insert( item ) creates a new tree node and copies item into the tree node's data field, it then passes the address of the new node to private method attach( newptr, root) which attaches the node to the tree.