Binary Tree Implementation in Java

1. Introduction

A binary tree is a hierarchical data structure in which each node has at most two children, referred to as the left child and the right child. The top node in a binary tree is called the root. Unlike arrays, linked lists, stacks, and queues, which are linear data structures, trees are hierarchical. 

Binary trees have applications in search algorithms, hierarchical data representation, path-finding algorithms, and more.

2. Implementation Steps

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

2. Define a BinaryTree class which will:

a. Have a reference root pointing to the start of the tree.

b. Provide methods like insert(), inOrderTraversal(), and search().

3. Implement these methods to manage the binary tree.

4. Instantiate the BinaryTree class and perform operations in a main() method to demonstrate its functionality.

3. Implementation in Java

class Node {
    int data;
    Node left, right;
    Node(int data) {
        this.data = data;
        left = right = null;
    }
}
class BinaryTree {
    Node root;
    public BinaryTree() {
        root = null;
    }
    // Insert a new node in the binary tree
    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 binary tree
    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 binary tree
    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) {
        BinaryTree tree = new BinaryTree();
        tree.insert(50);
        tree.insert(30);
        tree.insert(70);
        tree.insert(20);
        tree.insert(40);
        tree.insert(60);
        tree.insert(80);
        tree.inOrderTraversal();  // Outputs: 20 30 40 50 60 70 80
        System.out.println("\nIs 40 in the tree? " + tree.search(40));  // Outputs: true
    }
}

Output:

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

Explanation:

1. The Node class represents each element of the binary tree. Each node has an integer data value and two references: left and right.

2. The BinaryTree class maintains a root reference, which is the topmost node of the tree.

3. insert(int data) method allows adding a new node to the tree. This method uses a recursive helper, insertRec(), to find the appropriate position for the new node.

4. inOrderTraversal() performs an in-order traversal of the tree and prints the nodes. It uses the recursive helper method inOrderRec() to visit nodes in the left subtree, the current node, and then nodes in the right subtree.

5. search(int data) checks if a value exists in the tree. The searchRec() helper method is used to perform this operation recursively.

6. The main() method initializes a BinaryTree, adds several integer values, and then demonstrates traversal and search functionalities.


Comments