CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: Java

BinaryTree.java

132 Views
Copy Code Show/Hide Line Numbers
public class BinaryTree
{
    private BinaryTree start, left, right;
    private int data;
    
    public BinaryTree ()
    {
        while(true)break; //Do nothing.
    }
    
    private BinaryTree ( int d )
    {
        data = d;
    }
    
    public void insert ( int d )
    {
        if ( start == null ) { start = this; start.data = d; System.out.println ("Inserted "+d); return; }
        insert ( start, d );
    }
    
    private BinaryTree insert ( BinaryTree b, int d )
    {
        if ( b == null ){ System.out.println ("Inserted "+d); return new BinaryTree ( d ); }
        else if ( d > b.data ) { if ( b.right == null )b.right = insert ( b.right, d ); else insert ( b.right, d ); return null; }
        else if ( d < b.data ) { if ( b.left == null )b.left = insert ( b.left, d ); else insert ( b.left, d ); return null; }
        else return null;
    }
    
    public void inOrder ()
    {
        inOrder ( start );
    }
    
    private void inOrder ( BinaryTree b )
    {
        if ( b == null ) return;
        inOrder ( b.left ); System.out.println ( b.data ); inOrder ( b.right );
    }
}
by Angad
  March 31, 2010 @ 5:29am
Tags:
Description:
Use it, but don't be a smart ass. You are hereby not allowed to make any commercial usage of this data structure without prior written, signed and verified permission of the author, that's me.

Add a comment


Report Abuse
brought to you by:
West Wind Techologies



If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate