13b: Huffman Trees

ICS211, Spring 2013
Dr. Zach

(switch view)

Status check

Recap for Exam 3

Integers as binary

dec     binary    hex     oct
65 == 01000001 == 0x41 == 0101

Encoding: Characters as Binary

ASCII (or Unicode UTF-8)

ltr   binary    hex  dec
'A'  01000001  0x41  65
'a'  01100001  0x61  97
'b'  01100010  0x62  98
'0'  00110000  0x30  48
'1'  00110001  0x31  49
'2'  00110010  0x32  50
'3'  00110011  0x33  51

8 bits per byte; 1 byte = 1 symbol; 256 possible symbols.

int ltr = 'A';
System.out.println(ltr);  //prints 65

Customized Encoding

Entropy Encoding

Huffman Coding

Building the Tree

Example (symbol:count): A:32, 0:16, 1:8, 2:1, 3:1, 4:1, 5:1, 6:1; 7:1, 8:1, 9:1.
(Note: tree can vary depending on your policy of child order as they come out of PQ)
You try. Build tree from: a:7, b:5, c:4, d:2, e:1.
What are the paths (0 = left, 1 = right) to each node?

Using the Tree: Encoding

Using the Tree: Decoding

Sending the Tree

Receiving the Tree

  create a stack
  read first 0 from stream
  set root to a new empty internal node and push it onto stack
  while stack is not empty:
      read next bit from stream
      if bit is 0:
          create a new empty internal node
      else on a 1:
          read 8 more bits and create a new leaf node with the data
      if stack.peek() does not have left child:
          add the new node as left child of stack.peek()
      else:
          add the new node as right child of stack.peek()
          pop the top of the stack (because now it has two children)
      if the new node is an internal node:
          push the new node onto stack

You try: 01[c]01[d]1[b]

Deep breath...

Technical details remaining

Reading bytes from a file

What about getting bits out of a byte?

[Out of time; will return to this on Wed.]

For next time...