# Binary Search Tree Recursion Implementation

• June 8th, 2013, 03:46 PM
ambientplanet
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.

Code Java:

```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   //1. read numbers into arraylist try { fileScanner = new Scanner(new File("input.txt")); while(fileScanner.hasNextInt()){ allNumbers.add(fileScanner.nextInt()); }   for (int a : allNumbers) { if ( (isPrime(a))) { allPrimeNumbers.add(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 //Complete the addOntoBST() method     //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) { System.out.println("File not found!"); } 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; }   public void addOntoBST(Node treeHead, Node current){   if ( treeHead == null ) { treeHead = current; // if (treeHead.getValue() != null) // this.treeHead = treeHead; } else { if ( current.getValue() < treeHead.getValue()) { if ( treeHead.getLeft() == null ) treeHead.setLeft(current); else addOntoBST( treeHead.getLeft(), current); } else { if ( treeHead.getRight() == null ) treeHead.setRight(current); else addOntoBST( treeHead.getRight(), current); } }       } 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:

Code Java:

```for (int a : allPrimeNumbers) { Node tempNode = new Node (a, null, null); addOntoBST(BST.getRoot(), brandNode);```

just before the BST class declaration, but it gives me an "illegal start of type" error.

Any ideas? I'm just confused as to how I can use the method addOntoBST method to fill the tree.