-3

I have binary search tree code (It is hard code) doing ( insert , delete , max , min , sort and lookup ) and I want study efficancy for BST . I want create a random method to generate 1000 numbers rather than I enter number . How can I create this method ?

public class BinarySearchTree {


private Node root; 

    private static class Node {
        Node parent;
        Node left;
        Node right;
        int data;

        Node( int data ) {
            this.data = data;
        }

        @Override
        public String toString( ) {
            return "" + data;
        }
    }


    public void insert( int data ) {
        root = insert( root, data );
    }

    public Node insert( Node node, int data ) {
        if( node == null ) {
            node = new Node( data );
        } else if( data < node.data ) {
            node.left = insert( node.left, data );
            node.left.parent = node;
        } else {
            node.right = insert( node.right, data );
            node.right.parent = node;
        }
        return node;
    }

    private void swap( Node a, Node b ) {

        if( a.parent == null ) {
            root = b;
        } else if( a == a.parent.left ) {
            a.parent.left = b;
        } else {
            a.parent.right = b;
        }

        if( b != null ) {
            b.parent = a.parent;
        }
    }

    public void delete( int data ) {
        delete( root, data );
    }

    public void delete( Node node, int data ) {

        if( node == null ) {
            return;
        }
        else if ( data == node.data) {
            if( node.left == null ) {
                swap( node, node.right ); 
            }
            else if( node.right == null ) {
                swap( node, node.left );
            }
            else {
                Node minNode = node.right;
                while( minNode.left != null ) {
                    minNode = minNode.left;
                }
                if( minNode.parent != node ) {
                    swap( minNode, minNode.right );
                    minNode.right = node.right;
                    minNode.right.parent = minNode;
                }

                swap( node, minNode );
                minNode.left = node.left;
                minNode.left.parent = minNode;
            }
        } 
        // Continue searching in the left subtree.
        else if( data < node.data) {
            delete( node.left, data );
        } 
        // Continue searching in the right subtree.
        else {
            delete( node.right, data );
        }
    }

    public boolean lookup( int data ) {
        return lookup( root, data );
    }

    public boolean lookup( Node node, int data ) {
        if( node == null ) {
            // Can't find it.
            return false;
        } else if( data == node.data) {
            // Found it.
            return true;
        } else if( data < node.data) {
            // Search left subtree.
            return lookup( node.left, data );
        } else {
            // Search right subtree.
            return lookup( node.right, data );
        }
    }

    public int minValue( ) {
        return minValue( root );
    }

    public int minValue( Node node ) {
        Node cursor = node;
        while( cursor.left != null ) {
            cursor = cursor.left;
        }
        return cursor.data;
    }

    public int maxValue( ) {
        return maxValue( root );
    }

    public int maxValue( Node node ) {
        Node cursor = node;
        while( cursor.right != null ) {
            cursor = cursor.right;
        }
        return cursor.data;
    }

    public void inorderTraversal( ) {
        inorderTraversal( root );
    }

    private void inorderTraversal( Node node ) {
        if( node != null ) {
            inorderTraversal( node.left );
            System.out.print( node.data + " " );
            inorderTraversal( node.right );
        }
    }

    public static void main( String[ ] args ) {
        BinarySearchTree bst = new BinarySearchTree( );
        int[ ] input = new int[ ] { 5, 10, 3, 9, 7, 8 , 1 , 4 , 6 , 10};

        for( int i : input ) {
            bst.insert( i );
        }

        bst.delete( 5 );
        bst.delete( 10 );
        bst.delete( 3 );
        bst.delete( 7 );

        System.out.println( "\n Sorted :" );
        bst.inorderTraversal( );

        System.out.println( "\nMax Value:" );
        System.out.println(bst.maxValue());
        System.out.println( "\n Min Value:" );
        System.out.println(bst.minValue());

        System.out.println(bst.lookup(1));
    }
}
Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
Totoo Boo
  • 137
  • 1
  • 1
  • 7

3 Answers3

2

You might be interested in java.util.Random:

import java.util.Random;
...
Random random = new Random(System.currentTimeMillis());
random.nextInt();

