class No:
def __init__(self, dado):
self.esq = None
self.dir = None
self.dado = dado
def __str__(self):
s = '(' + str(self.dado) + ','
if self.esq != None:
s += str(self.esq) + ','
else:
s += 'X,'
if self.dir != None:
s += str(self.dir) + ')'
else:
s += 'X)'
return s
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
def __str__(self):
if self.raiz != None:
return str(self.raiz)
else:
return 'X'
T = Arvore()
T.inserir(15)
T.inserir(9)
T.inserir(5)
T.inserir(12)
T.inserir(20)
print(T.resp())
print(T)
Y2xhc3MgTm86CiAgICBkZWYgX19pbml0X18oc2VsZiwgZGFkbyk6CiAgICAgICAgc2VsZi5lc3EgPSBOb25lCiAgICAgICAgc2VsZi5kaXIgPSBOb25lCiAgICAgICAgc2VsZi5kYWRvID0gZGFkbwoKICAgIGRlZiBfX3N0cl9fKHNlbGYpOgogICAgICAgIHMgPSAnKCcgKyBzdHIoc2VsZi5kYWRvKSArICcsJwogICAgICAgIGlmIHNlbGYuZXNxICE9IE5vbmU6CiAgICAgICAgICAgIHMgKz0gc3RyKHNlbGYuZXNxKSArICcsJwogICAgICAgIGVsc2U6CiAgICAgICAgCXMgKz0gJ1gsJwogICAgICAgIGlmIHNlbGYuZGlyICE9IE5vbmU6CiAgICAgICAgICAgIHMgKz0gc3RyKHNlbGYuZGlyKSArICcpJwogICAgICAgIGVsc2U6CiAgICAgICAgCXMgKz0gJ1gpJwogICAgICAgIHJldHVybiBzCgpjbGFzcyBBcnZvcmU6CiAgICBkZWYgX19pbml0X18oc2VsZik6CiAgICAgICAgc2VsZi5yYWl6ID0gTm9uZQogICAgZGVmIHBlZ2FyUmFpeihzZWxmKToKICAgICAgICByZXR1cm4gc2VsZi5yYWl6CgogICAgZGVmIGluc2VyaXIoc2VsZiwgdmFsKToKICAgICAgICBpZiBzZWxmLnJhaXogPT0gTm9uZToKICAgICAgICAgICAgc2VsZi5yYWl6ID0gTm8odmFsKQogICAgICAgIGVsc2U6CiAgICAgICAgICAgIHNlbGYuX2luc2VyaXIodmFsLCBzZWxmLnJhaXopCgogICAgZGVmIF9pbnNlcmlyKHNlbGYsIHZhbCwgbm9kZSk6CiAgICAgICAgaWYgdmFsIDwgbm9kZS5kYWRvOgogICAgICAgICAgICBpZiBub2RlLmVzcSAhPSBOb25lOgogICAgICAgICAgICAgICAgc2VsZi5faW5zZXJpcih2YWwsIG5vZGUuZXNxKQogICAgICAgICAgICAgICAgbm9kZS5lc3EucGFpID0gbm9kZQogICAgICAgICAgICBlbHNlOgogICAgICAgICAgICAgICAgbm9kZS5lc3EgPSBObyh2YWwpCiAgICAgICAgZWxzZToKICAgICAgICAgICAgaWYgbm9kZS5kaXIgIT0gTm9uZToKICAgICAgICAgICAgICAgIHNlbGYuX2luc2VyaXIodmFsLCBub2RlLmRpcikKICAgICAgICAgICAgICAgIG5vZGUuZGlyLnBhaSA9IG5vZGUKICAgICAgICAgICAgZWxzZToKICAgICAgICAgICAgICAgIG5vZGUuZGlyID0gTm8odmFsKQoKICAgIGRlZiByZXNwKHNlbGYpOgogICAgICAgIGlmIHNlbGYucmFpeiAhPSBOb25lOgogICAgICAgICAgICByZXR1cm4gc2VsZi5fcmVzcChzZWxmLnJhaXopCgogICAgZGVmIF9yZXNwKHNlbGYsIG5vZGUpOgogICAgICAgIGF1eF9lc3EgPSAwCiAgICAgICAgYXV4X2RpciA9IDAKICAgICAgICBpZiBub2RlLmVzcSAhPSBOb25lOgogICAgICAgICAgICBhdXhfZXNxID0gc2VsZi5fcmVzcChub2RlLmVzcSkKICAgICAgICBpZiBub2RlLmRpciAhPSBOb25lOgogICAgICAgICAgICBhdXhfZGlyID0gc2VsZi5fcmVzcChub2RlLmRpcikKICAgICAgICBpZiAobm9kZS5lc3EgIT0gTm9uZSkgb3IgKG5vZGUuZGlyICE9IE5vbmUpOgogICAgICAgICAgICByZXR1cm4gMSArIGF1eF9lc3EgKyBhdXhfZGlyCiAgICAgICAgZWxzZToKICAgICAgICAgICAgcmV0dXJuIDAKCiAgICBkZWYgX19zdHJfXyhzZWxmKToKICAgICAgICBpZiBzZWxmLnJhaXogIT0gTm9uZToKICAgICAgICAgICAgcmV0dXJuIHN0cihzZWxmLnJhaXopCiAgICAgICAgZWxzZToKICAgICAgICAgICAgcmV0dXJuICdYJwoKVCA9IEFydm9yZSgpClQuaW5zZXJpcigxNSkKVC5pbnNlcmlyKDkpClQuaW5zZXJpcig1KQpULmluc2VyaXIoMTIpClQuaW5zZXJpcigyMCkKcHJpbnQoVC5yZXNwKCkpCnByaW50KFQp
2
(15,(9,(5,X,X),(12,X,X)),(20,X,X))