CMSC 341 Spring 2004
Project 3

Assigned Monday, March 8, 2004
Due 11:59 pm on Sunday, March 21, 2004
Updates: 10 March 04 A common implementation of a level-order traversal requires the use a queue. The author's implementation of an array-based queue is available for your use from Mr. Frey's directory

A sample output is provided below
Updates: 12 March 04 Due date for Project 3 has been extended to Sunday, March 28.
Updates: 16 March 04 A sentence at the end of "Files To Be Submitted" section concerning Stack.C, string.C and bector.C is deleted. These are either not needed for this project or are part of standard library (cstdlib).
Updates: 17 March 04 A error in the sample output is corrected. The tree heights were 1 greater than it should be.

Objectives

The purpose of this project is to give you significant exposure to Binary Search Trees (BST), tree traversals, and recursive code. You will also be asked to implement a technique that balances a BST with respect to the numbers of nodes in the left and right subtrees of each node. You will be modifying the author's BST code and implementing several new BST related methods.

Description

An arbitrary BST is seldom balanced, the left and right subtrees of a node may have different heights or contain different numbers of nodes. In the class we have discussed several techniques for constructing height-balanced BST. In this project, you will explore balancing BST based on the weights of its subtrees. Here we define the weight of a BST to be the number of nodes in that tree. A node in a BST is weight-balanced if the weights of its left and right subtrees differ by no more than 1. A weight-balanced BST is one in which every node is weight-balanced. An important property of weight-balanced BST is that the value at any node v is a median of the values at all nodes in the subtree rooted at v.

The following straightforward but inefficient recursive procedure can be used to achieve weight-balance of a given BST T:

weightBalance(T)
  1. find the median of T;
  2. create a new BST T' with a single node, the root, whose value is the found median of T;
  3. retrieve and insert elements of all nodes of T, except the median, into T';     /* T' thus has a weight-balanced root */
  4. replace T by T';
  5. call this procedure to balance the left and right subtrees of T.
In this project you are asked to write a program that first constructs a BST T by successively inserting a given number of randomly generated non-negative integers into an initially empty tree; then weight-balance T according to the procedure outlined above. Your program should also prints both the original T and its weight-balanced version, using a level-order traversal, to a given level.

Here are some specifics concerning this assignment.
  1. Command Line. Your program will accept three command line arguments:
  2. Random numbers. The random numbers shall be generated in the same way as in Project 2, except that they must be integers between 0 and 9999, inclusive. To guarantee the tree to have the given weight N, you need to properly handle duplicate integers that may be generated by the system's random number generator. For this purpose you need to modify the insert method in author's code since it does nothing when attempting to insert a duplicate element. Instead, your modified insert method should return "failure" when attempting to insert an element which is already in the tree.
  3. Median. There are two medians when N is even. In this project you should always use the smaller of the two. (An outline of algorithm findMedian is given in the Hints section below.)
  4. Weight-balancing. For efficiency reason, you should use pre-order traversal when retrieve and insert values in step 3 of the weightBalance procedure
  5. Print tree. You should print the tree in level-order, up to the level L. If the tree's height is less than L, then print the entire tree. After printing a tree, also print its height and its median.
          Tree nodes must be printed as ordered triples of values in the format ( x, y, z ), where x is the value found in the node's parent, y is the value found in the node being printed (print the value of ITEM_NOT_FOUND for the value of the root's "parent"), and z is the weight of the tree rooted at that node.  Your level-order tree print must start with a new line with each level, and print 4 nodes per line if there are more than 4 nodes at a given level.
          The format for printing trees is shown in the sample output below.

Your Tasks


Program Requirements

  1. Your code must follow all project guidelines established for this course. See the project organization and project policies web pages.  You are not required to modify the author's code to follow these guidelines.
  2. You are free to name your main program file, but your executable must be named Proj3
  3. Your code must handle (try/throw/catch) any exceptions thrown by the author's code or by your own classes.
  4. Your code must be able to handle trees of any size.

