
What is the Encoding of ALOHA?
Which of these is NOT a Huffman code?


Add all nodes to a priority queue While there are two nodes left Remove the two nodes with the smallest frequencies Make them children of a parent whose frequency is the sum of the child frequencies Add the parent to the priority queue

White 0 Pink 100 Yellow 1010 Blue 10110 Green 10111 Orange 11
class Node implements Comparable<Node>
{
public char character;
public int frequency;
public Node left;
public Node right;
public int compareTo(Node other) { ... }
}
Comparable so we can put the nodes in a priority queueMap<Character, Integer> with the frequenciesPriorityQueue<Node> nodes = new PriorityQueue<Node>();
for (char ch : frequencies.keySet())
{
Node newNode = new Node();
newNode.character = ch;
newNode.frequency = frequencies.get(ch);
nodes.add(newNode);
}
while (nodes.size() > 1)
{
Node smallest = nodes.remove();
Node nextSmallest = nodes.remove();
Node newNode = new Node();
newNode.frequency = smallest.frequency + nextSmallest.frequency;
newNode.left = smallest;
newNode.right = nextSmallest;
nodes.add(newNode);
}
root = nodes.remove();
Which line needs to be filled in in the definition of the compareTo method of the Node class to make the preceding code work?
public int compareTo(Node other) { return ___; }
character - other.characterother.character - characterfrequency - other.frequencyother.frequency - frequencypublic String decode(String input)
{
String result = "";
Node n = root;
for (int i = 0; i < input.length(); i++)
{
char ch = input.charAt(i);
if (ch == '0') { n = n.left; }
else { n = n.right; }
}
if (n.left == null) // n is a leaf
{
result = result + n.character;
n = root;
}
return result;
}
class Node implements Comparable<Node>
{
. . .
public void fillEncodingMap(Map<Character, String> map, String prefix)
{
if (left == null) // It’s a leaf
{
map.put(character, prefix);
}
else
{
left.fillEncodingMap(map, prefix + "0");
right.fillEncodingMap(map, prefix + "1");
}
}
}
Another way of doing the encoding would be to make a map from each character to the node that represents it.
Map<Character, Node> encodingMap;
When doing this, what else do you need to do?