Question: The program should be in java Create binary search tree (BST) that contains the first million prime numbers. After you are reading in the data,

The program should be in java

Create binary search tree (BST) that contains the first million prime numbers. After you are reading in the data, output the maximum and average depth of the BST. Allow the user to enter a number to see whether or not it is prime. If the number is found, output the depth at which it was found. If the number isn't found, output the nearest prime number greater than and the nearest prime number less than the user's number.

Example output Creating Binary Tree from 1000000 prime numbers... The maximum depth of the tree is ? The average depth of the tree is ?

import java.util.*;

import java.io.*;

public class tree

{

public static void main (String [] args) throws IOException

{

int userChoice;

Scanner input = new Scanner(System.in);

/*********************************************************/

File file = new File("primes4.txt");

Scanner infile = new Scanner(file);

int Count = 1;

Int MaxDepth = 0;

Temp = new Node();

Root.Data = infile.nextInt();

while (infile.hasNextInt())

{

Count++;

Depth = 0;

insert(Root, infile.nextInt());

if(Depth > MaxDepth)

MaxDepth = Depth;

}

System.out.println("Finished reading " + Count + " Prime numbers ");

System.out.println("Maximum Depth = " + MaxDepth);

} // end main

static void Insert(Node TheNode, int TheData)

{

if (TheNode.Data > NewData && TheNode.Right == null)

{

Node T = new Node();

T.Data = NewData;

TheNode.Left = T;

}

else if (TheNode.Data < NewData && TheNode.Right == null)

{

Node T = new Node();

T.Data = NewData;

TheNode.Right = T;

}

else if (TheNode.Data > TheData && TheNode.Left == null)

Insert(TheNode.Left, NewData);

else if (TheNode.Data < TheData && TheNode.Right == null)

Insert(TheNode.Right, NewData);

}

}

class Node

{

int Data;

Node Next;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!