-1

I made two trees in python with two different instances of the same class. I need to access the method in the class now with both the instances(roots) with which i created two trees.

# To create a tree from scratch
class node:
    """To create nodes each time an instance has been
    created"""
    def __init__(self, key):
        self.data = key
        self.left = None
        self.right = None

def issame(self,root1,root2):
  if root1 or root2:
    print(root1.data,root2.data)
    self.issame(root1.left,root2.left)

"""To insert the data manually"""
##First Tree
root1 = node(10)
root1.left = node(20)
root1.right = node(30)
root1.left.left = node(50)

##Second Tree
root2 = node(20)
root2.left = node(20)
root2.right = node(30)
root2.left.left = node(50)

issame(root1,root2)

I read in some post that we can keep the issame function definition outside of the class and use self within. Now at the end if i call the function, it says issame doesn't exist. Issame is used to check if leaf nodes of tree are same and the method is not fully developed as i faced the problem in first step.

user7422128
  • 902
  • 4
  • 17
  • 41
  • What is `issame` intended to do? – Reblochon Masque Nov 16 '17 at 01:13
  • just a method to check if the leaf nodes are same. Full code for it has not been written yet as i faced the difficulty in first step – user7422128 Nov 16 '17 at 01:14
  • "I read in some post that we can keep the issame function definition outside of the class and use self within" I'm not entirely sure if I am interpreting this correctly, but if I am, but it sounds wrong, or at the very least, not advisable. – juanpa.arrivillaga Nov 16 '17 at 01:19
  • https://stackoverflow.com/questions/12008991/python-create-instance-of-class-in-another-class-with-generic-example – user7422128 Nov 16 '17 at 01:21

2 Answers2

3

You must choose whether you want issame() in the class, or outside, but it cannot be a hybrid of a little bit of both:

You must also override the __eq__ method of your Node class in order to compare two nodes with the operator ==

issame as a method:

class Node:
    """To create nodes each time an instance has been
    created"""
    def __init__(self, key):
        self.data = key
        self.left = None
        self.right = None

    def __eq__(self, other):
        assert isinstance(other, Node)
        return self.data == other.data \
               and self.left == other.left \
               and self.right == other.right

    def issame(self, root2):
      return self == root2

"""To insert the data manually"""
##First Tree
root1 = Node(10)
root1.left = Node(20)
root1.right = Node(30)
root1.left.left = Node(50)

##Second Tree
root2 = Node(20)
root2.left = Node(20)
root2.right = Node(30)
root2.left.left = Node(50)

root1.issame(root2)          #<-- here you are calling issame on root1, with root2 as argument

issame as a function:

class Node:
    """To create nodes each time an instance has been
    created"""
    def __init__(self, key):
        self.data = key
        self.left = None
        self.right = None

    def __eq__(self, other):
        assert isinstance(other, Node)
        return self.data == other.data \
               and self.left == other.left \
               and self.right == other.right

def issame(root1, root2):
    return root1 == root2

"""To insert the data manually"""
##First Tree
root1 = Node(10)
root1.left = Node(20)
root1.right = Node(30)
root1.left.left = Node(50)

##Second Tree
root2 = Node(20)
root2.left = Node(20)
root2.right = Node(30)
root2.left.left = Node(50)

issame(root1, root2)       # < attention, different way of calling
                           # here you are calling issame passing root 1 and root2 as arguments
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
  • Can you please explain what you trying to achieve with the __eq__ module here? – user7422128 Nov 16 '17 at 01:49
  • 1
    `__eq__` is the underlying operations behind the `==` operator. It is the operator that tests for equality. For complex objects, you must override it so equality is tested the way you intend it to be; if not, the memory address of the objects will be tested for equality, not the values, and not the children, or whatever else you are using to assert equality between two of your `Node` objects – Reblochon Masque Nov 16 '17 at 02:34
  • 1
    Thanks for the info, but I need to comapre the leaf values not the leaf objects for the two trees I passed. – user7422128 Nov 16 '17 at 04:41
  • 1
    Yes, that is what it does: Read the code, `__eq__` recursively calls `==` on the leaves to compare them; if they are all equal, it returns True, if not, False – Reblochon Masque Nov 16 '17 at 04:47
2

Try modify your issame method (outside class):

def issame(root1,root2):
   if root1 or root2:
   print(root1.data,root2.data)
       issame(root1.left,root2.left)

I got the below result:

Python 3.6.1 (default, Dec 2015, 13:05:11)[GCC 4.8.2] on linux
10 20
20 20
50 50

Full code:

# To create a tree from scratch
class node:
    """To create nodes each time an instance has been created"""
    def __init__(self, key):
        self.data = key
        self.left = None
        self.right = None

def issame(roota,rootb):
   if roota or rootb:
      print(roota.data,rootb.data)
      issame(roota.left,rootb.left)

"""To insert the data manually"""
##First Tree
root1 = node(10)
root1.left = node(20)
root1.right = node(30)
root1.left.left = node(50)

##Second Tree
root2 = node(20)
root2.left = node(20)
root2.right = node(30)
root2.left.left = node(50)

issame(root1,root2)
Nguyen Pham
  • 444
  • 1
  • 4
  • 14
  • done... you can also try @classmethod (http://www.pythoncentral.io/difference-between-staticmethod-and-classmethod-in-python/) so you can make issame the method for the whole class (not instance) – Nguyen Pham Nov 16 '17 at 01:54
  • He is trying to traverse the tree to leaf node then compare but not finish that part yet. For now he got stuck in the traverse part – Nguyen Pham Nov 16 '17 at 03:01
  • That’s why I suggest him better use classmethod. So can cal method directly from class without instance to compare trees later – Nguyen Pham Nov 16 '17 at 03:04