Estoy intentado buscar un nodo de un árbol de forma recursiva...
La primera vez que uso el método print(t.searchForEuristicID(0))
funciona perfectamente pero la segunda vez me dice:
TypeError: 'Node' object is not callable
Lo que necesito hacer es lo siguiente:
Un método al cual yo le ingrese el ID de un nodo, y buscarlo de forma recursiva... y que me retorne ese nodo en particular.
Lo logre pero solo la primera vez... para la segunda me devuelve un error.
class Node(object):
def __init__(self, data, childrens, euristic) -> None:
"""
The first node by 0 the song of 0 by 1 and the new son by 3...
"""
self.euristic = euristic # This is a numberOrder to resolve a púzzle
self.data = data # contain information
self.childrens = childrens # []
class Three:
def __init__(self) -> None:
"""
data = puzzle status example [321875649]
the childrens if you can mouve
never save 2 equals childern
childers = [<MovRight><MovDown><MovLeft><MovUP>]
"""
self.controlList = [] # I never save 2 equal children
self.idControl = 0 # Unique id for every node
self.pivot = None # Main node
self.searchNodeById = None # If you need a node by id
def addData(self, father, data):
if self.pivot == None:
self.pivot = Node(data, [], self.idControl)
self.controlList.append(data)
self.idControl = self.idControl + 1
else:
if data not in self.controlList:
self._addData(self.pivot, father, data)
def _addData(self, pivot, father, data):
if father == pivot.data:
pivot.childrens.append(Node(data, [], self.idControl))
self.controlList.append(data)
self.idControl = self.idControl + 1
else:
for i in pivot.childrens:
self._addData(i, father, data)
def searchForEuristicID(self, id):
"""
Return a node with id
"""
self.searchForEuristicID = None
self._searchForEuristicID(self.pivot, id)
return self.searchForEuristicID
def _searchForEuristicID(self, node, id):
if node.euristic == id:
self.searchForEuristicID = node
else:
for i in node.childrens:
self._searchForEuristicID(i, id)
def showThree(self):
"""
Show all data nodes
"""
self._showThree(self.pivot)
def _showThree(self, pivot):
if pivot != None:
print("Data>",pivot.data, "Eur>",pivot.euristic, "Childs: ", len(pivot.childrens))
for i in pivot.childrens:
self._showThree(i)
oneShoot = [1,2,3,4,5,6,7,9,8]
t = Three()
t.addData(None, oneShoot)
t.addData(oneShoot, [1,2,3,4,5,6,9,7,8])
t.addData(oneShoot, [1,2,3,4,9,6,7,5,8])
t.addData(oneShoot, [1,2,3,4,5,6,7,8,9])
t.addData([1,2,3,4,9,6,7,5,8], [3,3,3])
t.showThree()
print(t.searchForEuristicID(0))
print(t.searchForEuristicID(0))
La verdad es que no entiendo por que siempre funciona la primera vez, pero no la segunda.