Java binary tree insert

The Binary Tree insert is specialized use of binary tree. The concept lies behind is that all the element value less than the root node value insert left to the root node and the element value greater than the root node insert right to this root node.

Java binary tree insert

The Binary Tree insert is specialized use of binary tree. The concept lies behind is that all the element value less than the root node value insert left to the root node and the element value greater than the root node insert right to this root node.

Java binary tree insert

Java binary tree insert

     

The Binary Tree insert is specialized use of binary tree. The concept lies behind is that all the element value less than the root node value insert left to the root node and the element value greater than the root node insert right to this root node.

This module implements a binary search tree, which is a specialized usage of a binary tree. The basic principle is that all elements to the left are less than the root, all elements to the right are greater than the root. This reduces the search time for elements in the tree, by halving the number of nodes that need to be searched each time a node is examined.Binary Tree find its extensive application in games and is well known in date-structure. Trees are recursive data-structure and therefore tend to recursive traversal function 

Understand with Example

In this Tutorial we want to describe you a code that helps you in understanding a code  Java binary tree insert. For this we have a class Binary Tree Insert, Inside the main class we instantiate Binary tree insert class, that call a run method. Inside this we have a static class node declared a variable node left ,right and int value to store the node value. The node constructor accept a value passed as argument. The run method creates root node object assign with root value of node is 25.The Println print the root value of the node. Further the insert method insert the different root node value.The conditional operator evaluate the root node value, if the value element is less than the root value  insert the node value to the left of root node else the value of the element insert right to the root node.

new Node( 25) -This is used to create all its new node 

BinarytreeInsert.java

public class BinarytreeInsert {

    public static void main(String[] args) {
        new BinarytreeInsert().run();
    }

    static class Node {

        Node left;
        Node right;
        int value;

        public Node(int value) {
            this.value = value;
        }
    }

    public void run() {
        Node rootnode = new Node(25);
        System.out.println("Building tree with root value " + rootnode.value);
        System.out.println("=================================");
        insert(rootnode, 11);
        insert(rootnode, 15);
        insert(rootnode, 16);
        insert(rootnode, 23);
        insert(rootnode, 79);

    }
    

    public void insert(Node node, int value) {
        if (value < node.value) {
            if (node.left != null) {
                insert(node.left, value);
            } else {
                System.out.println("  Inserted " + value + " to left of Node " + node.value);
                node.left = new Node(value);
            }
        } else if (value > node.value) {
            if (node.right != null) {
                insert(node.right, value);
            } else {
                System.out.println("  Inserted " + value + " to right of Node " + node.value);
                node.right = new Node(value);
            }
        }
    }
}

Output of the program

  Building tree with root value 25
=================================
  Inserted 11 to left of Node 25
  Inserted 15 to right of Node 11
  Inserted 16 to right of Node 15
  Inserted 23 to right of Node 16
  Inserted 79 to right of Node 25

Download Source code