I am trying to make a generic binary tree that is created from a text file. I am looking for the best way and correctly do so and already tried. In my first method is BinaryTree(Scanner) is what creates the tree from a Scanner tied to the file. Then leftInsert() inserts left and rightInsert() inserts right and I hope those are correct too. But I am not sure how to make the text file correctly in order for the tree to be made as well.
Text File:
A
B
C
D
E
F
Generic Binary Tree Class:
import java.util.*;
import java.io.*;
import org.w3c.dom.Node;
private class Nodes<E> {
public E value;
public Nodes<E> leftChild;
public Nodes<E> rightChild;
public Nodes(E value) {
leftChild = null;
rightChild = null;
this.value = value;
}
}
public class BinaryTree<E> {
private Node root;
// biggest issue here
public BinaryTree(Scanner){
// reads a file to create a tree
}
public void leftInsert(Nodes<E> Node, E value) {
if ((value).compareTo(Node.value) < 0) {
if (Node.leftChild == null) {
Node.leftChild = new Nodes<E>(value);
} else {
leftInsert(Node.leftChild, value);
}
}
}
public void rightInsert(Nodes<E> Node, E value) {
if ((value).compareTo(Node.value) >= 0) {
if (Node.rightChild == null) {
Node.rightChild = new Nodes<E>(value);
} else {
rightInsert(Node.rightChild, value);
}
}
}
}