Sample Output

The following output is for format reference only.
bash-2.03$./Proj3 1024 100 4
Before balancing the level order output is: 

Level 0:
	(-1,5283,100)
Level 1:
	(5283,2086,46)	(5283,8027,53)
Level 2: 
	(2086,1067,17)	(2086,4646,28)	(8027,7673,28)	(8027,9552,24)
Level 3:
	(1067,400,7)	(1067,1287,9)	(4646,2150,19)	(4646,5226,8)
	(7673,5436,24)	(7673,7999,3)	(9552,9182,18)	(9552,9925,5)
Level 4:
	(400,1,2)	(400,666,4)	(1287,1197,1)	(1287,1803,7)
	(2150,2217,18)	(5226,4916,5)	(5226,5237,2)	(5436,5352,2)
	(5436,7565,21)	(7999,7829,2)	(9182,8727,15)	(9182,9336,2)
	(9925,9699,4)

Median: 5436
Height of the tree: 12

After balancing the level order output is: 

Level 0:
	(-1,5436,100)

Level 1:
	(5436,2794,49)	(5436,7999,50)
Level 2:
	(2794,1323,24)	(2794,4286,24)	(7999,6674,24)	(7999,8862,25)
Level 3:
	(1323,776,11)	(1323,2086,12)	(4286,3436,11)	(4286,5126,12)
	(6674,6049,11)	(6674,7118,12)	(8862,8420,12)	(8862,9518,12)
Level 4:
	(776,400,5)	(776,1197,5)	(2086,1743,5)	(2086,2465,6)
	(3436,2960,5)	(3436,3968,5)	(5126,4891,5)	(5126,5242,6)
	(6049,5692,5)	(6049,6235,5)	(7118,6883,5)	(7118,7568,6)
	(8420,8126,5)	(8420,8563,6)	(9518,9072,5)	(9518,9624,6)

Median: 5436
Height of the tree: 6

Files To Be Submitted

Submit any files which you have written -- source code and makefile. Your files MUST be named as listed below.

Submit the files using the procedure given to you for your section of the course.
If your makefile is set up correctly, you should be able to execute the command make submit.


Hints


Submit Tools

There are a number of tools available to you to check on your submittal. It is your responsibility to ensure that the submittal is correct and will result in a successful compilation of your project. Do not wait till the last minute to submit your files. Give yourself enough time to check that your submittal is correct.

If you don't submit a project correctly, you will not get credit for it. Why throw away all that hard work you did to write the project? Check . Make sure they work. Do this before the due date.

Documentation for the submit program is on the web at http://www.gl.umbc.edu/submit/ . One of the tools provided by the submit program is
submitls. It lists the names of the files you have submitted.

Additionally, there are two programs for use only by CMSC-341 students (not part of the UCS submit program). They are in the directory
/afs/umbc.edu/users/d/e/dennis/pub/CMSC341/ and are named submitmake and submitrun. You can use these programs to make or run your submitted projects.

The syntax is similar to that for submit:

submitmake <class> <project>

Example:  submitmake cs341 Proj3

This makes the project, and shows you the report from the make utility. It cleans up the directory after making the project (removes .o and ii_files), but leaves the
executable in place.

submitrun <class> <project> [command-line args]

Example:  submitrun cs341 Proj3 12345 20 4

This runs the project, assuming there is an executable (i.e. submitmake was run successfully).


Grading and Academic Integrity

Your project will be tested using a variety of command lines, some of which will be invalid.
Your project will also be tested using a variety of command files which will test various conditions which your code should handle.

Project grading is described in the Project Policy handout.
Cheating in any form will not be tolerated. Please re-read the Project Policy handout for further details on honesty in doing projects for this course.

Remember, the due date is firm. Submittals made after midnight of the due date will not be accepted. Do not submit any files after that time.