About Me

My photo
"Enoughtheory.com" had its humble beginning in the year 2011 by ( Founder of Enoughtheory.com ) Mr Ravi Kant Soni , an Enterprise Java and Spring Framework Specialist, with a bachelor degree (B.E) in Information Science and Engineering from Reva Institute of Technology at Bangalore. He has been into the software development discipline for many years now. Ravi has worn many hats throughout his tenure, ranging from software development, designing multi-tenant applications, integration of new technology into an existing system, to his current love of writing a Spring Framework book. Currently, he is a lead engineer at HCL Technology. Ravi has focused on Web and Enterprise development using Spring Framework for most of his career and has been extensively involved in application design and implementation. He has developed applications for Core-Bank, HR and Payroll System, and e-Commerce systems using Spring Framework. Ravi Kant Soni is author of book "Learning Spring Application development" http://learningspringapplicationdevelopment.com

Friday 2 August 2013

Binary Tree Example In Java



OUTPUT

----Binary Tree Example--------
Binar Tree with Root Value: 25
--------------------------
inserted value to left of node11
inserted value to right of node14
inserted value to right of node55
inserted value to right of node93
inserted value to right of node93
inserted value to right of node44
inserted value to left of node44
-----------------------------
Traversing order
Traversed Order 11
Traversed Order 14
Traversed Order 25
Traversed Order 44
Traversed Order 55
Traversed Order 93
 


---------------------------------------------------------------

Code as
---------


/**
 * Binary Search Tree Left node will be less than parent node Right node will be
 * greater than parent node Each node has distinct key Information represented
 * by node is the object element Nodes are compared according to their keys than
 * any part of their associate records
 *
 * @author ravi
 *
 */
public class BinaryTreeExample {
    /**
     * Inside this class we have a static node class and have two static node
     * variable Node left, Node right and int value to store the respective node
     * value.
     */
    static class Node {
        Node left;
        Node right;
        int value;

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

    /**
     * Inside the main method we instantiate Binary Tree that call run( ) method
     */
    public static void main(String[] args) {
        System.out.println("----Binary Tree Example--------");
        BinaryTreeExample bTree = new BinaryTreeExample();
        bTree.run();
    }

    /**
     * The run method ( ) create an instance of node class start with node value
     * of 25
     */
    public void run() {
        Node rootnode = new Node(25);
        System.out.println("Binar Tree with Root Value: " + rootnode.value);
        System.out.println("--------------------------");
        /**
         * Add leaf values to Tree
         *
         * Information represented by node is the object element
         */
        insert(rootnode, 11);
        insert(rootnode, 14);
        insert(rootnode, 55);
        insert(rootnode, 93);
        insert(rootnode, 44);
        System.out.println("-----------------------------");
        System.out.println("Traversing order");
        printInOrder(rootnode);
    }

    /**
     * The insert method accept a node and int as argument value
     *
     * 2-->3--->4 (left->parent->right)
     *
     * @param node
     * @param value
     */
    public void insert(Node node, int value) {
        // left value will always be less than parent
        if (value < node.value) {
            // if left node exist, move inside.
            if (node.left != null) {
                insert(node.left, value);
            } else {
                System.out.println("inserted value to left of node" + value);
                /**
                 * left node object will get value
                 *
                 * create Node object with value and assign it left node
                 */
                node.left = new Node(value);
            }
        } else if (value > node.value) {
            System.out.println("inserted value to right of node" + value);
            // right node ill have greater value
            // if right node exist
            if (node.right != null) {
                insert(node.right, value);
            } else {
                node.right = new Node(value);
            }
        }

    }

    /**
     * The rootnode.value return you the value of the node
     *
     * @param node
     */
    public void printInOrder(Node node) {
        if (node != null) {
            printInOrder(node.left);
            System.out.println("Traversed Order " + node.value);
            printInOrder(node.right);
        }
    }

}

 

No comments:

Post a Comment