Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The program should be in java and please make sure it's working properly. The code is attached below that needs to be fixed. Create binary

The program should be in java and please make sure it's working properly. The code is attached below that needs to be fixed.

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 ? Enter a number to see if it's in the tree: 25 Your number was not found. The nearest prime number less than your number is 23 The nearest prime number greater than your number is 29

THE CODE IS BELOW....................................

import java.util.Scanner;

import java.util.*;

import java.io.*;

public class BinaryTree

{

public static void main(String[] args)

{

new BinaryTree().run();

}

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);

}

static class Node

{

Node left;

Node right;

int value;

public Node(int value)

{

this.value = value;

}

}

public void run()

{

int count = 0;

Node root = new Node(2);

System.out.println("Building the tree having root value " + root.value);

for (int i = 3; count < 100; i++)

{

if (isPrime(i) == true)

{

count += 1;

insert(root, i);

}

}

System.out.println("Traversing the tree in order");

printTreeInOrder(root);

Scanner s = new Scanner(System.in);

System.out.println("Enter a number to check if it is prime or not: ");

int num = s.nextInt();

if (isPrime(num) == true)

{

System.out.println(num + " is Prime number");

}

else

{

int Lower = num;

while (!isPrime(Lower))

Lower--;

System.out.println("Nearest prime number lesser than " + num + " is " + Lower);

for (int i = num + 1;; i++)

{

if (isPrime(i) == true)

{

System.out.println("Nearest prime number greater than " + num + " is " + i);

break;

}

}

}

}

public void insert(Node node, int value)

{

if (value < node.value)

{

if (node.left != null)

{

insert(node.left, value);

}

else

{

System.out.println("Inserted " + value + " to the left of " + node.value);

node.left = new Node(value);

}

}

else if (value > node.value)

{

if (node.right != null)

{

insert(node.right, value);

}

else

{

System.out.println("Inserted " + value + " to the right of " + node.value);

node.right = new Node(value);

}

}

}

public void printTreeInOrder(Node node)

{

if (node != null)

{

printTreeInOrder(node.left);

System.out.println(node.value);

printTreeInOrder(node.right);

}

}

public boolean isPrime(int n)

{

for (int i = 2; i <= n / 2; i++)

{

if (n % i == 0)

{

return false;

}

}

return true;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Select Healthcare Classification Systems And Databases

Authors: Katherine S. Rowell, Ann Cutrell

1st Edition

0615909760, 978-0615909769

More Books

Students also viewed these Databases questions

Question

1. How will you, as city manager, handle these requests?

Answered: 1 week ago

Question

1. Identify the sources for this conflict.

Answered: 1 week ago

Question

3. How would you address the problems that make up the situation?

Answered: 1 week ago