Move the root and data to the __init__ definition. Be that as it may, you have defined them as class attributes. This makes them available to all instances of the Tree class. When you create two trees ( tree and tree2 ), they both use the same list, which are accessed using self.data . For each instance to have its own instance attribute, you must move the declaration to the __init__ function.
def __init__(self, equation): self.root = equation self.data = []
Also use
if isinstance(item,Tree): # This is True if item is a subclass of Tree
instead
if (type(item) == type(self)):
and change
data = self.data
to
data = self.data[:]
in getRight . When you say data = self.data , then the variable name data points to the same list that self.data points self.data . When you subsequently flip data , you will also cancel self.data . To undo only data , you must copy the list. self.data[:] uses cutting notation to return a copy of the list. Note that self.data elements can be tree s, and self.data and self.data[:] can contain the same elements. I don't think your code requires these elements to be copied, but you will need to copy self.data recursively, if so.
def getRight(self): data = self.data[:] data.reverse() return data
source share