Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Java-get-example Java binary tree insert

Related Tutorials


 
 

Share on Google+Share on Google+

Java binary tree insert

Advertisement
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.

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

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 6, 2008

Related Tutorials

Discuss: Java binary tree insert   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:3
christopher carin
October 10, 2011
data stucture

thanks
janitra
January 13, 2012
asking

can I ask for explanation of this coding? 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); }
ian
February 23, 2012
kon

why it is nit running in com. promt? the error is at the first line..
DMCA.com