CSC 226 ASSIGNMENT 2 – PROGRAM and WRITTEN solution

$29.99

Original Work ?

Download Details:

  • Name: Ass2-f7izwm.zip
  • Type: zip
  • Size: 1.24 MB

Category: You will Instantly receive a download link upon Payment||Click Original Work Button for Custom work

Description

5/5 - (8 votes)

1 Programming Assignment
The assignment is to design and implement a program that computes the percentage of red nodes in a
given red-black tree. You will test your program by running at least 100 trials of the experiment of
inserting n random keys into an initially empty tree, for 𝑛 = 104
, 105
, and 106
.
Input: A random sequence of 𝑛 distinct integers.
Output: The percentage of red nodes after inserting all 𝑛 integers into a red-black tree in the
given order.
You will accomplish this by creating a java class RedBlackBST using algorithm 3.4, on page 439 of the
Sedgewick text, as a template. To complete the algorithm you will need to include the code found on
pages 433-436 as well. This includes the code for the Node class, and the isRed(), flipColors(),
rotateLeft(), and rotateRight() methods (the delete() methods are not needed.)
Your task is to add to this a method called percentRed() which returns the percentage of red nodes in the
red-black tree. How this is accomplished is up to you. You may alter the other methods as needed to
accomplish this goal. Keep in mind, the more efficient your code is at doing this the better. That is, try to
minimize both space and time, weighing the tradeoff between the two where necessary.
You will also need to add a main() method, which should do one of two things:
1. If one exists, read the contents of a text file provided on the command line, for example
C:\> java RedBlackBST test_file.txt
for correctness testing by the marker.
2. Else, generate your own sequences to accomplish your experiments for the different values of 𝑛 given
above.
2 Test Datasets
A set of input files containing test data are available in the β€˜Assignments’ folder under the β€˜Resources’ tab
on ConneX, sorted by their size. You should ensure that your implementation gives the correct answer on
these test files before submitting.
3 Evaluation Criteria
The programming assignment will be marked out of 25, based on a combination of automated testing and
human inspection. Remember to report your results and write your hypothesis as question 5 in the written
portion. That 5 marks is not included in this program mark.
Score Description
0 – 5 Submission does not compile.
5 – 15 Compiles but incorrectly reports percentages.
15 – 20 Correctly calculates the percent red but not
efficient as possible.
20 – 25 Correctly and efficiently calculates the percentage
of red nodes.
To be properly tested, every submission must compile correctly as submitted. If your submission does
not compile for any reason (even trivial mistakes like typos), it will receive at most 5 out of 25. The
best way to make sure your submission is correct is to download it from ConneX after submitting and test
it. You are not permitted to revise your submission after the due date, and late submissions will not be
accepted, so you should ensure that you have submitted the correct version of your code before the due
date. ConneX will allow you to change your submission before the due date if you notice a mistake. After
submitting your assignment, ConneX will automatically send you a confirmation email. If you do not
receive such an email, your submission was not received. If you have problems with the submission
process, send an email to the instructor before the due date.

 

1. Draw the 2-3 trees that result when you insert the keys Y L P M X H C R A E S in that order into an
initially empty tree. There should be 11 trees in all. Use the final tree to construct the corresponding
red-black tree.
2. Consider Case 2 (taken from slide 50 in Lecture-5-AVLTrees.pdf and shown below). On the left is
the subtree 𝑆′ rooted at 𝑧 that results from inserting key π‘˜ into an AVL tree 𝑇. On the right is the
subtree 𝑆
βˆ—
rooted at node π‘₯ which is the result of restructuring at node 𝑧 in 𝑆
β€²
. Prove that the height of
𝑆
βˆ—
is equal to the height of 𝑆, the subtree rooted at 𝑧 prior to inserting key π‘˜.
3. An inversion in a sequence is an out-of-order pair; i.e., 𝑖 < 𝑗 but π‘Žπ‘– > π‘Žπ‘—
. Inversions are discussed
briefly in the book on page 252. For example, the sequence (5,3,2,1,4) has 7 inversions. What is the
minimum number of inversions of a permutation of 1,2, … , 𝑛? What is the maximum number of
inversions of a permutation of 1,2, … , 𝑛?
4. Explain carefully how to use red-black trees to compute the number of inversions in a permutation in
time 𝑂(𝑛 log 𝑛). Effectively, you may need to modify the code for Algorithm 3.4 on page 439.
Explain in detail any changes that you would make to method put().
5. In the programming portion (full specification to come) you are going to write a program that
computes the percentage of red nodes in a given red-black tree. You will test your program by
running 100 trials of the experiment of inserting 𝑁 random integer keys into an initially empty tree,
for 𝑁 = 104
, 105
, and 106
. Here you will report your findings and formulate a hypothesis about the
percentage of red nodes in any red-black tree based on your results.