Given the assumptions of no friction and constant acceleration of 32 feet per second per second,
the height of an object is given by
[h=-1/2g t^2+v_0t+h_0] where [g=32] , [v_0=47] is the initial speed and [h_0=3] is the initial
height of the ball. Sub in these values to get:
[h=-16t^2+47t+3]
The height of the ball in feet after t seconds is [h=-16t^2+47t+3] .
Solution
Given the assumptions of no friction and constant acceleration of 32 feet per second per second,
the height of an object is given by
[h=-1/2g t^2+v_0t+h_0] where [g=32] , [v_0=47] is the initial speed and [h_0=3] is the initial
height of the ball. Sub in these values to get:
[h=-16t^2+47t+3]
The height of the ball in feet after t seconds is [h=-16t^2+47t+3] ..
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());
}
}