I am trying to read in numbers from a file then put those in a binary tree. I am trying to read in the file of numbers into an array then use the array to transfer my numbers to my binary tree. I currently have a treeNode class (below) and a tree class (also below)
package com.company;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;
public class treeNode
{
public int data;
public treeNode left, right;
public int frequency;
public treeNode(int data)
{
this.data = data;
this.left = null;
this.right = null;
}
public int getData() {
Scanner in = new Scanner(System.in);
System.out.println("Enter file name: ");
String fileName = in.nextLine();
String line = null;
try
{
FileReader fileReader = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(fileReader);
while ((line = bufferedReader.readLine()) != null)
{
System.out.println(line);
}
}
catch (FileNotFoundException e)
{
System.out.println(e);
}
catch (java.io.IOException e)
{
System.out.println("io");
}
ArrayList<Integer> numFile = new ArrayList<>();
while (in.hasNext())
{
numFile.add(in.nextInt());
}
return data;
}
public treeNode getLeft() {
return left;
}
public treeNode getRight() {
return right;
}
public void setLeft(treeNode left) {
this.left = left;
}
public void setRight(treeNode right) {
this.right = right;
}
Tree class
package com.company;
public class tree
{
public treeNode root;
public tree()
{
root = null;
}
private void buildBinaryTree(int[] list)
{
treeNode temp;
for (int data:list) {
if (root == null) {
root = new treeNode(data);
}
else {
temp = root;
boolean searching = true;
while(searching)
{
if (temp.getData() > data) {
if (temp.getLeft() != null) {
temp = temp.getLeft();
} else {
searching = false;
}
} else {
if (temp.getRight() != null) {
temp = temp.getRight();
} else {
searching = false;
}
}
}
if (temp.getData() > data)
{
temp.setLeft(new treeNode(data));
} else {
temp.setRight(new treeNode(data));
}
}
}
}
}
I can't figure out how to build the binary tree with the numbers in the file. I appreciate any help you can give me.