I am working on making a Binary Search Tree in Java, and I am running into a NullPointerException when trying to add nodes to the tree. Attached are the screenshots of the code that I have currently.
import java.util.Scanner;
import java.io.FileWriter;
import java.io.IOException;
/**
* Write a description of class BinaryTree here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class BinaryTree<T extends Comparable<T>> {
public TreeNode<T> root = null;
public void add(T value) {
if(root == null) {
root = new TreeNode<T>(value, 0);
} else {
add(root, value, 0);
}
}
private void add(TreeNode<T> node, T entry, int rank) {
if(entry.compareTo(node.value) < 0) {
if(node.left == null) {
node.left = new TreeNode<T>(entry, rank + 1);
} else {
add(node.left, entry, rank + 1);
}
}
if(node.right == null) {
node.right = new TreeNode<T>(entry, rank + 1);
} else {
add(node.right, entry, rank + 1);
}
}
public void printInOrder() {
printInOrder(root);
System.out.println();
}
private void printInOrder(TreeNode<T> node) {
if(node.left != null) {
printInOrder(node.left);
}
System.out.println(node.value);
if(node.right != null) {
printInOrder(node.right);
}
}
public void printPreOrder() {
printPreOrder(root);
System.out.println();
}
private void printPreOrder(TreeNode<T> node) {
if(node != null) {
System.out.println(node.value);
printPreOrder(node.left);
}
if(node.right != null) {
printPreOrder(node.right);
}
}
public void printPostOrder() {
printPostOrder(root);
System.out.println();
}
private void printPostOrder(TreeNode<T> node) {
if(node.left != null) {
printPostOrder(node.left);
}
if(node.right != null) {
printPostOrder(node.right);
}
System.out.println(node.value);
}
public void printTreeGraph() {
Queue<TreeNode<T>> queue = new Queue<>();
queue.push(root);
try {
FileWriter treeFile = new FileWriter("rankOrderTreeGraph.txt");
treeFile.write("digraph Tree {");
while(! queue.isEmpty()) {
TreeNode<T> node = queue.pop();
if(node.parent != null) {
treeFile.write("\t" + node.parent.value + "->" + node.value + ";");
System.out.println(node.value + ";");
}
if(node.left != null) {
queue.push(node.left);
}
if(node.right != null) {
queue.push(node.right);
}
}
treeFile.write("}");
treeFile.close();
} catch (IOException e) {
System.out.println("An error occurred");
}
}
public static void main(String[] args) {
BinaryTree<String> tree = new BinaryTree<>();
System.out.println("Enter name, enter 'quit' when finished");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
while(! name.equals("quit")) {
tree.add(name);
name = scanner.nextLine();
}
System.out.printf("Choose how to print tree: %n" +
"1: Inorder Traversal %n" + "2: Preorder Traversal %n" +
"3: Postorder Traversal %n" + "4: As a GraphViz Dot File %n" +
"q: Quit %n");
String choice = scanner.nextLine();
while(! choice.equals("q")) {
if(choice.equals("1")) {
tree.printInOrder();
System.out.println();
}
if(choice.equals("2")) {
tree.printPreOrder();
System.out.println();
}
if(choice.equals("3")) {
tree.printPostOrder();
System.out.println();
}
if(choice.equals("4")) {
tree.printTreeGraph();
} else {
System.out.println("Error");
}
System.out.println(">");
choice = scanner.nextLine();
}
}
}
/**
* Write a description of class TreeNode here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class TreeNode<T extends Comparable<T>> {
public T value;
public TreeNode<T> left;
public TreeNode<T> right;
public int rank;
public TreeNode<T> parent;
public TreeNode(T value, int rank) {
value = this.value;
left = null;
right = null;
this.rank = rank;
}
}
I when I try to add nodes to the list I get a NullPointerException that points to if(entry.compareTo(node.value) < 0) in the private method add.