Skip to content

Tree binary tree

Understanding Binary Tree

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. Binary trees are used in various applications such as representing hierarchical data, searching and sorting, and in algorithms like Huffman coding and expression parsing.

Java Implementation of a Binary Tree

Here's how you can implement and use a binary tree in Java:

Node Class

class Node {
    int data;
    Node left, right;

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

BinaryTree Class with Basic Operations

public class BinaryTree {
    Node root;

    public BinaryTree() {
        root = null;
    }

    // Inorder Traversal
    void inorder(Node node) {
        if (node == null)
            return;

        // Recur on the left child
        inorder(node.left);

        // Print the data of the node
        System.out.print(node.data + " ");

        // Recur on the right child
        inorder(node.right);
    }

    // Preorder Traversal
    void preorder(Node node) {
        if (node == null)
            return;

        // Print the data of the node
        System.out.print(node.data + " ");

        // Recur on the left child
        preorder(node.left);

        // Recur on the right child
        preorder(node.right);
    }

    // Postorder Traversal
    void postorder(Node node) {
        if (node == null)
            return;

        // Recur on the left child
        postorder(node.left);

        // Recur on the right child
        postorder(node.right);

        // Print the data of the node
        System.out.print(node.data + " ");
    }

    // Wrapper for inorder traversal
    void inorder() {
        inorder(root);
    }

    // Wrapper for preorder traversal
    void preorder() {
        preorder(root);
    }

    // Wrapper for postorder traversal
    void postorder() {
        postorder(root);
    }

    // Insert a new node
    void insert(int data) {
        root = insertRec(root, data);
    }

    // Recursive insert function
    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;
    }

    public static void main(String[] args) {
        BinaryTree tree = new BinaryTree();

        /* Let us create the following BST
                  50
               /     \
              30      70
             /  \    /  \
            20   40  60   80 */
        tree.insert(50);
        tree.insert(30);
        tree.insert(20);
        tree.insert(40);
        tree.insert(70);
        tree.insert(60);
        tree.insert(80);

        System.out.println("Inorder traversal:");
        tree.inorder();

        System.out.println("\nPreorder traversal:");
        tree.preorder();

        System.out.println("\nPostorder traversal:");
        tree.postorder();
    }
}

Explanation

  1. Node Class: ```java class Node { int data; Node left, right;

    public Node(int item) { data = item; left = right = null; } } `` This class represents a node in the binary tree, containing an integerdata`, and pointers to the left and right children.

  2. BinaryTree Class:

  3. Inorder Traversal: java void inorder(Node node) { if (node == null) return; inorder(node.left); System.out.print(node.data + " "); inorder(node.right); } This method prints the nodes of the tree in an inorder sequence (left-root-right).

  4. Preorder Traversal: java void preorder(Node node) { if (node == null) return; System.out.print(node.data + " "); preorder(node.left); preorder(node.right); } This method prints the nodes of the tree in a preorder sequence (root-left-right).

  5. Postorder Traversal: java void postorder(Node node) { if (node == null) return; postorder(node.left); postorder(node.right); System.out.print(node.data + " "); } This method prints the nodes of the tree in a postorder sequence (left-right-root).

  6. Insert Method: java void insert(int data) { root = insertRec(root, data); } This method inserts a new node into the binary tree.

  7. Recursive Insert Function: java 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; } This is a helper method for inserting a new node recursively.

  8. Main Method: ```java public static void main(String[] args) { BinaryTree tree = new BinaryTree();

    tree.insert(50); tree.insert(30); tree.insert(20); tree.insert(40); tree.insert(70); tree.insert(60); tree.insert(80);

    System.out.println("Inorder traversal:"); tree.inorder();

    System.out.println("\nPreorder traversal:"); tree.preorder();

    System.out.println("\nPostorder traversal:"); tree.postorder(); } ``` This method creates a binary tree, inserts nodes, and performs inorder, preorder, and postorder traversals, printing the results.

Conclusion

This example demonstrates the basic operations of a binary tree, including insertion and different types of traversal (inorder, preorder, and postorder). The BinaryTree class in Java provides a clear and simple way to implement and work with binary trees, allowing for efficient data storage and retrieval.