CS210: Intermediate Computing/Data Structures (Java)

Lab #8

Purpose

This lab gives you experience implementing a level order traversal of a binary tree. 

Pre-Lab Assignment

Study the Level Order Traversal Algorithm given in lecture 20 notes (slide 13) so that you understand the design of the method that you will need to implement in this lab.

 

Prior to the lab session, do the following to get started:

Download, save, and unzip the file Lab8.zip on your removable media (floppy disk, Zip disk, or memory stick) creating a directory named Lab8.

Open the Dr Java Application on your PC. 

Create a new Dr Java project in the Lab8 directory by selecting the menu Project -> New naming the project file Lab8 and saving it in your new Lab8 directory.  (Dr Java automatically opens an Untitled file in the editor window which you will ignore.)

Using the menu File-> Open, open the files: BinaryTreeADT.java, BinaryTreeNode.java, LinkedBinaryTree.java, and LevelTraverse.java  Dr Java will automatically include these source files in the Project.  Use the menu Project -> Save to save the updated project file. 

Lab Activities

The LevelTraverse class has the main method.  Its main method sets up a test case binary tree using the LinkedBinaryTree class and calls the LinkedBinaryTree class method LevelOrderIterator.  It uses the iterator object obtained to print the contents of the tree in level order.  

 

You need to design and add code to the levelOrderIterator method of the LinkedBinaryTree class where indicated. 

 

1.  Create a queue using the java.util.Queue interface and java.util.LinkedList class.

 

2.  Create a linkedlist using the java.util.LinkedList class to contain the elements in level order.

 

3.  Write code to implement the algorithm in lecture 20 notes (slide 13).

 

You need to complete your code for the levelOrderIterator method for use in Lab 9.

 

A sample run of the finished program is shown here:

 

> java LevelTraverse

Root

Child1

Child2

Grandchild1

Grandchild2

. . .

Report

In your report, explain why you got some nulls printed as part of the level order traversal.