Binary Search Tree Program in Java

Binary Search Tree Program

Binary Search Tree Program in Java helps you understand how to implement one of the most important tree data structures used in searching and sorting problems.

A Binary Search Tree (BST) is a special type of binary tree where:

  • Left subtree contains values less than root
  • Right subtree contains values greater than root
  • This property makes searching, insertion, and deletion efficient.
Binary Search Tree Program in Java

Binary Search Tree Program in Java

What is a Binary Search Tree?

A Binary Search Tree (BST) is a binary tree with an ordering property:

Left < Root < Right

Example BST:

        50
       /  \
     30    70
    / \    / \
  20  40  60  80

Representation of Binary Search Tree:

  1. The representation of a BST is similar to that of a binary tree. The order of a BST is ‘2’. Each node can have at most two children.
  2. Only difference between the two is that there is a certain criteria of arrangement or insertion of the elements based on their comparisons with the root node and the sub tree segment they are added to.
Binary Search Tree

Operations in a Binary Search Tree:

  1. Traversal
  2. Searching
  3. Insertion
  4. Deletion
Binary Search Tree 1

Algorithms for Binary Search Tree

Java Code for Binary Search Tree

Run
class Node {
    int data;
    Node left, right;

    Node(int value) {
        data = value;
        left = right = null;
    }
}

public class BinarySearchTree {

    Node root;

    // Insert node
    Node insert(Node root, int value) {

        if (root == null) {
            return new Node(value);
        }

        if (value < root.data) {
            root.left = insert(root.left, value);
        } else if (value > root.data) {
            root.right = insert(root.right, value);
        }

        return root;
    }

    // Search node
    boolean search(Node root, int key) {

        if (root == null)
            return false;

        if (root.data == key)
            return true;

        if (key < root.data)
            return search(root.left, key);
        else
            return search(root.right, key);
    }

    // Find minimum value node
    Node minValue(Node root) {

        while (root.left != null)
            root = root.left;

        return root;
    }

    // Delete node
    Node delete(Node root, int key) {

        if (root == null)
            return null;

        if (key < root.data) {
            root.left = delete(root.left, key);
        } else if (key > root.data) {
            root.right = delete(root.right, key);
        } else {

            // Case 1: No child
            if (root.left == null && root.right == null)
                return null;

            // Case 2: One child
            if (root.left == null)
                return root.right;

            if (root.right == null)
                return root.left;

            // Case 3: Two children
            Node successor = minValue(root.right);
            root.data = successor.data;
            root.right = delete(root.right, successor.data);
        }

        return root;
    }

    // Inorder traversal
    void inorder(Node root) {

        if (root != null) {
            inorder(root.left);
            System.out.print(root.data + " ");
            inorder(root.right);
        }
    }

    public static void main(String[] args) {

        BinarySearchTree bst = new BinarySearchTree();

        bst.root = bst.insert(bst.root, 50);
        bst.insert(bst.root, 30);
        bst.insert(bst.root, 70);
        bst.insert(bst.root, 20);
        bst.insert(bst.root, 40);
        bst.insert(bst.root, 60);
        bst.insert(bst.root, 80);

        System.out.print("Inorder Traversal: ");
        bst.inorder(bst.root);

        System.out.println("\nSearch 40: " + bst.search(bst.root, 40));

        bst.root = bst.delete(bst.root, 20);

        System.out.print("After Deletion (20): ");
        bst.inorder(bst.root);
    }
}

Input:

Insert: 50, 30, 70, 20, 40, 60, 80
Search: 40
Delete: 20

Output:

Inorder Traversal: 20 30 40 50 60 70 80
Search 40: true
After Deletion (20): 30 40 50 60 70 80

Advantages Of BST:

  1. Inorder Traversal gives sorted order of elements.
  2. Easy to implement order statistics.
  3. Helpful in range queries.

Disadvantages Of BST:

  1. The cost of operations may be high.
  2. Shape of tree depends on insetions and may be degenerated.

To wrapping up with

The Conditions of BST:

  • Duplicate values should be handled properly
  • Tree can become skewed (degrades performance)
  • Check null before operations

Common problems with Binary Search Tree:

  • Ignoring BST property
  • Incorrect deletion logic (especially 2 children case)
  • Not updating root after deletion
  • Infinite recursion due to wrong conditions

Frequently Asked Questions

Answer:

A BST is a binary tree where left child < root < right child.

Answer:

Average case is O(log n), worst case is O(n).

Answer:

It returns elements in sorted order.

Answer:

It returns elements in sorted order.

Answer:

No child, one child, and two children.

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription