SlideShare a Scribd company logo
1 of 11
Download to read offline
public final category Huffman non-public Huffman() ;
personal static category HuffmanNode
}
personal static category HuffManComparator implements Comparator<HuffmanNode> come
node1.frequency - node2.frequency;
}
}
/**
* Compresses the string victimization huffman formula.
* The huffman tree and also the huffman code area unit serialized to disk
*
* @param sentence The sentence to be serialized
* @throws FileNotFoundException If file isn't found
* @throws IOException If IO exception happens.
*/
public static void compress(String sentence) throws FileNotFoundException, IOException ought
to atleast have one character.");
}
final Map<Character, Integer> charFreq = getCharFrequency(sentence);
final HuffmanNode root = buildTree(charFreq);
final Map<Character, String> charCode = generateCodes(charFreq.keySet(), root);
final String encodedMessage = encodeMessage(charCode, sentence);
serializeTree(root);
serializeMessage(encodedMessage);
}
personal static Map<Character, Integer> getCharFrequency(String sentence) else
}
come map;
}
/**
* Map<Character, Integer> map
* Some implementation of that treeSet is passed as parameter.
* @param map
*/
personal static HuffmanNode buildTree(Map<Character, Integer> map) whereas
(nodeQueue.size() > 1)
// take away it to forestall object leak.
come nodeQueue.remove();
}
personal static Queue<HuffmanNode> createNodeQueue(Map<Character, Integer> map)
return pq;
}
personal static Map<Character, String> generateCodes(Set<Character> chars, HuffmanNode
node) {
final Map<Character, String> map = new HashMap<Character, String>();
doGenerateCode(node, map, "");
come map;
}
personal static void doGenerateCode(HuffmanNode node, Map<Character, String> map, String
s)
doGenerateCode(node.left, map, s + '0');
doGenerateCode(node.right, map, s + '1' );
}
personal static String encodeMessage(Map<Character, String> charCode, String sentence)
come stringBuilder.toString();
}
personal static void serializeTree(HuffmanNode node) throws FileNotFoundException,
IOException attempt (ObjectOutputStream oosTree = new ObjectOutputStream(new
FileOutputStream("/Users/ap/Desktop/tree"))) attempt (ObjectOutputStream oosChar = new
ObjectOutputStream(new FileOutputStream("/Users/ap/Desktop/char"))) cushioned to mark
finish of bit set relevant for deserialization.
oosTree.writeObject(bitSet);
}
}
}
personal static category IntObject
/*
* Algo:
* 1. Access the node
* 2. Register the worth in bit set.
*
*
* here true and false dont correspond to left branch and right branch.
* there,
* - true means that "a branch originates from leaf"
* - false mens "a branch originates from non-left".
*
* conjointly since branches originate from some node, the foundation node should be provided
as supply
* or start line of initial branches.
*
* Diagram and the way associate degree bit set would look as a result.
* (source node)
* / 
* true true
* / 
* (leaf node) (leaf node)
* | |
* false false
* | |
*
* thus currently a little set sounds like [false, true, false, true]
*
*/
personal static void preOrder(HuffmanNode node, ObjectOutputStream oosChar, BitSet bitSet,
IntObject intObject) throws IOException
bitSet.set(intObject.bitPosition++, true); // register branch in bitset
preOrder(node.left, oosChar, bitSet, intObject); // take the branch.
bitSet.set(intObject.bitPosition++, true); // register branch in bitset
preOrder(node.right, oosChar, bitSet, intObject); // take the branch.
}
personal static void serializeMessage(String message) throws IOException attempt
(ObjectOutputStream oos = new ObjectOutputStream(new
FileOutputStream("/Users/ap/Desktop/encodedMessage")))
}
personal static BitSet getBitSet(String message) else
}
bitSet.set(i, true); // dummy bit set to grasp the length
come bitSet;
}
/**
* Retrieves back the initial string.
*
*
* @return the initial uncompressed string
* @throws FileNotFoundException If the file isn't found
* @throws ClassNotFoundException If category isn't found
* @throws IOException If IOException happens
*/
public static String expand() throws FileNotFoundException, ClassNotFoundException,
IOException {
final HuffmanNode root = deserializeTree();
come decodeMessage(root);
}
personal static HuffmanNode deserializeTree() throws FileNotFoundException, IOException,
ClassNotFoundException attempt (ObjectInputStream oisBranch = new ObjectInputStream(new
FileInputStream("/Users/ap/Desktop/tree"))) attempt (ObjectInputStream oisChar = new
ObjectInputStream(new FileInputStream("/Users/ap/Desktop/char"))) federal
agencyBranch.readObject();
come preOrder(bitSet, oisChar, new IntObject());
}
}
}
/*
* Construct a tree from:
* input [false, true, false, true, (dummy faithful mark the top of bit set)]
* The input is made from preorder traversal
*
* Algo:
* one produce the node.
* 2. browse what's registered in bitset, and choose if created node is meant to be a leaf or non-
leaf
*
*/
personal static HuffmanNode preOrder(BitSet bitSet, ObjectInputStream oisChar, IntObject o)
throws IOException deciding if created node is that the leaf or non-leaf.
if (!bitSet.get(o.bitPosition)) succeeding position to future stack frame by doing computation
before preOrder is termed.
node.ch = oisChar.readChar();
come node;
}
o.bitPosition = o.bitPosition + 1; // feed future position to future stack frame by doing
computation before preOrder is termed.
node.left = preOrder(bitSet, oisChar, o);
o.bitPosition = o.bitPosition + 1; // feed future position to future stack frame by doing
computation before preOrder is termed.
node.right = preOrder(bitSet, oisChar, o);
come node;
}
personal static String decodeMessage(HuffmanNode node) throws FileNotFoundException,
IOException, ClassNotFoundException attempt (ObjectInputStream OIS = new
ObjectInputStream(new FileInputStream("/Users/ameya.patil/Desktop/encodedMessage")))
worker = node;
// since huffman code generates full binary tree, temp.right is actually null if worker.left is null.
whereas (temp.left != null) {
if (!bitSet.get(i)) {
worker = worker.left;
} else {
worker = worker.right;
}
i = i + 1;
}
stringBuilder.append(temp.ch);
}
come stringBuilder.toString();
}
}
public static void main(String[] args) throws FileNotFoundException, IOException,
ClassNotFoundException variety of characters
Huffman.compress("some");
Assert.assertEquals("some", Huffman.expand());
// odd range of characters
Huffman.compress("someday");
Assert.assertEquals("someday", Huffman.expand());
// repetition even range of characters + house + non-ascii
Huffman.compress("some some#");
Assert.assertEquals("some some#", Huffman.expand());
// odd range of characters + house + non-ascii
Huffman.compress("someday someday&");
Assert.assertEquals("someday someday&", Huffman.expand());
}
}
Solution
public final category Huffman non-public Huffman() ;
personal static category HuffmanNode
}
personal static category HuffManComparator implements Comparator<HuffmanNode> come
node1.frequency - node2.frequency;
}
}
/**
* Compresses the string victimization huffman formula.
* The huffman tree and also the huffman code area unit serialized to disk
*
* @param sentence The sentence to be serialized
* @throws FileNotFoundException If file isn't found
* @throws IOException If IO exception happens.
*/
public static void compress(String sentence) throws FileNotFoundException, IOException ought
to atleast have one character.");
}
final Map<Character, Integer> charFreq = getCharFrequency(sentence);
final HuffmanNode root = buildTree(charFreq);
final Map<Character, String> charCode = generateCodes(charFreq.keySet(), root);
final String encodedMessage = encodeMessage(charCode, sentence);
serializeTree(root);
serializeMessage(encodedMessage);
}
personal static Map<Character, Integer> getCharFrequency(String sentence) else
}
come map;
}
/**
* Map<Character, Integer> map
* Some implementation of that treeSet is passed as parameter.
* @param map
*/
personal static HuffmanNode buildTree(Map<Character, Integer> map) whereas
(nodeQueue.size() > 1)
// take away it to forestall object leak.
come nodeQueue.remove();
}
personal static Queue<HuffmanNode> createNodeQueue(Map<Character, Integer> map)
return pq;
}
personal static Map<Character, String> generateCodes(Set<Character> chars, HuffmanNode
node) {
final Map<Character, String> map = new HashMap<Character, String>();
doGenerateCode(node, map, "");
come map;
}
personal static void doGenerateCode(HuffmanNode node, Map<Character, String> map, String
s)
doGenerateCode(node.left, map, s + '0');
doGenerateCode(node.right, map, s + '1' );
}
personal static String encodeMessage(Map<Character, String> charCode, String sentence)
come stringBuilder.toString();
}
personal static void serializeTree(HuffmanNode node) throws FileNotFoundException,
IOException attempt (ObjectOutputStream oosTree = new ObjectOutputStream(new
FileOutputStream("/Users/ap/Desktop/tree"))) attempt (ObjectOutputStream oosChar = new
ObjectOutputStream(new FileOutputStream("/Users/ap/Desktop/char"))) cushioned to mark
finish of bit set relevant for deserialization.
oosTree.writeObject(bitSet);
}
}
}
personal static category IntObject
/*
* Algo:
* 1. Access the node
* 2. Register the worth in bit set.
*
*
* here true and false dont correspond to left branch and right branch.
* there,
* - true means that "a branch originates from leaf"
* - false mens "a branch originates from non-left".
*
* conjointly since branches originate from some node, the foundation node should be provided
as supply
* or start line of initial branches.
*
* Diagram and the way associate degree bit set would look as a result.
* (source node)
* / 
* true true
* /
* (leaf node) (leaf node)
* | |
* false false
* | |
*
* thus currently a little set sounds like [false, true, false, true]
*
*/
personal static void preOrder(HuffmanNode node, ObjectOutputStream oosChar, BitSet bitSet,
IntObject intObject) throws IOException
bitSet.set(intObject.bitPosition++, true); // register branch in bitset
preOrder(node.left, oosChar, bitSet, intObject); // take the branch.
bitSet.set(intObject.bitPosition++, true); // register branch in bitset
preOrder(node.right, oosChar, bitSet, intObject); // take the branch.
}
personal static void serializeMessage(String message) throws IOException attempt
(ObjectOutputStream oos = new ObjectOutputStream(new
FileOutputStream("/Users/ap/Desktop/encodedMessage")))
}
personal static BitSet getBitSet(String message) else
}
bitSet.set(i, true); // dummy bit set to grasp the length
come bitSet;
}
/**
* Retrieves back the initial string.
*
*
* @return the initial uncompressed string
* @throws FileNotFoundException If the file isn't found
* @throws ClassNotFoundException If category isn't found
* @throws IOException If IOException happens
*/
public static String expand() throws FileNotFoundException, ClassNotFoundException,
IOException {
final HuffmanNode root = deserializeTree();
come decodeMessage(root);
}
personal static HuffmanNode deserializeTree() throws FileNotFoundException, IOException,
ClassNotFoundException attempt (ObjectInputStream oisBranch = new ObjectInputStream(new
FileInputStream("/Users/ap/Desktop/tree"))) attempt (ObjectInputStream oisChar = new
ObjectInputStream(new FileInputStream("/Users/ap/Desktop/char"))) federal
agencyBranch.readObject();
come preOrder(bitSet, oisChar, new IntObject());
}
}
}
/*
* Construct a tree from:
* input [false, true, false, true, (dummy faithful mark the top of bit set)]
* The input is made from preorder traversal
*
* Algo:
* one produce the node.
* 2. browse what's registered in bitset, and choose if created node is meant to be a leaf or non-
leaf
*
*/
personal static HuffmanNode preOrder(BitSet bitSet, ObjectInputStream oisChar, IntObject o)
throws IOException deciding if created node is that the leaf or non-leaf.
if (!bitSet.get(o.bitPosition)) succeeding position to future stack frame by doing computation
before preOrder is termed.
node.ch = oisChar.readChar();
come node;
}
o.bitPosition = o.bitPosition + 1; // feed future position to future stack frame by doing
computation before preOrder is termed.
node.left = preOrder(bitSet, oisChar, o);
o.bitPosition = o.bitPosition + 1; // feed future position to future stack frame by doing
computation before preOrder is termed.
node.right = preOrder(bitSet, oisChar, o);
come node;
}
personal static String decodeMessage(HuffmanNode node) throws FileNotFoundException,
IOException, ClassNotFoundException attempt (ObjectInputStream OIS = new
ObjectInputStream(new FileInputStream("/Users/ameya.patil/Desktop/encodedMessage")))
worker = node;
// since huffman code generates full binary tree, temp.right is actually null if worker.left is null.
whereas (temp.left != null) {
if (!bitSet.get(i)) {
worker = worker.left;
} else {
worker = worker.right;
}
i = i + 1;
}
stringBuilder.append(temp.ch);
}
come stringBuilder.toString();
}
}
public static void main(String[] args) throws FileNotFoundException, IOException,
ClassNotFoundException variety of characters
Huffman.compress("some");
Assert.assertEquals("some", Huffman.expand());
// odd range of characters
Huffman.compress("someday");
Assert.assertEquals("someday", Huffman.expand());
// repetition even range of characters + house + non-ascii
Huffman.compress("some some#");
Assert.assertEquals("some some#", Huffman.expand());
// odd range of characters + house + non-ascii
Huffman.compress("someday someday&");
Assert.assertEquals("someday someday&", Huffman.expand());
}
}

More Related Content

Similar to public final category Huffman non-public Huffman() ;personal stati.pdf

mainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdfmainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdf
fathimafancyjeweller
 
The Morse code (see Table 6.10 in book) is a common code that is use.pdf
The Morse code (see Table 6.10 in book) is a common code that is use.pdfThe Morse code (see Table 6.10 in book) is a common code that is use.pdf
The Morse code (see Table 6.10 in book) is a common code that is use.pdf
bhim1213
 
Frequency .java Word frequency counter package frequ.pdf
Frequency .java  Word frequency counter  package frequ.pdfFrequency .java  Word frequency counter  package frequ.pdf
Frequency .java Word frequency counter package frequ.pdf
arshiartpalace
 
AnswerNote LinkedList.cpp is written and driver program main.cpp.pdf
AnswerNote LinkedList.cpp is written and driver program main.cpp.pdfAnswerNote LinkedList.cpp is written and driver program main.cpp.pdf
AnswerNote LinkedList.cpp is written and driver program main.cpp.pdf
anwarsadath111
 
I need to adjust this Huffman code so that it asks for the user to i.pdf
I need to adjust this Huffman code so that it asks for the user to i.pdfI need to adjust this Huffman code so that it asks for the user to i.pdf
I need to adjust this Huffman code so that it asks for the user to i.pdf
jibinsh
 
Create an implementation of a binary tree using the recursive appr.pdf
Create an implementation of a binary tree using the recursive appr.pdfCreate an implementation of a binary tree using the recursive appr.pdf
Create an implementation of a binary tree using the recursive appr.pdf
federaleyecare
 
ANSimport java.util.Scanner; class Bina_node { Bina_node .pdf
ANSimport java.util.Scanner; class Bina_node { Bina_node .pdfANSimport java.util.Scanner; class Bina_node { Bina_node .pdf
ANSimport java.util.Scanner; class Bina_node { Bina_node .pdf
anukoolelectronics
 
I need hlep I have posted this proble 4 times have gotten no help can.pdf
I need hlep I have posted this proble 4 times have gotten no help can.pdfI need hlep I have posted this proble 4 times have gotten no help can.pdf
I need hlep I have posted this proble 4 times have gotten no help can.pdf
shreeaadithyaacellso
 
java question Fill the add statement areaProject is to wo.pdf
java question Fill the add statement areaProject is to wo.pdfjava question Fill the add statement areaProject is to wo.pdf
java question Fill the add statement areaProject is to wo.pdf
dbrienmhompsonkath75
 
I have the following code and I need to know why I am receiving the .pdf
I have the following code and I need to know why I am receiving the .pdfI have the following code and I need to know why I am receiving the .pdf
I have the following code and I need to know why I am receiving the .pdf
ezzi552
 
ikh331-06-distributed-programming
ikh331-06-distributed-programmingikh331-06-distributed-programming
ikh331-06-distributed-programming
Anung Ariwibowo
 
Please help write BinaryTree-java Thank you! Create a class BinaryTr.pdf
Please help write BinaryTree-java Thank you!   Create a class BinaryTr.pdfPlease help write BinaryTree-java Thank you!   Create a class BinaryTr.pdf
Please help write BinaryTree-java Thank you! Create a class BinaryTr.pdf
info750646
 
JavaSE7 Launch Event: Java7xGroovy
JavaSE7 Launch Event: Java7xGroovyJavaSE7 Launch Event: Java7xGroovy
JavaSE7 Launch Event: Java7xGroovy
Yasuharu Nakano
 
Getting the following errorsError 1 error C2436 {ctor} mem.pdf
Getting the following errorsError 1 error C2436 {ctor}  mem.pdfGetting the following errorsError 1 error C2436 {ctor}  mem.pdf
Getting the following errorsError 1 error C2436 {ctor} mem.pdf
herminaherman
 
#ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
 #ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf #ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
#ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
angelsfashion1
 
OrderTest.javapublic class OrderTest {       Get an arra.pdf
OrderTest.javapublic class OrderTest {         Get an arra.pdfOrderTest.javapublic class OrderTest {         Get an arra.pdf
OrderTest.javapublic class OrderTest {       Get an arra.pdf
akkhan101
 

Similar to public final category Huffman non-public Huffman() ;personal stati.pdf (20)

Node js mongodriver
Node js mongodriverNode js mongodriver
Node js mongodriver
 
mainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdfmainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdf
 
The Morse code (see Table 6.10 in book) is a common code that is use.pdf
The Morse code (see Table 6.10 in book) is a common code that is use.pdfThe Morse code (see Table 6.10 in book) is a common code that is use.pdf
The Morse code (see Table 6.10 in book) is a common code that is use.pdf
 
Frequency .java Word frequency counter package frequ.pdf
Frequency .java  Word frequency counter  package frequ.pdfFrequency .java  Word frequency counter  package frequ.pdf
Frequency .java Word frequency counter package frequ.pdf
 
5. Ввод-вывод, доступ к файловой системе
5. Ввод-вывод, доступ к файловой системе5. Ввод-вывод, доступ к файловой системе
5. Ввод-вывод, доступ к файловой системе
 
AnswerNote LinkedList.cpp is written and driver program main.cpp.pdf
AnswerNote LinkedList.cpp is written and driver program main.cpp.pdfAnswerNote LinkedList.cpp is written and driver program main.cpp.pdf
AnswerNote LinkedList.cpp is written and driver program main.cpp.pdf
 
I need to adjust this Huffman code so that it asks for the user to i.pdf
I need to adjust this Huffman code so that it asks for the user to i.pdfI need to adjust this Huffman code so that it asks for the user to i.pdf
I need to adjust this Huffman code so that it asks for the user to i.pdf
 
Flashback, el primer malware masivo de sistemas Mac
Flashback, el primer malware masivo de sistemas MacFlashback, el primer malware masivo de sistemas Mac
Flashback, el primer malware masivo de sistemas Mac
 
Create an implementation of a binary tree using the recursive appr.pdf
Create an implementation of a binary tree using the recursive appr.pdfCreate an implementation of a binary tree using the recursive appr.pdf
Create an implementation of a binary tree using the recursive appr.pdf
 
ANSimport java.util.Scanner; class Bina_node { Bina_node .pdf
ANSimport java.util.Scanner; class Bina_node { Bina_node .pdfANSimport java.util.Scanner; class Bina_node { Bina_node .pdf
ANSimport java.util.Scanner; class Bina_node { Bina_node .pdf
 
I need hlep I have posted this proble 4 times have gotten no help can.pdf
I need hlep I have posted this proble 4 times have gotten no help can.pdfI need hlep I have posted this proble 4 times have gotten no help can.pdf
I need hlep I have posted this proble 4 times have gotten no help can.pdf
 
java question Fill the add statement areaProject is to wo.pdf
java question Fill the add statement areaProject is to wo.pdfjava question Fill the add statement areaProject is to wo.pdf
java question Fill the add statement areaProject is to wo.pdf
 
I have the following code and I need to know why I am receiving the .pdf
I have the following code and I need to know why I am receiving the .pdfI have the following code and I need to know why I am receiving the .pdf
I have the following code and I need to know why I am receiving the .pdf
 
ikh331-06-distributed-programming
ikh331-06-distributed-programmingikh331-06-distributed-programming
ikh331-06-distributed-programming
 
JNI - Java & C in the same project
JNI - Java & C in the same projectJNI - Java & C in the same project
JNI - Java & C in the same project
 
Please help write BinaryTree-java Thank you! Create a class BinaryTr.pdf
Please help write BinaryTree-java Thank you!   Create a class BinaryTr.pdfPlease help write BinaryTree-java Thank you!   Create a class BinaryTr.pdf
Please help write BinaryTree-java Thank you! Create a class BinaryTr.pdf
 
JavaSE7 Launch Event: Java7xGroovy
JavaSE7 Launch Event: Java7xGroovyJavaSE7 Launch Event: Java7xGroovy
JavaSE7 Launch Event: Java7xGroovy
 
Getting the following errorsError 1 error C2436 {ctor} mem.pdf
Getting the following errorsError 1 error C2436 {ctor}  mem.pdfGetting the following errorsError 1 error C2436 {ctor}  mem.pdf
Getting the following errorsError 1 error C2436 {ctor} mem.pdf
 
#ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
 #ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf #ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
#ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
 
OrderTest.javapublic class OrderTest {       Get an arra.pdf
OrderTest.javapublic class OrderTest {         Get an arra.pdfOrderTest.javapublic class OrderTest {         Get an arra.pdf
OrderTest.javapublic class OrderTest {       Get an arra.pdf
 

More from apposekitchenpvd

What is question. i am ready to answerSolutionWhat is question.pdf
What is question. i am ready to answerSolutionWhat is question.pdfWhat is question. i am ready to answerSolutionWhat is question.pdf
What is question. i am ready to answerSolutionWhat is question.pdf
apposekitchenpvd
 
They are dependent since in order to be paired they must be correlat.pdf
They are dependent since in order to be paired they must be correlat.pdfThey are dependent since in order to be paired they must be correlat.pdf
They are dependent since in order to be paired they must be correlat.pdf
apposekitchenpvd
 
The answer is S2- has the largest radius.All the S species have t.pdf
The answer is S2- has the largest radius.All the S species have t.pdfThe answer is S2- has the largest radius.All the S species have t.pdf
The answer is S2- has the largest radius.All the S species have t.pdf
apposekitchenpvd
 
qweSolutionqwe.pdf
qweSolutionqwe.pdfqweSolutionqwe.pdf
qweSolutionqwe.pdf
apposekitchenpvd
 

More from apposekitchenpvd (20)

object 1 sink object2 float as for object 2 weig.pdf
                     object 1 sink object2 float  as for object 2 weig.pdf                     object 1 sink object2 float  as for object 2 weig.pdf
object 1 sink object2 float as for object 2 weig.pdf
 
Nitrogen Pollution Overuse or misuse of nitrogen .pdf
                     Nitrogen Pollution Overuse or misuse of nitrogen .pdf                     Nitrogen Pollution Overuse or misuse of nitrogen .pdf
Nitrogen Pollution Overuse or misuse of nitrogen .pdf
 
many of compounds tend to be unstable and either .pdf
                     many of compounds tend to be unstable and either .pdf                     many of compounds tend to be unstable and either .pdf
many of compounds tend to be unstable and either .pdf
 
Mendeleev,s law of periodic table .pdf
                     Mendeleev,s law of periodic table                .pdf                     Mendeleev,s law of periodic table                .pdf
Mendeleev,s law of periodic table .pdf
 
It is very slightly soluble in in boiling water, .pdf
                     It is very slightly soluble in in boiling water, .pdf                     It is very slightly soluble in in boiling water, .pdf
It is very slightly soluble in in boiling water, .pdf
 
hydride shift is more favorable due to shearing e.pdf
                     hydride shift is more favorable due to shearing e.pdf                     hydride shift is more favorable due to shearing e.pdf
hydride shift is more favorable due to shearing e.pdf
 
Highly conjugated organic compounds are normally .pdf
                     Highly conjugated organic compounds are normally .pdf                     Highly conjugated organic compounds are normally .pdf
Highly conjugated organic compounds are normally .pdf
 
H2O and OH- .pdf
                     H2O and OH-                                      .pdf                     H2O and OH-                                      .pdf
H2O and OH- .pdf
 
What is question. i am ready to answerSolutionWhat is question.pdf
What is question. i am ready to answerSolutionWhat is question.pdfWhat is question. i am ready to answerSolutionWhat is question.pdf
What is question. i am ready to answerSolutionWhat is question.pdf
 
They are dependent since in order to be paired they must be correlat.pdf
They are dependent since in order to be paired they must be correlat.pdfThey are dependent since in order to be paired they must be correlat.pdf
They are dependent since in order to be paired they must be correlat.pdf
 
The correct answer is Synapomorphy.ReasonFlowering plants or an.pdf
The correct answer is  Synapomorphy.ReasonFlowering plants or an.pdfThe correct answer is  Synapomorphy.ReasonFlowering plants or an.pdf
The correct answer is Synapomorphy.ReasonFlowering plants or an.pdf
 
Sufficient amount of water would absorbed by the carrot  in the dish.pdf
Sufficient amount of water would absorbed by the carrot  in the dish.pdfSufficient amount of water would absorbed by the carrot  in the dish.pdf
Sufficient amount of water would absorbed by the carrot  in the dish.pdf
 
D) [H3O+][F-] [HF] .pdf
                     D) [H3O+][F-]  [HF]                             .pdf                     D) [H3O+][F-]  [HF]                             .pdf
D) [H3O+][F-] [HF] .pdf
 
The answer is S2- has the largest radius.All the S species have t.pdf
The answer is S2- has the largest radius.All the S species have t.pdfThe answer is S2- has the largest radius.All the S species have t.pdf
The answer is S2- has the largest radius.All the S species have t.pdf
 
Sharing of electrons to form covalent bond one atom shares its one.pdf
Sharing of electrons to form covalent bond one atom shares its one.pdfSharing of electrons to form covalent bond one atom shares its one.pdf
Sharing of electrons to form covalent bond one atom shares its one.pdf
 
qweSolutionqwe.pdf
qweSolutionqwe.pdfqweSolutionqwe.pdf
qweSolutionqwe.pdf
 
Please follow the data and description Interface ID In the for.pdf
Please follow the data and description Interface ID In the for.pdfPlease follow the data and description Interface ID In the for.pdf
Please follow the data and description Interface ID In the for.pdf
 
C. is correct. note 1) aldehyde is more incline.pdf
                     C. is correct.  note 1) aldehyde is more incline.pdf                     C. is correct.  note 1) aldehyde is more incline.pdf
C. is correct. note 1) aldehyde is more incline.pdf
 
Because ethanol and water are miscible and they w.pdf
                     Because ethanol and water are miscible and they w.pdf                     Because ethanol and water are miscible and they w.pdf
Because ethanol and water are miscible and they w.pdf
 
Moral Standards - Morality is the disjuntion between right and wron.pdf
Moral Standards - Morality is the disjuntion between right and wron.pdfMoral Standards - Morality is the disjuntion between right and wron.pdf
Moral Standards - Morality is the disjuntion between right and wron.pdf
 

Recently uploaded

MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MysoreMuleSoftMeetup
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
CaitlinCummins3
 

Recently uploaded (20)

Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
 
Rich Dad Poor Dad ( PDFDrive.com )--.pdf
Rich Dad Poor Dad ( PDFDrive.com )--.pdfRich Dad Poor Dad ( PDFDrive.com )--.pdf
Rich Dad Poor Dad ( PDFDrive.com )--.pdf
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 

public final category Huffman non-public Huffman() ;personal stati.pdf

  • 1. public final category Huffman non-public Huffman() ; personal static category HuffmanNode } personal static category HuffManComparator implements Comparator<HuffmanNode> come node1.frequency - node2.frequency; } } /** * Compresses the string victimization huffman formula. * The huffman tree and also the huffman code area unit serialized to disk * * @param sentence The sentence to be serialized * @throws FileNotFoundException If file isn't found * @throws IOException If IO exception happens. */ public static void compress(String sentence) throws FileNotFoundException, IOException ought to atleast have one character."); } final Map<Character, Integer> charFreq = getCharFrequency(sentence); final HuffmanNode root = buildTree(charFreq); final Map<Character, String> charCode = generateCodes(charFreq.keySet(), root); final String encodedMessage = encodeMessage(charCode, sentence); serializeTree(root); serializeMessage(encodedMessage); } personal static Map<Character, Integer> getCharFrequency(String sentence) else } come map; } /** * Map<Character, Integer> map * Some implementation of that treeSet is passed as parameter. * @param map */
  • 2. personal static HuffmanNode buildTree(Map<Character, Integer> map) whereas (nodeQueue.size() > 1) // take away it to forestall object leak. come nodeQueue.remove(); } personal static Queue<HuffmanNode> createNodeQueue(Map<Character, Integer> map) return pq; } personal static Map<Character, String> generateCodes(Set<Character> chars, HuffmanNode node) { final Map<Character, String> map = new HashMap<Character, String>(); doGenerateCode(node, map, ""); come map; } personal static void doGenerateCode(HuffmanNode node, Map<Character, String> map, String s) doGenerateCode(node.left, map, s + '0'); doGenerateCode(node.right, map, s + '1' ); } personal static String encodeMessage(Map<Character, String> charCode, String sentence) come stringBuilder.toString(); } personal static void serializeTree(HuffmanNode node) throws FileNotFoundException, IOException attempt (ObjectOutputStream oosTree = new ObjectOutputStream(new FileOutputStream("/Users/ap/Desktop/tree"))) attempt (ObjectOutputStream oosChar = new ObjectOutputStream(new FileOutputStream("/Users/ap/Desktop/char"))) cushioned to mark finish of bit set relevant for deserialization. oosTree.writeObject(bitSet); } } } personal static category IntObject /* * Algo:
  • 3. * 1. Access the node * 2. Register the worth in bit set. * * * here true and false dont correspond to left branch and right branch. * there, * - true means that "a branch originates from leaf" * - false mens "a branch originates from non-left". * * conjointly since branches originate from some node, the foundation node should be provided as supply * or start line of initial branches. * * Diagram and the way associate degree bit set would look as a result. * (source node) * / * true true * / * (leaf node) (leaf node) * | | * false false * | | * * thus currently a little set sounds like [false, true, false, true] * */ personal static void preOrder(HuffmanNode node, ObjectOutputStream oosChar, BitSet bitSet, IntObject intObject) throws IOException bitSet.set(intObject.bitPosition++, true); // register branch in bitset preOrder(node.left, oosChar, bitSet, intObject); // take the branch. bitSet.set(intObject.bitPosition++, true); // register branch in bitset preOrder(node.right, oosChar, bitSet, intObject); // take the branch. } personal static void serializeMessage(String message) throws IOException attempt (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("/Users/ap/Desktop/encodedMessage")))
  • 4. } personal static BitSet getBitSet(String message) else } bitSet.set(i, true); // dummy bit set to grasp the length come bitSet; } /** * Retrieves back the initial string. * * * @return the initial uncompressed string * @throws FileNotFoundException If the file isn't found * @throws ClassNotFoundException If category isn't found * @throws IOException If IOException happens */ public static String expand() throws FileNotFoundException, ClassNotFoundException, IOException { final HuffmanNode root = deserializeTree(); come decodeMessage(root); } personal static HuffmanNode deserializeTree() throws FileNotFoundException, IOException, ClassNotFoundException attempt (ObjectInputStream oisBranch = new ObjectInputStream(new FileInputStream("/Users/ap/Desktop/tree"))) attempt (ObjectInputStream oisChar = new ObjectInputStream(new FileInputStream("/Users/ap/Desktop/char"))) federal agencyBranch.readObject(); come preOrder(bitSet, oisChar, new IntObject()); } } } /* * Construct a tree from: * input [false, true, false, true, (dummy faithful mark the top of bit set)] * The input is made from preorder traversal * * Algo: * one produce the node.
  • 5. * 2. browse what's registered in bitset, and choose if created node is meant to be a leaf or non- leaf * */ personal static HuffmanNode preOrder(BitSet bitSet, ObjectInputStream oisChar, IntObject o) throws IOException deciding if created node is that the leaf or non-leaf. if (!bitSet.get(o.bitPosition)) succeeding position to future stack frame by doing computation before preOrder is termed. node.ch = oisChar.readChar(); come node; } o.bitPosition = o.bitPosition + 1; // feed future position to future stack frame by doing computation before preOrder is termed. node.left = preOrder(bitSet, oisChar, o); o.bitPosition = o.bitPosition + 1; // feed future position to future stack frame by doing computation before preOrder is termed. node.right = preOrder(bitSet, oisChar, o); come node; } personal static String decodeMessage(HuffmanNode node) throws FileNotFoundException, IOException, ClassNotFoundException attempt (ObjectInputStream OIS = new ObjectInputStream(new FileInputStream("/Users/ameya.patil/Desktop/encodedMessage"))) worker = node; // since huffman code generates full binary tree, temp.right is actually null if worker.left is null. whereas (temp.left != null) { if (!bitSet.get(i)) { worker = worker.left; } else { worker = worker.right; } i = i + 1; } stringBuilder.append(temp.ch); } come stringBuilder.toString(); }
  • 6. } public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException variety of characters Huffman.compress("some"); Assert.assertEquals("some", Huffman.expand()); // odd range of characters Huffman.compress("someday"); Assert.assertEquals("someday", Huffman.expand()); // repetition even range of characters + house + non-ascii Huffman.compress("some some#"); Assert.assertEquals("some some#", Huffman.expand()); // odd range of characters + house + non-ascii Huffman.compress("someday someday&"); Assert.assertEquals("someday someday&", Huffman.expand()); } } Solution public final category Huffman non-public Huffman() ; personal static category HuffmanNode } personal static category HuffManComparator implements Comparator<HuffmanNode> come node1.frequency - node2.frequency; } } /** * Compresses the string victimization huffman formula. * The huffman tree and also the huffman code area unit serialized to disk * * @param sentence The sentence to be serialized * @throws FileNotFoundException If file isn't found * @throws IOException If IO exception happens. */ public static void compress(String sentence) throws FileNotFoundException, IOException ought to atleast have one character.");
  • 7. } final Map<Character, Integer> charFreq = getCharFrequency(sentence); final HuffmanNode root = buildTree(charFreq); final Map<Character, String> charCode = generateCodes(charFreq.keySet(), root); final String encodedMessage = encodeMessage(charCode, sentence); serializeTree(root); serializeMessage(encodedMessage); } personal static Map<Character, Integer> getCharFrequency(String sentence) else } come map; } /** * Map<Character, Integer> map * Some implementation of that treeSet is passed as parameter. * @param map */ personal static HuffmanNode buildTree(Map<Character, Integer> map) whereas (nodeQueue.size() > 1) // take away it to forestall object leak. come nodeQueue.remove(); } personal static Queue<HuffmanNode> createNodeQueue(Map<Character, Integer> map) return pq; } personal static Map<Character, String> generateCodes(Set<Character> chars, HuffmanNode node) { final Map<Character, String> map = new HashMap<Character, String>(); doGenerateCode(node, map, ""); come map; } personal static void doGenerateCode(HuffmanNode node, Map<Character, String> map, String s) doGenerateCode(node.left, map, s + '0');
  • 8. doGenerateCode(node.right, map, s + '1' ); } personal static String encodeMessage(Map<Character, String> charCode, String sentence) come stringBuilder.toString(); } personal static void serializeTree(HuffmanNode node) throws FileNotFoundException, IOException attempt (ObjectOutputStream oosTree = new ObjectOutputStream(new FileOutputStream("/Users/ap/Desktop/tree"))) attempt (ObjectOutputStream oosChar = new ObjectOutputStream(new FileOutputStream("/Users/ap/Desktop/char"))) cushioned to mark finish of bit set relevant for deserialization. oosTree.writeObject(bitSet); } } } personal static category IntObject /* * Algo: * 1. Access the node * 2. Register the worth in bit set. * * * here true and false dont correspond to left branch and right branch. * there, * - true means that "a branch originates from leaf" * - false mens "a branch originates from non-left". * * conjointly since branches originate from some node, the foundation node should be provided as supply * or start line of initial branches. * * Diagram and the way associate degree bit set would look as a result. * (source node) * / * true true * /
  • 9. * (leaf node) (leaf node) * | | * false false * | | * * thus currently a little set sounds like [false, true, false, true] * */ personal static void preOrder(HuffmanNode node, ObjectOutputStream oosChar, BitSet bitSet, IntObject intObject) throws IOException bitSet.set(intObject.bitPosition++, true); // register branch in bitset preOrder(node.left, oosChar, bitSet, intObject); // take the branch. bitSet.set(intObject.bitPosition++, true); // register branch in bitset preOrder(node.right, oosChar, bitSet, intObject); // take the branch. } personal static void serializeMessage(String message) throws IOException attempt (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("/Users/ap/Desktop/encodedMessage"))) } personal static BitSet getBitSet(String message) else } bitSet.set(i, true); // dummy bit set to grasp the length come bitSet; } /** * Retrieves back the initial string. * * * @return the initial uncompressed string * @throws FileNotFoundException If the file isn't found * @throws ClassNotFoundException If category isn't found * @throws IOException If IOException happens */ public static String expand() throws FileNotFoundException, ClassNotFoundException, IOException { final HuffmanNode root = deserializeTree();
  • 10. come decodeMessage(root); } personal static HuffmanNode deserializeTree() throws FileNotFoundException, IOException, ClassNotFoundException attempt (ObjectInputStream oisBranch = new ObjectInputStream(new FileInputStream("/Users/ap/Desktop/tree"))) attempt (ObjectInputStream oisChar = new ObjectInputStream(new FileInputStream("/Users/ap/Desktop/char"))) federal agencyBranch.readObject(); come preOrder(bitSet, oisChar, new IntObject()); } } } /* * Construct a tree from: * input [false, true, false, true, (dummy faithful mark the top of bit set)] * The input is made from preorder traversal * * Algo: * one produce the node. * 2. browse what's registered in bitset, and choose if created node is meant to be a leaf or non- leaf * */ personal static HuffmanNode preOrder(BitSet bitSet, ObjectInputStream oisChar, IntObject o) throws IOException deciding if created node is that the leaf or non-leaf. if (!bitSet.get(o.bitPosition)) succeeding position to future stack frame by doing computation before preOrder is termed. node.ch = oisChar.readChar(); come node; } o.bitPosition = o.bitPosition + 1; // feed future position to future stack frame by doing computation before preOrder is termed. node.left = preOrder(bitSet, oisChar, o); o.bitPosition = o.bitPosition + 1; // feed future position to future stack frame by doing computation before preOrder is termed. node.right = preOrder(bitSet, oisChar, o); come node;
  • 11. } personal static String decodeMessage(HuffmanNode node) throws FileNotFoundException, IOException, ClassNotFoundException attempt (ObjectInputStream OIS = new ObjectInputStream(new FileInputStream("/Users/ameya.patil/Desktop/encodedMessage"))) worker = node; // since huffman code generates full binary tree, temp.right is actually null if worker.left is null. whereas (temp.left != null) { if (!bitSet.get(i)) { worker = worker.left; } else { worker = worker.right; } i = i + 1; } stringBuilder.append(temp.ch); } come stringBuilder.toString(); } } public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException variety of characters Huffman.compress("some"); Assert.assertEquals("some", Huffman.expand()); // odd range of characters Huffman.compress("someday"); Assert.assertEquals("someday", Huffman.expand()); // repetition even range of characters + house + non-ascii Huffman.compress("some some#"); Assert.assertEquals("some some#", Huffman.expand()); // odd range of characters + house + non-ascii Huffman.compress("someday someday&"); Assert.assertEquals("someday someday&", Huffman.expand()); } }