Binary Search Tree Implementation in Java

1. Introduction

A Binary Search Tree (BST) is a special form of a Binary Tree wherein for every node, the nodes to the left are lesser than the current node and the nodes to the right are greater than the current node. This property provides an efficient way to search, insert, and delete values in the tree. The left and right subtree each must also be a binary search tree. 

The BST has applications in database systems for indexing, in computer graphics, and various algorithms due to its balanced nature and efficient search capabilities.

2. Implementation Steps

1. Define a Node class to represent individual elements of the BST.

2. Define a BinarySearchTree class which will:

a. Maintain a reference root marking the start of the tree.

b. Provide methods such as insert(), inOrderTraversal(), and search().

3. Implement these methods to manage the BST.

4. Create an instance of the BinarySearchTree class and demonstrate its functionality within a main() method.

3. Implementation in Java

class Node {
    int data;
    Node left, right;
    Node(int data) {
        this.data = data;
        left = right = null;
    }
}
class BinarySearchTree {
    Node root;
    public BinarySearchTree() {
        root = null;
    }
    // Insert a new node in the BST
    public void insert(int data) {
        root = insertRec(root, data);
    }
    private Node insertRec(Node root, int data) {
        if (root == null) {
            root = new Node(data);
            return root;
        }
        if (data < root.data) {
            root.left = insertRec(root.left, data);
        } else if (data > root.data) {
            root.right = insertRec(root.right, data);
        }
        return root;
    }
    // Inorder traversal of the BST
    public void inOrderTraversal() {
        inOrderRec(root);
    }
    private void inOrderRec(Node root) {
        if (root != null) {
            inOrderRec(root.left);
            System.out.print(root.data + " ");
            inOrderRec(root.right);
        }
    }
    // Search for a value in the BST
    public boolean search(int data) {
        return searchRec(root, data);
    }
    private boolean searchRec(Node root, int data) {
        if (root == null) {
            return false;
        }
        if (root.data == data) {
            return true;
        }
        return data < root.data ? searchRec(root.left, data) : searchRec(root.right, data);
    }
    public static void main(String[] args) {
        BinarySearchTree bst = new BinarySearchTree();
        bst.insert(50);
        bst.insert(30);
        bst.insert(70);
        bst.insert(20);
        bst.insert(40);
        bst.insert(60);
        bst.insert(80);
        bst.inOrderTraversal();  // Outputs: 20 30 40 50 60 70 80
        System.out.println("\nIs 40 in the BST? " + bst.search(40));  // Outputs: true
    }
}

Output:

20 30 40 50 60 70 80
Is 40 in the BST? true

Explanation:

1. The Node class is the basic unit of our BST. It contains a data value and references to its left and right children.

2. The BinarySearchTree class houses the root reference, which marks the starting point of our tree.

3. insert(int data) method provides a means to add new nodes to our BST. Given the properties of the BST, it utilizes the insertRec() helper method to recursively find the correct placement for the new node.

4. inOrderTraversal() offers an in-order traversal of the BST. It uses the helper method inOrderRec() to sequentially visit the left subtree, current node, and right subtree.

5. The search(int data) method facilitates checking the existence of a value in the BST. With the help of the searchRec() method, it recursively scans the appropriate branches of the tree to find a match.

6. In the main() method, we instantiate a BinarySearchTree, insert several integer values, and then showcase traversal and search operations.


Comments