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.
1. Draw the binary tree that is represented by each of the following Polish expressions.
Polish Expression | Infix Interpretation |
|
|
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:
S
/ \
V U
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.
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:
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.
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:
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!