1

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.

Mrs.Jones
  • 11
  • 3
  • Possible duplicate: http://stackoverflow.com/questions/36806939/array-to-binary-search-trees-quick – santosh-patil Apr 13 '17 at 04:37
  • Binary search tree or plain binary tree? – stinepike Apr 13 '17 at 04:40
  • Is this a question of getting the numbers from the file into an `int` array so you can call `tree.buildBinaryTree()`? Or have you got other issues? One of the first steps: If you want to call the mentioned method from `treenode.getData()`, it cannot be private (or maybe both methods should be in the same class?) – Ole V.V. Apr 13 '17 at 04:42
  • What is the file format? One number on each line, nothing else? – Ole V.V. Apr 13 '17 at 04:42
  • There is one number on each line @OleV.V. – Mrs.Jones Apr 13 '17 at 05:15
  • Yes @OleV.V. I am trying to access the numbers in the file and I thought the best way to do that was to read them into an array. I have to sort the numbers into the binary treee – Mrs.Jones Apr 13 '17 at 05:17
  • @StinePike binary search tree – Mrs.Jones Apr 13 '17 at 05:18
  • You may want to use a `Scanner` for reading the file just as you are using one for `System.in`. Otherwise you may use `Integer.parseInt(line)` for obtaining the number from each line you have read through the `BufferedReader`. If you don’t know in advance how many numbers there are, I suggest putting them in an `ArrayList` first and converting to an array after you have read them all. – Ole V.V. Apr 13 '17 at 05:47
  • For converting from `ArrayList` to `int[]`, you may for example see [this answer: How to convert an ArrayList containing Integers to primitive int array?](http://stackoverflow.com/questions/718554/how-to-convert-an-arraylist-containing-integers-to-primitive-int-array) – Ole V.V. Apr 13 '17 at 05:51

1 Answers1

0

I'd suggest to develop each component in isolation. You can view it as a collection of entirely separate projects, that will build up to implementing the functionality you're looking for. I suggest these steps:

Start by building your generic Node class and a Tree classes without worrying about file I/O.

As part of that, implement a clean void add(final int data) in the Tree class, using data you provide statically from a main(). You test calls will be like:

tree.add(4);
tree.add(2);
... etc.

Next, expand Tree to include a addList():

public void addList(final List<Integer> list) {
    for (final int value : list) {
        this.add(value);
    }
}

Or if you know about / use / care about Java 8, you could skip the loop and do:

list.forEach(value -> this.add(value));

Lastly, make a separate class, FileReader or FileUtils, which implements utility method:

public static List<Integer> getValuesFromFile(final String path) {
    // open file, read values, put into list, return it
}

Now, in your code which builds the tree, you just piece these components together:

Tree tree = new Tree();
tree.addList(FileUtils.getValuesFromFile("koolFile.txt"));

tree.has(5) // etc.. other tree calls here

Oh right. I should also note that the order of the items in the file is enough to dictate how it is constructed. You actually only need the node values, not the linking information. This would simplify the file logic significantly.

Jameson
  • 6,400
  • 6
  • 32
  • 53