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 new 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 BinaryTreeNode and Stack classes provide 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 BinaryTreeNode methods you will need are the following:

t = new BinaryTreeNode(S) - constructs a new binary tree node t and assigns S as its value
t.setLeft(V) - attaches the BinaryTreeNode V as the left child t
t.setRight(U) - attaches the BinaryTreeNode U as the right child of t
t.left() - retrieves the root of the left subtree of t
t.right() - retrieves the root of the right subtree of t
t.value() - returns the value stored in 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 the last tree in the stack after it has been built, in preorder, inorder, and postorder form. Below is a simple recursive method that displays a binary tree in postorder form.

    public static void display (BinaryTreeNode root) {
        if (root != null) {
           display(root.left());
           display(root.right());
           System.out.println(root.value() + " ");
        }
    }

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. On the following page 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.


Sample output from running the program to find a Huffman encoding of the message If a woodchuck could chuck wood!