# construct a BST tree using the Node class given - revision

• June 7th, 2013, 04:19 AM
stun31
construct a BST tree using the Node class given - revision
Code :

```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; } }```
• June 7th, 2013, 08:58 AM
copeg
Re: construct a BST tree using the Node class given - revision
Do you have a question?
• June 7th, 2013, 11:22 PM
stun31
Re: construct a BST tree using the Node class given - revision
Hey there Thanks for having a look - I am trying to construct a BST tree using this Node class given

--- Update ---

The program Im writing reads a txt into an arraylist, I have done that - from there I pick out which numbers are prime using my isPrime() method, done that - now I am trying to construct a binary search tree using the Node class given and use my addOntoBST() method to add into the BST.

Code :

```import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; import java.util.*;     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(){   ArrayList<Integer> allNumbers = new ArrayList<Integer>();   try { fileScanner = new Scanner(new File("input.txt")); while(fileScanner.hasNextInt()){ allNumbers.add(fileScanner.nextInt()); } System.out.println("File input.txt" + " " +allNumbers);     for(int i = 0; i < allNumbers.size(); i++){ if(isPrime(allNumbers.get(i))){ System.out.print(allNumbers.get(i)+ " "); } }     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; } public void addOntoBST(Node treeHead, Node current){ if( treeHead == null){   }   }       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; } }```