Switch to full style
Java Collections classes examples .
Post a reply

Java Binary Tree

Mon Apr 04, 2011 4:56 pm

Java Binary Tree example
Code:

public class BinaryTree 
{

    static class TreeNode {

        TreeNode leftReference;
        TreeNode rightReference;
        int nodeValue;

        public TreeNode(int nodeValue) {
            this.nodeValue = nodeValue;
        }
    }

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

    public void run() {
        // tree root node.
        int rootValue = 40;
        TreeNode rootnode = new TreeNode(rootValue);
        System.out.println("root node created. " + rootnode.nodeValue);

        // insertNode new element starting with rootnode.
        insertNode(rootnode, 11);
        insertNode(rootnode, 15);
        insertNode(rootnode, 16);
        insertNode(rootnode, 23);
        insertNode(rootnode, 79);
        System.out.println("print the content of  tree in  binary tree order");

        printTree(rootnode);

    }

    /*
     *
     *  inserting new parentNode to tree.
     *  binary tree set the smaller nodeValue on left and the
        bigger nodeValue on the right
     * parent TreeNode in the first case will be root node.
     */
    public void insertNode(TreeNode parentNode, int nodeValue) {
        if (nodeValue < parentNode.nodeValue) {
            if (parentNode.leftReference != null) {
                 // Go more depth to left.
                insertNode(parentNode.leftReference, nodeValue);
            } else {
                System.out.println(" LEFT:   new node value " + nodeValue
                        
+ "  , its root  " + parentNode.nodeValue);
                parentNode.leftReference = new TreeNode(nodeValue);
            }
        } else if (nodeValue > parentNode.nodeValue) {
            
            if 
(parentNode.rightReference != null) {
                // Go more depth to right
                insertNode(parentNode.rightReference, nodeValue);
            } else {
                System.out.println("  Right: new node value  " + nodeValue + ", its root " + parentNode.nodeValue);
                parentNode.rightReference = new TreeNode(nodeValue);
            }
        }
    }

    /*
     *
     * recursivly printing the content of the tree.
     */
    public void printTree(TreeNode node) {
        if (node != null) {
            printTree(node.leftReference);
            System.out.println("  node value  " + node.nodeValue);
            printTree(node.rightReference);
        }
    }
}
 




Re: Java Binary Tree

Wed May 30, 2012 7:14 pm

Your program has a static nested class TreeNode which has left, right member variables of type TreeNode. Doesn't this cause out of memory error while initializing an object of type Node as vm tries to allocate memory to TreeNode objects recursively.

Post a reply
  Related Posts  to : Java Binary Tree
 need help for creatinb binary tree in php     -  
 Binary search tree C++     -  
 Take tree nodes then create new tree with another root     -  
 java tree problem     -  
 java code for decision tree algorithm     -  
 Java code to draw shortest path tree     -  
 binary search     -  
 decision tree     -  
 Read Binary File in C++     -  
 convert string into binary     -  

Topic Tags

Java Collections