jeudi 28 avril 2016

Random integers in a binary search tree

For my homework I'm creating a code that generates one thousand random integers from a set of 10,000 numbers, moves them into a container, and then into a binary search tree. I'm given the following code first:

Random ran = new Random();
Integer x;{  
for(int i = 1; i <= 100; i++){
x = ran.nextInt(10,000) + 1;
}

I have a BSTNode & an Intclass. Given the following code in the BST class, I can use this to search

public boolean search(Integer value) {
    boolean retval = false;
    numofcomps ++;
    if (root == null) {
        retval = false;
    } else {
        retval = searchtree(root, value);
    }
    return retval;
}

public boolean searchtree(BSTNode<IntClass> myroot, int value) {
    numofcomps ++;
    boolean retval = false;
    if (myroot == null) {
        retval = false;
    } else {
        if (value == myroot.element.myInt) {
            retval = true;
        } else {
            if (value < myroot.element.myInt) {
                retval = searchtree(myroot.leftTree, value);
            } else {
                retval = searchtree(myroot.rightTree, value);
            }
        }
    }

I need to figure out how to move the random numbers into the binary tree. Any suggestions?




Aucun commentaire:

Enregistrer un commentaire