Java program to implement binary tree data structure

Here's a Java program that implements a binary tree data structure:

public class BinaryTree {
    private TreeNode root;

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

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

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

    private TreeNode insert(TreeNode node, int data) {
        if (node == null) {
            node = new TreeNode(data);
        } else {
            if (data <= node.data) {
                node.left = insert(node.left, data);
            } else {
                node.right = insert(node.right, data);
            }
        }
        return node;
    }

    public void traverseInorder() {
        traverseInorder(root);
        System.out.println();
    }

    private void traverseInorder(TreeNode node) {
        if (node != null) {
            traverseInorder(node.left);
            System.out.print(node.data + " ");
            traverseInorder(node.right);
        }
    }

    public void traversePreorder() {
        traversePreorder(root);
        System.out.println();
    }

    private void traversePreorder(TreeNode node) {
        if (node != null) {
            System.out.print(node.data + " ");
            traversePreorder(node.left);
            traversePreorder(node.right);
        }
    }

    public void traversePostorder() {
        traversePostorder(root);
        System.out.println();
    }

    private void traversePostorder(TreeNode node) {
        if (node != null) {
            traversePostorder(node.left);
            traversePostorder(node.right);
            System.out.print(node.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);

        System.out.print("Inorder traversal: ");
        tree.traverseInorder();

        System.out.print("Preorder traversal: ");
        tree.traversePreorder();

        System.out.print("Postorder traversal: ");
        tree.traversePostorder();
    }
}
Sourc‮.www:e‬theitroad.com

In this program, we define a TreeNode class to represent the individual nodes in the binary tree. Each node has an integer data value, and left and right pointers to its left and right child nodes, respectively.

We also define a BinaryTree class, which has a root pointer to the top node of the tree. The insert method takes an integer value and inserts it into the binary tree in the correct location, maintaining the binary search tree property that all nodes to the left of a node have values less than the node's value, and all nodes to the right have values greater than the node's value.

We also define three traversal methods - traverseInorder, traversePreorder, and traversePostorder - which perform an inorder, preorder, or postorder traversal of the binary tree, respectively. The traverseInorder method prints the values of the tree's nodes in increasing order, while traversePreorder and traversePostorder print the values in preorder and postorder, respectively.

In the main method, we create a BinaryTree object and insert several values into it. We then call each of the three traversal methods to print out the values of the tree's nodes in the desired order.