Total members 11893 |It is currently Fri Nov 01, 2024 9:34 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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);
        }
    }
}
 




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

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.



Author:
Post new topic Reply to topic  [ 2 posts ] 

  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     -  
 decision tree     -  
 binary search     -  
 Read Binary File in C++     -  
 convert string into binary     -  



Topic Tags

Java Collections
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com