1
I have a code to analyze and I’m not sure understand why this method of 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?
The
def
should be more thanclass
, right?– Victor Stafusa
Considering the @Victorstafusa remark, I believe
resp
should return the amount of knots from the tree, but this does not work on the leaves. From what I’m seeing, as for sheets it returns 0, it must be returning the amount of internal elements, which in case are only 15 and 9– Jefferson Quesado