Computer Science 210 Lab 7: Binary Trees and Huffman Coding
Due: November 6, 1997

Objectives and Overview: The binary tree is a widely-used data structure in computer science applications, from parsing arithmetic expressions to designing efficient search algorithms. This lab involves building a binary tree, traversing it, and understanding its use in the design of a data compression algorithm called "Huffman coding." Chapter 11 in your text provides valuable information to support this lab.

Part 1 - From Polish Expressions to Binary Trees

1. Draw the binary tree that is represented by each of the following Polish expressions.

    Polish Expression Infix Interpretation
    3 4 -
    3 4 5 - -
    3 4 - 5 -
    35 17 40 9 - * + 7 -
    3 - 4
    3 - (4 - 5)
    3 - 4 - 5
    35 + 17 * (40 - 9) - 7

Notice that the process that can be used to draw a tree could be realized by the following algorithm on a stack T of binary trees:

Scan the input expression from left to right, one string S at a time. For each S read, do one of the following actions:
a. If S is an operand, push it onto the stack T as a new tree whose single (root) node is S.
b. If S is an operator, pop the top two trees U and V (in that order), construct the binary tree
out of these, and push it back onto the stack.

When this algorithm finishes (the end of the input is reached), the stack T should contain a single entry, a binary tree that represents the original Polish expression.

2. Trace this algorithm for the third Polish expression in the above table. Show the stack contents just after each of the strings in the expression is scanned and processed by step a or b above.

Part 2 - Design a Program to Build a Tree and Traverse It

The BinaryTree and Stack classes provides all the methods necessary for implementing this algorithm. You are already familiar with the push and pop methods of the Stack class from Lab 6. The key BinaryTree methods you will need are the following:

t = new BinaryTree() - constructs a new binary tree t
t.insert(new String(S))- inserts S as the root of an empty tree t
(or as the left or right child of the most recently visited node) and sets the inserted node as the
"current node".
t.moveLeft() - moves the cursor off the left child of the current node
t.moveRight() - moves the cursor off the right child of the current node
t.moveUp() - moves to the parent of the current node
t.elements() - an iterator for an inorder traversal of t
t.preorderElements() - a preorder iterator for t
t.postorderElements() - a postorder iterator for t

Design a complete Java program that uses these two classes and implements the tree building algorithm described in Part 1. The program should display, using the appropriate iterators, the last tree in the stack after it has been built, in preorder, inorder, and postorder form.

In debugging your program, be sure that the postorder display is identical with the series of strings S that were entered as input. That is, a postorder tree traversal for an expression is identical with that expression's Polish representation.

Part 3 - Examine the Huffman Coding Algorithm

The program Huffman.java is a good example of the use of binary trees and linked lists to solve an important problem in computing. You should read over the discussion of this problem that appears in your text on page 203. Below is a sample output from running the program to find a Huffman encoding of the message If a woodchuck could chuck wood!.

Locate this program in the CS210 (Tucker) -> examples folder, and make a copy of it on the desktop. Now build a project called Lab7.µ that will produce the output shown above. Be sure to change the Edit -> Target Settings and Edit -> Java Target to Huffman, and the Edit -> Linker Output to type Application before running this project.

Using a slightly different run of this program for the input If a woodchuck could chuck wood (without the exclamation mark at the end), answer the following quesitons:

  1. Draw a diagram of the linked list that represents the variable freq.
  2. Draw another diagram of the ordered list that represents the variable trees.
  3. Show the result, diagrammatically, of executing each of the first six steps of the while loop that reduces the ordered list trees to a single tree.
  4. The Huffman encodings of the individual letters in this slightly different input message are different from those of the letters in the original message, shown above. Explain why, in as much detail as you can.
  5. What is it about Huffman encoding that requires the design of a Comparable class huffmanTree? That is, why can't the predesigned, plain vanilla class binaryTree be used instead?

Lab 7 Deliverables:

Submit your Java program from Part 2 of this lab by dragging it to the Drop Box folder. Also hand in a hard copy listing of your program, along with your answers to the questions in Parts 1 and 3.

You should work on this lab individually, since it will be part of test #2; all programming and answers to the questions should be developed by you. I will be available for help during the week on Friday, Tuesday, and Wednesday afternoons from 3:30-5:00. I also will reply to e-mail questions over the weekend.