I'm having trouble constructing a huffmanTree without using frequencies, the user will enter a character and a binary number directly to the huffmanTree. Based on the user input, if the first input of the binary is 0, it should go left, if else it's 1, it should go to the right. When inserting the second binary code the program should check if that place is a leaf or not. If it's a leaf, insert. If not return a message as that place is taken.
I have came up with an idea that is returning a error..
Exception in thread "main" java.lang.NullPointerException at ideas.ideas.insert(ideas.java:70)
import java.util.LinkedList;
import java.util.Scanner;
class Node {
char data;
int idata;
boolean isCreated = false;
Node leftChild, rightChild;
public Node(char data) {
this.data = data;
leftChild = new Node();
rightChild = new Node();
}
public Node() {
}
}
public class ideas{
Node root = new Node();
public ideas(){
root.isCreated = true;
}
void insert(char data, String bin) {
Node newNode = new Node();
newNode.isCreated=true;
if (!root.isCreated) {
System.out.println("It never enters here, since the root is always empty and is created with the constructor ");
root = newNode;
root.isCreated = true;
} else {
System.out.println("");
Node current = root;
Node parent = current;
int i = 0;
while (i < bin.length()) {
parent = current;
if (bin.charAt(i) == '0') {
System.out.println("Parent leftChild is : "+newNode.isCreated);
current = current.leftChild;
// System.out.println("Current left child is : "+current.isCreated);
if (current.isCreated == false) {
parent.leftChild = newNode;
current.isCreated = true;
// current.idata = 0;
//return;
i++;
}
} else {
current = current.rightChild;
if (current.isCreated == false) {
parent.rightChild = newNode;
current.isCreated = true;
//current.idata = 1;
//return;
i++;
}
}
}
current.data = data;
System.out.println("the data is "+current.data);
}
}
void preOrder(Node current) {
if (current.isCreated == false) {
System.out.println(current.data);
preOrder(current.leftChild);
preOrder(current.rightChild);
}
}
public static void main(String[] args) {
System.out.println("Enter a character");
System.out.println("Enter its binary code");
ideas t = new ideas();
t.insert('e', "10101");
// t.insert('a', "00101");
t.preOrder(t.root);
}
}