-1

my class 'tree' contains a check_balance() function definition inside, but when i try to print it it says it's not defined.. even though it should be. could you tell me what i'm doing wrong? this has happened many times and i cannot understand. thanks

class tree():
    def __init__(self,a=None,lc=None,rc=None):
        self.label=a
        self.lt=lc
        self.rt=rc
        if a is not None:
            if lc is None:
                self.lt=tree()
            if rc is None:
                self.rt=tree()

    def getLabel(self):
        return self.label

    def isEmpty(self):
        return self.label is None

    def isLeaf(self):
        return (self.lt.isEmpty() and self.rt.isEmpty())

    def getLc(self):
        return self.lt

    def getRc(self):
        return self.rt
    
    def check_balance(t):
        if t.isLeaf() or t.isEmpty():
            return True
        if t.getLc().isEmpty() or t.getRc().isEmpty():
            return False
        return check_balance(t.getLc()) and check_balance(t.getRc())

a = tree(1, tree(2, tree(7), tree(8)), tree(3, tree(7), tree()))
print(check_balance(a))

1 Answers1

0

The correct syntax for this would be:

a.check_balance()
Quantum
  • 510
  • 1
  • 2
  • 19