Binary Search Tree Node Insertion
First define a binary search tree, expressed in Java code as follows:
public
class
BST
<
Key
extends
Comparable
<
Key, Value
{
// The nodes in the tree are a private class; the outside world does not need to know the specific implementation of binary search tree nodes
private
class
Node
{
private
Key
key
;
private
Value value
;
private
Node left, right
;
public
Node
(
Key
key, Value value
)
{
this
.
key
=
key
;
this
.
value
=
value
;
left
=
right
=
null
;
}
}
// Root node
private
Node root
;
// Number of nodes in the tree
private
int
count
;
// Constructor, by default constructs an empty binary search tree
public
BST
(
)
{
root
=
null
;
count
=
0
;
}
// Return the number of nodes in the binary search tree
public
int
size
(
)
{
return
count
;
}
// Return whether the binary search tree is empty
public
boolean
isEmpty
(
)
{
return
count
==
0
;
}
}
Node represents a node, and count represents the number of nodes.
The following example shows the steps to insert element 61 into the binary search tree below:

(1) The element 61 to be inserted is greater than 42, so compare with the root node of 42's right subtree.

(2) 61 is greater than 59, so 61 needs to be moved to the corresponding position in 59's right subtree, which is empty at this point, so it is directly inserted as the right child of 59.

The insertion operation is also a recursive process, with three cases: equal to, greater than, and less than.
Java Example Code
Source code package download:Download
src/runoob/binary/BinarySearchTreeInsert.java file code:
package
runoob.binary
;
/**
* Binary Search Tree Inserting a New Element
*/
public
class
BinarySearchTreeInsert
<
Key
extends
Comparable
<
Key, Value
{
// The nodes in the tree are a private class; the outside world does not need to know the specific implementation of binary search tree nodes
private
class
Node
{
private
Key
key
;
private
Value value
;
private
Node left, right
;
public
Node
(
Key
key, Value value
)
{
this
.
key
=
key
;
this
.
value
=
value
;
left
=
right
=
null
;
}
}
private
Node root
;
// Root node
private
int
count
;
// Number of nodes in the tree
// Constructor, by default constructs an empty binary search tree
public
BinarySearchTreeInsert
(
)
{
root
=
null
;
count
=
0
;
}
// Return the number of nodes in the binary search tree
public
int
size
(
)
{
return
count
;
}
// Return whether the binary search tree is empty
public
boolean
isEmpty
(
)
{
return
count
==
0
;
}
// Insert a new (key, value) data pair into the binary search tree
public
void
insert
(
Key
key, Value value
)
{
root
=
insert
(
root, key, value
)
;
}
// Core code---start
// Insert a node (key, value) into the binary search tree rooted at node, using a recursive algorithm
// Return the root of the binary search tree after inserting the new node
private
Node insert
(
Node node,
Key
key, Value value
)
{
if
(
node
==
null
)
{
count
++;
return
new
Node
(
key, value
)
;
}
if
(
key.
compareTo
(
node.
key
)
==
0
)
node.
value
=
value
;
else
if
(
key.
compareTo
(
node.
key
)
<
0
)node.
left
=
insert
(
node.
left
, key, value
)
;
else
// key > node->key
node.
right
=
insert
(
node.
right
, key, value
)
;
return
node
;
}
// Core code---end
}