CS 46B - Lecture 23

Cover page image

Pre-class reading

Huffman coding

Huffman coding

Lecture 23 Clicker Question 1

What is the Encoding of ALOHA?

  1. 100000011000110
  2. 100000001000110
  3. 10000000100010
  4. Something else

Lecture 23 Clicker Question 2

Which of these is NOT a Huffman code?

  1. 7-bit ASCII
  2. UTF-8
  3. Morse code
  4. None of them are

Building a Good Huffman Tree

Building a Good Huffman Tree

Huffman Algorithm

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

Huffman Algorithm

Huffman Algorithm

Huffman Tree Implementation

class Node implements Comparable<Node>
{
   public char character;
   public int frequency;
   public Node left;
   public Node right;
   public int compareTo(Node other) { ... }
}

Huffman Tree Implementation

PriorityQueue<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);
}

Huffman Tree Implementation

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();

Lecture 23 Clicker Question 3

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 ___; }
  1. character - other.character
  2. other.character - character
  3. frequency - other.frequency
  4. other.frequency - frequency

Decoding

public 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;
}

Encoding

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");
      }
   }
}

Lecture 23 Clicker Question 4

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?

  1. Give each node a link to the parent
  2. Add an instance variable to each node that contains the encoding, and set it when the node is constructed.
  3. Either of these will work
  4. Neither of these will work