Step 1 generate 50 random numbers ranging from 1 to 99 and store it in an array
ID: 3813356 • Letter: S
Question
Step 1 generate 50 random numbers ranging from 1 to 99 and store it in an array (DO NOT SORT THE ARRAY). Print these values to the screen. Step 2: Now take one number at a time seguentially starting from the leftmost value (the value at index position 0 of the array) and insert it into a Binary Search Tree. You can use a second array to store the Binary Search Tree they must use the formula we discussed in class to store Left Child and Right Child Step 4: Upon building the Binary Search Tree, they must follow the rule that LeftChild Parent RightChild. Step 5: Print out the values of this second array. Note: you must compare the original distribution of the values in the array vs the distribution after creating the Binary Search Tree (the second array/ArrayList) by printing this observation7comparison to the screen.Explanation / Answer
package chegg;
import java.util.Random;
//BinarySearchTree class
public class BinarySearchTree
{
public static Node root;
//constructor calling
public BinarySearchTree()
{
this.root = null;
}
/*a Binary Search Tree (where each key is greater than the keys in its left subtree and
* smaller than the keys in its right subtree).
*/
public void Binary_Search_Tree(int id)
{
Node newNode = new Node(id);
//if root equal to null then root will be newnode
if(root==null){
root = newNode;
return;
}
Node current = root;
Node parent = null;
while(true)
{
parent = current;
if(id<current.data){
current = current.left;
if(current==null){
parent.left = newNode;
return;
}
}else{
current = current.right;
if(current==null){
parent.right = newNode;
return;
}
}
}
}
//dispaly function
public void display(Node root)
{
if(root!=null){
display(root.left);
System.out.print(root.data+" ");
display(root.right);
}
}
static int random_num()
{
Random rand = new Random();
int rnd=rand.nextInt((98) + 1)+1;
return rnd;
}
//main function
public static void main(String arg[])
{
BinarySearchTree b = new BinarySearchTree();
int array[]=new int[50];
int i;
for (i = 0; i <50; i++)
{
array[i]=random_num();
}
System.out.println("--------Array IS -------");
for (i = 0; i <50; i++)
{
System.out.print(array[i]+" ");
}
System.out.println();
System.out.println("Array of BST IS : ");
for (i = 0; i < 50; i++)
{
b.Binary_Search_Tree(array[i]);
}
// calling display function
b.display(b.root);
}
}
----------------------
Output sample 1:-
--------Array IS -------
62 9 62 69 9 14 47 17 44 62 44 6 85 50 91 92 85 97 13 28 29 18 39 13 6 69 15 81 57 48 5 37 83 12 98 31 51 52 63 32 28 76 66 25 70 78 89 46 1 33
Array of BST IS :
1 5 6 6 9 9 12 13 13 14 15 17 18 25 28 28 29 31 32 33 37 39 44 44 46 47 48 50 51 52 57 62 62 62 63 66 69 69 70 76 78 81 83 85 85 89 91 92 97 98
----------------------
Output sample 2:-
--------Array IS -------
52 2 15 64 36 46 45 62 58 48 74 99 15 87 74 44 72 15 15 64 31 68 96 89 59 85 96 21 29 84 83 92 20 72 87 30 1 74 46 41 10 56 35 33 88 13 99 66 10 78
Array of BST IS :
1 2 10 10 13 15 15 15 15 20 21 29 30 31 33 35 36 41 44 45 46 46 48 52 56 58 59 62 64 64 66 68 72 72 74 74 74 78 83 84 85 87 87 88 89 92 96 96 99 99
--------------------------------
Output sample 3:-
--------Array IS -------
57 59 87 60 60 98 99 65 97 28 16 13 30 30 27 63 41 40 49 21 8 31 50 47 43 46 60 61 57 67 39 46 67 26 32 83 50 98 39 78 29 63 28 33 13 52 97 70 85 27
Array of BST IS :
8 13 13 16 21 26 27 27 28 28 29 30 30 31 32 33 39 39 40 41 43 46 46 47 49 50 50 52 57 57 59 60 60 60 61 63 63 65 67 67 70 78 83 85 87 97 97 98 98 99
---------------------------------------------------------------------------------------------
If you have any query, please feel free to ask.
Thanks a lot.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.