Hi,

I have implemented an array a binary search tree and an AVL tree. Ive made a random number generator to insert 20 randomly generated numbers into the array, now what I would like to do and im finding it difficult is that the same numbers generated and insrted into the array be inserted into the binary search tree and the AVL tree. in other word im trying to make a random number generator common to the array, binary search tree and the AVL tree.

can you help me please ?

thanks

ron

## 5 Replies - 4606 Views - Last Post: 22 December 2010 - 03:39 PM

##
**Replies To:** random number generator

### #2

## Re: random number generator

Posted 22 December 2010 - 05:00 AM

Really, you implemented an AVL tree and you can't do this?

If the problem is trying to add an int or double when you need an object you can create wrapper Integer and Double objects to add to your data structures.

for(Number n : array) { binarySearchTree.add(n); avlTree.add(n); }

If the problem is trying to add an int or double when you need an object you can create wrapper Integer and Double objects to add to your data structures.

This post has been edited by **cfoley**: 22 December 2010 - 04:58 AM

### #3

## Re: random number generator

Posted 22 December 2010 - 05:49 AM

Random generator = new Random(); // Create a random generator. int l = generator.nextInt(20); AvlNode root = AvlTree.CreateAvlNode(l); // Creating the Avl tree root for (int i = 0; i < arraycapacity; i++) // Loop through all the array { Array[i] = l; // Insert a random number between 0-20. theTree.insert(l, 0); } for (int i = 1; i < arraycapacity; i++) { AvlTree.AvlInsert(root, AvlTree.CreateAvlNode(l)); }

to insert into the Array the BST and the AVL I made the above code but the numbers in the AVL and the BST are not the same as the numbers in the array. I made another separate for loop to insert into the avl because im inserting the root separetly so I need to decrease one from the 20 numbers.

what im doing wrong ?

thanks

### #4

## Re: random number generator

Posted 22 December 2010 - 03:07 PM

You are inserting arrayCapacity times the same number

you need

Array[i] = l; // Insert a random number between 0-20. theTree.insert(l, 0);

you need

l = generator.nextInt(20); Array[i] = l; // Insert a random number between 0-20. theTree.insert(l, 0);

### #5

## Re: random number generator

Posted 22 December 2010 - 03:37 PM

pbl, on 22 December 2010 - 09:07 PM, said:

You are inserting arrayCapacity times the same number

you need

Array[i] = l; // Insert a random number between 0-20. theTree.insert(l, 0);

you need

l = generator.nextInt(20); Array[i] = l; // Insert a random number between 0-20. theTree.insert(l, 0);

But he has write

l = generator.nextInt(20);

### #6

## Re: random number generator

Posted 22 December 2010 - 03:39 PM

Page 1 of 1