I want to return the address of a node after I find it.
The node object class is inside a tree class set as private (private is the node inside)
I'll show you a function that's very similar to what I want but with a bool return type. It returns true if the node exists within the tree or false if it's not there, I only want to return the pointer or address of the node containing the element Im looking for, so I can work with it in the future.
below is the Function. (the "t" im passing is an integer)
template <class T>
class Arbol
{
private:
template <class DNodo>
class Nodo
{
public:
Nodo(const DNodo datoPasado,
Nodo<DNodo> *izqPasado=NULL,//HIJO IZQUIERDO NULL
Nodo<DNodo> *derPasado=NULL)//HIJO DERECHO NULL
: dato(datoPasado),izq(izqPasado),der(derPasado){}
Nodo();
//members
DNodo dato;
Nodo<DNodo> *izq;
Nodo<DNodo> *der;
};
Nodo<T> *raiz;//variable raiz de la clase interna
Nodo<T> *actual;//posicion actual
int contador;//contador
int altura;//altura
////////////////////////////////////////////////
public:
Arbol() : raiz(NULL), actual(NULL){};
//Iniciar(const T &t);
~Arbol();
//INSERTO 开发者_如何学CEN EL ARBOL
void Insertar(const T t);
//BORRO ELEMENTO DEL ARBOL
void Borrar(const T t);
//Busca un elemento del arbol
bool Buscar(const T t);
//Busca y devuelve puntero a elemento
Nodo<T>* BuscarDevolver(const T t);
//EsVacio ????
bool EsVacio();
};
template<class T>
Node<T>* Arbol<T>::BuscarDevolver(const T t)
{
if(!EsVacio())
{
actual = raiz;
while(actual!=NULL)
if(actual->dato == t)
return actual;
else if(t < actual->dato)
{
actual = actual->izq;
}
else if(t > actual->dato)
{
actual = actual->der;
}
}
return NULL;
}
As you may noticed it Im searching for a node in an Binary tree Thanks in advance for trying to help.
Im getting errors like "Node does not name a type"
Node
is a nested type within Arbol
, so you should actually declare your function like this:
template< class T > Arbol<T>::Node<T>* Arbol<T>::Buscar(const T t);
精彩评论