## Binary Search Tree Recursion Implementation

Hello everyone,

I'm working on a Binary Search Tree project, and I'm stuck on how to implement a method.

```import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class OrderingPrime {
public static void main(String[] args) {
orderPrime a = new orderPrime();
a.order();
}
}
class orderPrime{
public orderPrime(){}
private Scanner fileScanner;
public void order(){

//1. read numbers into an array list
//2. remove all the non-prime
//3. construct a BST
//4. insert the elements
//5. output.

ArrayList<Integer> allNumbers = new ArrayList<Integer>();
ArrayList<Integer> allPrimeNumbers = new ArrayList<Integer>(); //created ArrayList to store prime numbers

try {
fileScanner = new Scanner(new File("input.txt"));
while(fileScanner.hasNextInt()){
}

for (int a : allNumbers) {
if ( (isPrime(a))) {

}
}

//To be Completed
//2. pick out the primes
//use the isPrime() method

//To be Completed
//3-4. construct a BST using the Node class.
//then insert prime numbers into the tree

//To be completed:
//5. output result:
//Complete the recursivePrint()method to do a IN ORDER TRAVERSAL
//Print the nodes separated by a space

fileScanner.close();
} catch (FileNotFoundException e) {
} finally{
if(fileScanner!= null)fileScanner.close();
}
}
public boolean isPrime(int input) {
boolean isPrime = true;
if(input<2)return false;
for(int i=2;i<=Math.sqrt(input);i++){
if(input%i == 0){
//found a factor!
isPrime = false;
}
}
return isPrime;
}

}
class BST {

private Node root;
public BST() {
root = null;
}

public Node getRoot() {
return root;
}

if ( treeHead == null ) {
}
else {
if ( current.getValue() < treeHead.getValue()) {
if ( treeHead.getLeft() == null )
else
} else {
if ( treeHead.getRight() == null )
else
}
}

}
public void recursivePrint(Node treeNode){
if (treeNode.getLeft() != null)
recursivePrint (treeNode.getLeft());
System.out.print(treeNode.getLeft().toString());
if (treeNode.getLeft() != null)
recursivePrint(treeNode.getRight());
System.out.print(treeNode.getRight().toString());

//To be compelted for step 5
}

}

class Node{
private int value;
private Node[] children;
boolean hasLeft = false;
boolean hasRight = false;
public Node(int a){
children = new Node[2];
value = a;
}
public Node(int a, Node left, Node right){
children = new Node[2];
value = a;
children[0] = left;
children[1] = right;
hasLeft = true;
hasRight = true;
}
public void setLeft(Node left){
children[0] = left;
hasLeft = true;
}
public void setRight(Node right){
children[1] = right;
hasRight = true;
}

public Node getLeft(){
return children[0];
}
public Node getRight(){
return children[1];
}
public int getValue(){
return value;
}
public String toString(){
return ""+value;
}
}
}```

Now, I want to use the method addOntoBST to fill my BST with nodes, but haven't figured out how. I've tried to use the following code:

```for (int a : allPrimeNumbers) {
Node tempNode = new Node (a, null, null);