A bag is an unordered collection that can contain duplicate items. Otherwise, it behaves like a set. Define a class HashBag, which does not add multiple instances of the same datum to itself, but...


A bag is an unordered collection that can contain duplicate items. Otherwise, it behaves like a set. Define a class HashBag, which does not add multiple instances of the same datum to itself, but instead maintains a counter for each unique datum. This class extends the class HashSet and overrides the definitions of the methods add , remove, and __str__. When the first instance of a datum is added to the bag, the bag wraps it in a new object of the class BagNode. This class extends the Node class by adding a field for the count of instances. The count is initially 1 and is simply incremented each time the bag receives a duplicate instance of the node’s datum for insertion. Likewise, when a datum is removed, its node’s counter is simply decremented. When the counter becomes 0, the datum’s node is also removed. The __str__ method should build a string that includes the number of instances of each item.



May 19, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here