Note that Random only creates pseudorandom numbers, so you'll have to use a sufficiently unique seed.

Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
  • I use this code to genarte numbers public static final void main(String [] Args){ log("Generating 10 random integers in range 0..99."); //note a single Random object is reused here Random randomGenerator = new Random(); for (int idx = 1; idx <= 10; ++idx){ int randomInt = randomGenerator.nextInt(100); log("Generated : " + randomInt); } log("Done."); } private static void log(String aMessage){ System.out.println(aMessage); } but how i can delete from it randoly and find max min value – Totoo Boo Dec 24 '11 at 16:58
1

I second the java.util.Random suggestion. Do you really want to store an array of them or do you want to just ask the Random generator for a random number between 0 and 999, 1000 times? Here's a function to get an array of them, but I would just forgo the array and loop through the Random 1000 times.

public static int[] generateRandomNumbers( int size ) {
    if ( size <= 0 ) {
        throw new IllegalArgumentException( "size must be greater than 0" );
    }
    Random random = new Random( System.currentTimeMillis() );
    int[] results = new int[ size ];
    for ( int i = 0; i < size; i++ ) {
        results[ i ] = random.nextInt( size );
    }
    return results;
}

...all other things equal, make your main like this and you've got a working program. I don't know whether it is correct or not, it works...

public static void main( String[] args ) {
    BinarySearchTree bst = new BinarySearchTree();
    int[] randoms = generateRandomNumbers( 1000 );
    for ( int i : randoms ) {
        bst.insert( i );
    }

    bst.delete( randoms[ 5 ] );
    bst.delete( randoms[ 10 ] );
    bst.delete( randoms[ 3 ] );
    bst.delete( randoms[ 7 ] );

    System.out.println( "\n Sorted :" );
    bst.inorderTraversal();

    System.out.println( "\nMax Value:" );
    System.out.println( bst.maxValue() );
    System.out.println( "\n Min Value:" );
    System.out.println( bst.minValue() );

    System.out.println( bst.lookup( randoms[ 1 ] ) );
    System.out.println( bst.lookup( randoms[ 999 ] ) );
}
Bob Kuhar
  • 10,838
  • 11
  • 62
  • 115
  • I posted my code , please help me becaeuse realy I do not have any backggreound about random genartor – Totoo Boo Dec 24 '11 at 17:15
  • See the blurb that starts "...alternatively..." above for what to do to your main() to simplify the initialization of your BinarySearchTree. You really should just take a minute or two and read the JavaDocs for java.util.Random. – Bob Kuhar Dec 24 '11 at 17:33
  • JavaDocs: http://docs.oracle.com/javase/6/docs/api/java/util/Random.html – Bob Kuhar Dec 24 '11 at 17:34
  • and by keeping the list/array of random numbers generated you can pick a random item to delete that exists in your tree. Just generate a random number and mod it against the length of the list. – bluphoenix Dec 24 '11 at 17:39
  • pleas show me this in my code then I will applied it in my second code of array beceause realy I can not get it any thing – Totoo Boo Dec 24 '11 at 17:45
  • out put come like this sorted : 0 0 1 2 2 3 4 7 8 8 8 9 11 14 16 16 ------ 994 994 995 998 Max Value: 998 Min Value: 0 true why repat number ? – Totoo Boo Dec 24 '11 at 18:47
  • Random is just Random...its not unique. – Bob Kuhar Dec 24 '11 at 20:03
  • can not find symbol method generateRandomNumbers( int ) ?? eror – Totoo Boo Dec 24 '11 at 21:01
  • Dude, you are starting to worry me. How much Java have you done? The generateRandomNumbers method is at the top of my answer, you will need to add it in there somewhere around the main. Oh, and it needs to be made static. – Bob Kuhar Dec 25 '11 at 04:10
  • I doing that before but still there is same eror – Totoo Boo Dec 25 '11 at 14:11
0

This will give you 1000 random integers between MIN and MAX, including MIN but not including MAX.

int MIN;
int MAX;
int[] randoms = new int[1000];
Random randGen = new Random();

for(int i = 0; i < randoms.length; i++)
{
    randoms[i] = MIN + randGen.nextInt(MAX - MIN));
}
donnyton
  • 5,874
  • 9
  • 42
  • 60