You are allowed to add data members to both Node and SetList. Node The Node class is
Question:
You are allowed to add data members to both Node and SetList.
Node
The Node class is declared within SetList. It stores:
- a piece of data
- a reference to the next Node in the SetList (None if Node is the last node)
- a reference to the previous Node in the SetList (None if Node is the first node)
- a reference to the SetList.
When a Node is initialized, it is passed a data value and a reference to the SetList the Node belongs to. Optionally it is also passed a reference to the next node and a reference to the previous node (in that order). If the data values are not passed in, they are defaulted to None.
The Node function has the following member functions:
def get_data(self)
function returns data stored in node
def get_next(self)
function returns reference to next node in SetList
def get_previous(self)
function returns reference to previous node in SetList
def get_set(self)
function returns reference to the SetList
SetList
A SetList is a doubly linked list that can be used to represent a single set of a disjoint set. With a SetList, each node contains data and is linked to other nodes in the list. The difference with a regular doubly linked list is that each node also contains a link to the SetList object. The first node within the SetList contains the set's representative.
The following is a diagram of the set: {"cat", "fish", "dog"}. Note that values within a set do not have any particular ordering and the exact ordering does not matter in your implementation. This means that any of the 3 can be the representative..its simply a matter of which one is at the front of the list
When a SetList is initialized, the SetList is empty.
The SetList has the following member functions
def get_front(self)
This function returns a reference to the first node in the list
def get_back(self)
This function returns a reference to the last node in the list
def make_set(self, data)
This function adds the first value to the SetList object if the list is empty and returns a reference to newly created Node. If the list is not empty, function does nothing and returns None.
def union_set(self, other_set)
This function is passed a SetList and performs a union with the current object. This means that the elements of other_set are transfered to current object and other_set becomes empty in the process. Function returns number of elements transferred. Note, this process does not create or destroy any of the nodes.
def find_data(self, data)
This function is passed a data value, the function returns the a reference to the Node containing data. If data does not exist, function returns None
def representative_node(self)
This function returns a reference to the node that holds the representative of the SetList. If SetList is empty, the function returns None
def representative(self)
This function returns the data of the representative node of the SetList. If SetList is empty, the function returns None
def __len__(self)
This function returns the number of items within the SetList.
Java An Introduction To Problem Solving And Programming
ISBN: 9780134462035
8th Edition
Authors: Walter Savitch