logo Hurry, Grab up to 30% discount on the entire course
Order Now logo

Ask This Question To Be Solved By Our ExpertsGet A+ Grade Solution Guaranteed

expert
Connor EvanssEconomics
(5/5)

896 Answers

Hire Me
expert
lahiruEducation
(/5)

741 Answers

Hire Me
expert
Pierre BernierrBusiness
(5/5)

699 Answers

Hire Me
expert
Eugene BaranowskiMathematics
(5/5)

530 Answers

Hire Me
Python Programming
(5/5)

you may follow the example of the contains method from lectures.

INSTRUCTIONS TO CANDIDATES
ANSWER ALL QUESTIONS

Task 1 (6 marks). At the point marked # TODO: Task 1, implement a method

 

lookup(self,key)

 

that returns the value associated with the given key (or None if this key is not present). A helper method is encouraged: you may follow the example of the contains method from lectures.

Task 2 (12 marks). At the point marked # TODO: Task 2, implement a method

 

plainInsert(self,key,value)

that inserts a new key-value pair into the ordered tree at the correct position, without worrying about colour information or the rules specific to red-black trees. The new node should be built using the Node constructor. If the given key is already present in the tree, the method should simply overwrite the existing value with the one given

— in this case it should not create a new node or make any other change to the tree.

You may follow the example of the insert method for plain binary trees from lectures. The case of inserting into an initially empty tree will need special treatment.

Once you have got the basic form of this method working, you should add suitable commands so that the path from the root to the new (or updated) node is recorded as a list in the self.stack field. Specifically, after calling the method, this field should contain the list of Node objects from the root to the new node, alternating with strings ’l’ or ’r’ to indicate the left or right branches. (We take advantage of the fact that Python permits mixed-type lists.) For example, calling

 

sampleTree.plainInsert(5,’five’) sampleTree.showStack()

 

should result in the list

 

[’2:two:B’, ’r’, ’4:four:R’, ’r’, ’6:six:B’, ’l’, ’5:five:R’]

 

If the tree is initially empty, the stack after the insertion should contain just one node.

Of course, the tree resulting from plainInsert may not be a legal red-black tree.  The purpose of Tasks 3 and 4 is to do the fix-up needed to obtain a legal tree.

 

 

 

 

 

Task 3 (12 marks). Here the task is to write code to apply the red-uncle rule as many times as possible.

By the current node, we shall mean the one at the top (= right-hand end) of the stack. The red-uncle rule will be applicable if the current node, its parent and its uncle are all coloured red.  In this situation, the rule may be applied by colour-flipping of nodes as described in the lecture.

Write a method tryRedUncle(self) that first tests whether the conditions of the rule apply. If they don’t, the method should simply return False without making any changes. If they do, the method should apply the rule and return True. In the latter case, it should also remove items from the stack as appropriate (using the pop method for lists) so that the new current node will be the one you have just coloured red: this will set things up correctly for the next attempt at the red-uncle rule.

Here and in Task 4, you’re encouraged to use the provided operations opposite, getChild, setChild to reduce needless duplication within your code.

Now write a method repeatRedUncle(self) that uses tryRedUncle to apply the rule as many times as possible (which may be zero times).

Task 4 (12 marks). Once we have applied red-uncle as much as possible, we will be in one of the endgame scenarios described in Lecture 9. Your main task here is to write a method endgame(self) that inspects the tree to see what needs to be done, and applies the appropriate action to turn it into a legal red-black tree. The provided functions toNextBlackLevel and balancedTree are there to help you in the case where some local re-balancing is called for. In other implementations (and in CLRS), this is achieved by means of certain rotation operations, but here I’ve followed the more simple-minded approach of inspecting the relevant subtree down to the next ‘level of blacks’, retrieving the relevant 7 components A,a,B,b,C,c,D (in the notation of the lecture slides), and building a new and more ‘balanced’ subtree from these.

Finally, add a method insert(self,key,value) that pulls everything together, in- serting the given key-value pair into the tree and performing all necessary fix-up.

 

You should test your code thoroughly to ensure that you’ve handled all cases correctly. One way to test it would be using the provided function TreeSort, which uses a red-black tree to sort a given list in O(n lg n) time. This isn’t a very serious contender for a sorting algorithm, though it’s somewhat reminiscent of the way HeapSort works. You might also like to do some experiments to compare the efficiency of  TreeSort with that of  SimpleTimSort on lists of various kinds.

 

(5/5)
Attachments:

Related Questions

. The fundamental operations of create, read, update, and delete (CRUD) in either Python or Java

CS 340 Milestone One Guidelines and Rubric  Overview: For this assignment, you will implement the fundamental operations of create, read, update,

. Develop a program to emulate a purchase transaction at a retail store. This  program will have two classes, a LineItem class and a Transaction class

Retail Transaction Programming Project  Project Requirements:  Develop a program to emulate a purchase transaction at a retail store. This

. The following program contains five errors. Identify the errors and fix them

7COM1028   Secure Systems Programming   Referral Coursework: Secure

. Accepts the following from a user: Item Name Item Quantity Item Price Allows the user to create a file to store the sales receipt contents

Create a GUI program that:Accepts the following from a user:Item NameItem QuantityItem PriceAllows the user to create a file to store the sales receip

. The final project will encompass developing a web service using a software stack and implementing an industry-standard interface. Regardless of whether you choose to pursue application development goals as a pure developer or as a software engineer

CS 340 Final Project Guidelines and Rubric  Overview The final project will encompass developing a web service using a software stack and impleme