I have a code to parse and I can not really understand why this method in my Binary Tree returns me 2.
Code:
class No:
def __init__(self, dado):
self.esq = None
self.dir = None
self.dado = dado
class Arvore:
def __init__(self):
self.raiz = None
def pegarRaiz(self):
return self.raiz
def inserir(self, val):
if self.raiz == None:
self.raiz = No(val)
else:
self._inserir(val, self.raiz)
def _inserir(self, val, node):
if val < node.dado:
if(node.esq != None):
self._inserir(val, node.esq)
node.esq.pai = node
else:
node.esq = No(val)
else:
if node.dir != None:
self._inserir(val, node.dir)
node.dir.pai = node
else:
node.dir = No(val)
def resp(self):
if(self.raiz != None):
return self._resp(self.raiz)
def _resp(self,node):
aux_esq = 0
aux_dir = 0
if node.esq != None:
aux_esq = self._resp(node.esq)
if node.dir != None:
aux_dir = self._resp(node.dir)
if (node.esq != None) or (node.dir != None):
return 1 + aux_esq + aux_dir
else:
return 0
T = Arvore()
T.inserir(15)
T.inserir(9)
T.inserir(5)
T.inserir(12)
T.inserir(20)
T.resp()
After the program runs it returns me 2, why?