Loading
Home › Computer Science Help › red-black trees
Status: Completed

red-black trees

Date Posted: 18/05/2020
Category: Computer Science
Due Date: 20/05/2020
Instruction
Learning objectives: Show understanding of red-black trees, a common data structure for efficiently implementing search Write code that correctly manipulates trees Analyze the complexity of an operation on balanced trees (This is adapted from Problem 14-2 in Introduction to Algorithms by Cormen, Leiserson, and Rivest (first edition), pages 278-279.) I recommend reviewing the materials from Week 13, Tuesday: Balanced Trees before you attempt this problem. Your task is to implement the following operation on red-black trees: public void join(Key k, Value v, RedBlackBST t) { assert (max().compareTo(k) != 1); // k must be >= all keys in this assert(k.compareTo(t.min()) != -1; // k must be <= all keys in t /* If N == size() + t.size(), then with ~lg(N) comparisons, make this tree include v and all the keys and values in t. Both t and this may be mutated. */ } Example call: RedBlackBST t1 = // initialize t1 here RedBlackBST t2 = // initialize t2 here // Make sure that all the keys in t1 are = 29 t1.join(29, "whatever", t2); // after join() returns, t1 will contain all the original keys from t1; plus 29; // plus all the keys from t2 Definition: Black height Black height is defined as the number of black links from the root of the tree to any leaf. For example: IMG_2737.jpg This is a tree whose black height is 4. Its left child, which is black, has black height 3. For example, the path consisting of the nodes 26, 17, 14, 10, 7, and 3 contains four black links. The black link from a leaf (such as 3) to null counts as a black link. Example The following examples show what join() does: IMG_2720.jpg NOTE: the picture above is slightly wrong -- the 29 node should be red, not black. A "fixup" pass has to be applied, because the joined tree has a right-leaning red node. This pass applies a rotateLeft operation, and then sets the root's color to be black: IMG_2736.jpg In the top half of the first page, st1 and t2 are shown before calling: st1.join(29, "whatever", t2); st1's black height is 3 and t2's black height is 2. join() needs to find a node, y, within st1 such that y's black height is 2 and y's key is as big as possible. That node is 27, circled in pink in the bottom half of the page. join() makes a new node containing k, makes y the left child of k, and makes t2 the right child of k. As an exercise, it's worth figuring out: why this operation is guaranteed to create a valid binary search tree (assuming st1 and t2 were both valid red-black trees) why this operation is guaranteed to uphold all of the red-black tree properties... ...assuming that the correct color is chosen for the node with key k. (What color is that?) And assuming that a "fixup" step is performed after splicing the new tree rooted at k into st1. (What is the "fixup" step?) Required Strategy The RedBlackBST class has been modified for you to include a new instance variable that denotes the black height of this red-black tree. Right now, the value of blackHeight is always 0. You need to figure out what code to modify so that it correctly reflects the black height of the tree. private static int blackHeight; You only need to modify the RedBlackBST class. You do not need to modify the Node inner class. Hint: The isBalanced() method computes the black height dynamically, but there's still work involved in keeping this instance variable consistent after insertions or deletions. Hint: When put() is called, where are all the locations in the code where the black height of the tree could change? The answer will tell you where to add code to change the blackHeight instance variable. Hint: Ask yourself the same question about deletion (note that there are three different deletion methods). While descending through a tree, you can determine the black height of each node in constant time. I've added two new methods to the RedBlackBST class for you. Explain how to fill in the missing code in these methods. Hint: use recursion. private Node findBlackNodeWithLargestKey(int blackHeight) { // Returns a black node whose black height is blackHeight, // and has the largest possible key of all such nodes } private Node findBlackNodeWithSmallestKey(int blackHeight) { // Returns a black node whose black height is blackHeight, // and has the smallest possible key of all such nodes } Suppose y is any node and Ty is the subtree rooted at y. Describe how you can replace Ty with a new tree T3 that contains all the keys and values from Ty, as well as k and v, as well as all the keys and values from t, so that T3 is still a binary search tree. This is the core of what the join() method does. What color should the new node for k and v be so that the red-black-tree properties are still true? Describe how to make these properties be true in ~lg(n) time. Argue that the running time of your new join() method is ~lg(n). (Your answer to this question can be very short. You will give a more detailed answer in the final lab report.) Before writing your design document, read through the starter code. The comments at the top of the file explain what methods you need to change. Be sure you look at the code for those methods so you can see what is already provided and what code you will need to write. This is a challenging problem! Please don't be discouraged if you don't "get it" right away. Please do ask questions. I'm here to help. I expect most students, if not all, will need some more explanation. Partial credit will be given for partial solutions. Focus on doing your best. Do not change the types of any of the methods (return types or argument types). You will not receive credit. Starter code: In Eclipse: Right-click on the "Lab3" icon in the left sidebar. If using eclipse with git, you may need to first right-click on the "cis27" folder and then select Team, then Pull, before you can see the "Lab3" icon. Select "Build Path" from the pop-up menu, then select "Add Libraries..." from the second menu that pops up. Make sure "JRE System Library" is selected (highlighted). Click Next. Click Finish. Right-click on the "Part1" folder underneath the "Lab3" icon. In the menu that pops up, select "Build Path", then select "Use as source folder" from the second menu that pops up. To run the code, right-click on the "RedBlackBST.java" icon in the sidebar and select "Run As", then "Java application" from the second menu that pops up. You can also go to the Run menu > "Run As" > "Java application". You will see an assertion failure, which is expected; start editing the code so that the tests will pass! Without Eclipse: See https://github.com/catamorphism/cis27/blob/master/Lab3/Part1/RedBlackBST.java (Links to an external site.)
Attached
No File uploaded yet.
Bidders
truewriter 6 years, 4 months ago
Rated 9.26 earned 20430.87 around 562 assignments.
Greetings buddy! I am a professional writer, trainer, programmer and editor with more than five years experience. Have my assurance that this assignment will be delivered EVEN BEFORE THE DUE TIME.NO PLAGIARISM AT ALL. I follow instructions as outlined. I don’t disappoint . You can trust me to deliver a well-researched and scholarly assignment to you
$350.00
  • {$ item.message_content $}
    {$ item.sender_username $} | {$ item.date_entered $}
 
 
vickymi 6 years, 4 months ago
Rated 8.96 earned 6249.41 around 194 assignments.
Please let me work on the order my job will speak for itself, I 0-guarantee perfection!!! NO PLAGIARISM, WELL STRUCTURED PAPER, NO GRAMMATICAL MISTAKES, EASY FLOW OF WORK, ADHERENCE TO ORDER INSTRUCTIONS AND SUBMISSION ON TIME!!!! Thank you
$350.00
  • {$ item.message_content $}
    {$ item.sender_username $} | {$ item.date_entered $}
 
 
Vinniax 6 years, 4 months ago
Rated 9.34 earned 13276.58 around 356 assignments.
I am a guru in computer science and i promise you an A in this work, please award me this bid now.
$280.00
  • 1. What code do you need to change in order to make the blackHeight instance variable reflect the actual black height of the tree? In other words: when, in the course of adding or deleting nodes, can the black height of a tree increase or decrease? 2. How will you implement the findBlackNodeWithLargestKey() and findBlackNodeWithSmallestKey() methods? Give a pseudocode description or an English description. Be sure to look over the starter code so you know what you have to change. 3. Suppose join() was called as follows: t.join(k, v, t2); Suppose t.blackHeight >= t2.blackHeight. Suppose y is any node and Ty is the subtree rooted at y. Describe how you can replace Ty with a new tree T3 that contains all the keys and values from Ty, as well as k and v, as well as all the keys and values from t2, so that T3 is still a binary search tree. 4. What if t2.blackHeight > t.blackHeight? What do we have to do differently? 5. What color should the new node be?
    User_28808 | May 19, 2020, 07:57 AM
  • 6. How will you add code (see the fixup() method) to preserve the red-black tree properties? 7. Explain how steps 3 and 6 preserve all the red-black tree properties: Why is the tree still a binary search tree after calling join()? How do you know that all red links lean left after calling join()? How do you know that no node has two red children after calling join()? How do you know that every path from the root to a null link has the same number of black links after calling join()? 8. Briefly explain why your algorithm for join() takes ~lg(n) time, where n = the number of nodes in t and t2. (You will give a more detailed explanation in the final lab report.)
    User_28808 | May 19, 2020, 07:57 AM
  • I for got to include these questions sorry!
    User_28808 | May 19, 2020, 07:58 AM
  • (This is a separate questions) Analyze the running time of your new join() method: Give a mathematical function for the worst-case running time of your code, in terms of n, where n is the total number of nodes in both of the two trees. The answer should be ~lg(n), but in your answer, you should justify why, in a general, mathematical way. Read pages 444-447 of the textbook for examples of how to write your analysis. Your final answer should use tilde notation. It's OK to hand-write your answer to this question and attach a scan or photo of it.
    User_28808 | May 19, 2020, 08:00 AM
  • OK, I WILL include them
    Vinniax | May 19, 2020, 08:00 AM
  • (separate question) Analyze the running time of your new join() method: Give a mathematical function for the worst-case running time of your code, in terms of n, where n is the total number of nodes in both of the two trees. The answer should be ~lg(n), but in your answer, you should justify why, in a general, mathematical way. Read pages 444-447 of the textbook for examples of how to write your analysis. Your final answer should use tilde notation. It's OK to hand-write your answer to this question and attach a scan or photo of it.
    User_28808 | May 19, 2020, 08:00 AM
  • thanks
    User_28808 | May 19, 2020, 08:04 AM
  • Welcome
    Vinniax | May 19, 2020, 08:06 AM
  • Hi, I am just wondering on the update of the homework
    User_28808 | May 21, 2020, 08:07 AM
  • I am halfway done.
    Vinniax | May 21, 2020, 09:00 AM
  • kindly release teh funds for this project and give me a bonus, thanks
    Vinniax | May 21, 2020, 10:49 AM
  • Also rate me 10/10 at the feedback level.
    Vinniax | May 21, 2020, 10:50 AM
  • Hello.
    Vinniax | May 21, 2020, 13:44 PM
  • Hey there. Where are you, release the payment so I can be able to pick new orders
    Vinniax | May 21, 2020, 14:01 PM
  • kindly come online and release the payment.
    Vinniax | May 21, 2020, 16:48 PM
  • hi there kindly release the payment
    Vinniax | May 21, 2020, 17:53 PM
  • hello kindly release the funds
    Vinniax | May 21, 2020, 19:14 PM
  • Do you have any other tasks?
    Vinniax | May 21, 2020, 22:14 PM
  • {$ item.message_content $}
    {$ item.sender_username $} | {$ item.date_entered $}
 
 
lupita 6 years, 4 months ago
Rated 9.36 earned 11670.86 around 435 assignments.
Hey, allow me to assist you with your assignment. Am an expert and only deliver quality and plagiarism free work within the stipulated time. I have the ability and experience to work on every possible niche. Accord me the chance to help you. Thank you
$300.00
  • {$ item.message_content $}
    {$ item.sender_username $} | {$ item.date_entered $}
 
 
highquality 6 years, 4 months ago
Rated 8.77 earned 11734.44 around 327 assignments.
Dear client, I have read and understood the task requirements and I can assure you that I will deliver a WELL-RESEARCHED and a HIGH QUALITY paper BEFORE the deadline. In addition, I promise to deliver a 100% plagiarism free paper that meets all your expectations. Kindly assent to my bid. Thank you in advance
$500.00
  • {$ item.message_content $}
    {$ item.sender_username $} | {$ item.date_entered $}