Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

[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 vector = new HashMap(); Map decodedMap = new HashMap();

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 fm = new HashMap(); while(f.hasNextLine()){ String data = f.nextLine(); for(int i=0;i keys = fm.keySet(); int size = keys.size(); chars = new char[size]; freq = new int[size]; int i=0; for(char x : keys){ chars[i] = x; freq[i] = fm.get(chars[i]); i++; } System.out.println("Frequency counts:"); for(i=0;i

void buildTree(String file)throws java.io.FileNotFoundException{ getFrequencies(file);

//HuffTree h = new HuffTree(chars,freq); PriorityQueue heap = new PriorityQueue(); for(int i=0;i

}

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 33rd International Conference Dexa 2022 Vienna Austria August 22 24 2022 Proceedings Part 1 Lncs 13426

Authors: Christine Strauss ,Alfredo Cuzzocrea ,Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil

1st Edition

3031124227, 978-3031124228

More Books

Students also viewed these Databases questions

Question

=+ f. instituting laws against driving while intoxicated

Answered: 1 week ago