Data Structure & Algorithm in Java

⌘K
  1. Home
  2. Docs
  3. Data Structure & Alg...
  4. Trees
  5. Implementing Binary Trees

Implementing Binary Trees

Here is the detailed explanation of Implementing Binary Trees

A binary tree is a non-linear hierarchical data structure consisting of a collection of nodes that stores data forming a hierarchy. It does not store data sequentially as data structures like Arrays, Linked lists, Queues and Stacks do. Instead, it stores data at multiple levels where each level can store at most 2 children nodes.

Implementing a binary tree in Java involves creating classes to represent nodes and the binary tree itself, along with methods for common operations such as insertion, deletion, and searching.

Below are the basic steps to implement a binary tree in Java:

Create a class to represent the nodes of the binary tree. Each node should have references to its left and right children, as well as the data it stores.

class TreeNode {
    int data;
    TreeNode left;
    TreeNode right;

    public TreeNode(int data) {
        this.data = data;
        this.left = null;
        this.right = null;
    }
}

Create a class to represent the binary tree itself. This class should have a reference to the root node and methods to perform tree operations like insertion, deletion, and searching.

class BinaryTree {
    private TreeNode root;

    public BinaryTree() {
        this.root = null;
    }

    // Methods for tree operations (insert, delete, search) will go here
}

Write a method to insert a new node into the binary tree. This method should traverse the tree to find the appropriate position for the new node based on the binary search tree property (left child < parent < right child).

public void insert(int data) {
    root = insertRecursive(root, data);
}

private TreeNode insertRecursive(TreeNode root, int data) {
    if (root == null) {
        return new TreeNode(data);
    }

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

    return root;
}

Write a method to search for a value in the binary tree. This method should traverse the tree recursively, comparing the target value with the data in each node.

public boolean search(int data) {
    return searchRecursive(root, data);
}

private boolean searchRecursive(TreeNode root, int data) {
    if (root == null) {
        return false;
    }

    if (data == root.data) {
        return true;
    } else if (data < root.data) {
        return searchRecursive(root.left, data);
    } else {
        return searchRecursive(root.right, data);
    }
}

Write a method to delete a node from the binary tree. This method should handle different cases depending on whether the node to be deleted is a leaf node, has one child, or has two children.

public void delete(int data) {
    root = deleteRecursive(root, data);
}

private TreeNode deleteRecursive(TreeNode root, int data) {
    if (root == null) {
        return null;
    }

    if (data < root.data) {
        root.left = deleteRecursive(root.left, data);
    } else if (data > root.data) {
        root.right = deleteRecursive(root.right, data);
    } else {
        // Node to delete found
        if (root.left == null) {
            return root.right;
        } else if (root.right == null) {
            return root.left;
        }

        // Node has two children
        root.data = minValue(root.right);
        root.right = deleteRecursive(root.right, root.data);
    }

    return root;
}

private int minValue(TreeNode root) {
    int minVal = root.data;
    while (root.left != null) {
        minVal = root.left.data;
        root = root.left;
    }
    return minVal;
}

How can we help?

Leave a Reply

Your email address will not be published. Required fields are marked *