Insertion in binary search tree (BST) in Java
Binary trees with a certain ordering attribute are called binary search trees (BSTs). In a BST, all nodes in a given node’s left subtree have values that are less than that node’s value, and all nodes in the node’s right subtree have values that are higher than that node’s value. Searching, insertion, and deletion operations may all be done effectively thanks to this ordering characteristic.
There are many applications that use BSTs for fast lookups and retrievals. Spell checkers, address lookup and packet forwarding in routers, game development, and data compression, all rely on BSTs.
Now, let’s focus on the insertion operation in a BST.
Insertion in a binary search tree
To insert a value in a BST:
- Add the specified value to a new node.
- Make the new node the root if the tree is empty.
- Otherwise, begin at the base and work our way up the tree using the guidelines below:
- Go to the
leftsubtree if the value is smaller than the value of the current node. - Go to the
rightsubtree if the value is higher than the value of the current node. - This approach should be repeated until a suitable opening is discovered.
- Go to the
- Place the new node in the vacant space.
Code example
Here is the Java implementation of the insertion in the BST:
class Node {int data;Node left, right;public Node(int data) {this.data = data;left = right = null;}}class BST {Node root;public BST() {root = null;}public void insertValue(int data) {root = insertNode(root, data);}private Node insertNode(Node root, int data) {if (root == null) {root = new Node(data);return root;}if (data < root.data) {root.left = insertNode(root.left, data);} else if (data > root.data) {root.right = insertNode(root.right, data);}return root;}public void printTree(Node node) {if (node != null) {printTree(node.left);System.out.print(node.data + " ");printTree(node.right);}}}public class Main {public static void main(String[] args) {BST tree = new BST();tree.insertValue(50);tree.insertValue(30);tree.insertValue(20);tree.insertValue(40);tree.insertValue(70);tree.insertValue(60);tree.insertValue(80);System.out.println("In-order traversal of the BST:");tree.printTree(tree.root);}}
Explanation
Let’s understand the above code implementation:
- Lines 1–9: We define the
Nodeclass, which stands for a binary tree node.- Line 2: We define an integer
datato store the value of the node. - Line 3: We declare two references of type
Nodewith the namesleftandrightto link the current node’sleftandrightchild nodes, respectively. - Lines 5–7: We define the
Nodeclass constructor. In this constructor, we initialize the node with the value supplied as an integer parameter nameddata. We set theleftandrightchild references tonull.
- Line 2: We define an integer
- Lines 11–35: We define a class named
BST.- Line 12: We declare a
Nodevariable namedroot. - Lines 14–16: We define the
BSTclass constructor and set therootvariable tonullto represent an empty tree. - Lines 18–20: We define the
inserValue()method for putting a new node with the specified value into the binary search tree. Inside this method, we call theinsertNode()method passingrootanddatavariables. - Lines 22–35: We define the
insertNode()method that accepts two arguments:dataandroot. If therootisnull, a newNodeis created with the given data and returned. If thedatais less than the current node’s data, the method recursively calls itself with the left child. If thedatais greater than the current node’s data, the method recursively calls itself with the right child. Lastly, the current node is returned. - Lines 37–43: We define the
printTree()method that performs an in-order traversal of the tree. In this method, If the current node is notnull, the method recursively calls itself with the left child, prints the data of the current node, and recursively calls itself with the right child.
- Line 12: We declare a
- Line 48: We create an instance named
treeof theBSTclass. - Lines 49–55: We insert several values into the
treeusing theinsertValue()method. - Lines 57–58: We call the
printTree()method on thetreeobject, passing therootof the tree to print its elements in the in-order traversal.
Free Resources