Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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.

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

                         

» View all related tutorials
Related Tags: c class ant help property get tutorial name ria this for to e des pe in as sta m nt

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

kkl

Posted by prabhat on Saturday, 11.29.08 @ 04:27am | #82172

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.