[JAVA] I wrotea program to do file compression/decompression using the Huffman algorithm. When I run the program with with a filled text file it works,
[JAVA] I wrotea program to do file compression/decompression using the Huffman algorithm. When I run the program with with a filled text file it works, but when I run it with an empty file I get a NullPointer Exception on the buildtree method. Can someone offer help on how to get around this?
Here is my code
//Huffman tree implementation
class Main{
public static void main(String args[])throws java.io.FileNotFoundException{
//Choose your own file location:
String file = "/Users/vboogie/Downloads/DataStructures/workspace/HuffAlgorithm/src/Input.txt";
HuffTree tree = new HuffTree();
tree.buildTree(file);
String encoded = tree.encodeFile(file);
System.out.println("Encoded String "+encoded);
String decoded = tree.decodeFile(encoded);
System.out.println("Decoded String : "+decoded);
}
}
class Node implements Comparable
char ch;
int freq;
Node left,right;
boolean leaf = false;
String encoded="";
Node(int freq){
this.freq = freq;
this.leaf = false;
}
Node(char ch, int freq){
this.ch = ch;
this.freq = freq;
this.leaf = true;
}
@Override
public int compareTo(Node n){
return this.freq-n.freq;
}
@Override
public String toString(){
return "("+ch+" "+freq+" "+encoded+")";
}
}
import java.util.HashMap; import java.util.Map; import java.util.PriorityQueue; import java.util.Scanner; import java.util.Set; //Implements Huffman Tree class HuffTree{
char chars[]; int freq[]; Node root; Map
HuffTree() { } HuffTree(char chars[], int freq[]){ this.chars = chars; this.freq = freq; root = null; }
void getFrequencies(String file)throws java.io.FileNotFoundException { Scanner f = new Scanner(new java.io.File(file)); Map void buildTree(String file)throws java.io.FileNotFoundException{ getFrequencies(file); //HuffTree h = new HuffTree(chars,freq); PriorityQueue } String decode(String data){ String gen = ""; String ans = ""; Node temp = root; for(int i=0;i return ans; } String encode(String data){ String ans = ""; for(int i=0;i String decodeFile(String fl)throws java.io.FileNotFoundException { Scanner f = new Scanner(fl); String ans = ""; while(f.hasNextLine()){ ans = ans+decode(f.nextLine())+" "; } return ans; } String encodeFile(String file)throws java.io.FileNotFoundException { Scanner f = new Scanner(new java.io.File(file)); String ans = ""; while(f.hasNextLine()){ ans = ans+encode(f.nextLine())+" "; } return ans; } void gen( Node node, String gen){ if(node.leaf){ node.encoded = gen; vector.put(node.ch,gen); return; } gen(node.left,gen+"0"); gen(node.right,gen+"1"); } void print(Node node){ if(node.leaf){ System.out.println(node); return; } print(node.left); print(node.right); } void traverseHuffmanTree() throws Exception{ print(root); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started